valjs

0.0.28 • Public • Published

Installation

npm install --save valjs

Simple example

Let's validate if a string is minimum 5 chars.

import { string } from 'valjs';
 
let error = string.min(5).test('hello')
// passes! (null)

Badass example

Let's validate this

const todo = {
  id: 'todo-1',
  title: 'Ship Login Page',
  updatedAt: '2017-01-21T22:54:45Z',
  state: 'completed',
  assignees: [
    'user-1'
  ],
  subtasks: [
    { title: 'Design' },
    { title: 'Develop' },
    { title: 'QA' }
  ]
}

Like this

import { object, string, array, any } from 'valjs';
 
const error = object.as({
  id: string.require(),
  title: string.require().min(1).max(255),
  updatedAt: string.format('iso8601'),
  state: any.of('active', 'completed'),
  assignees: array.require().of(string.min(6).startsWith('user-')),
  subtasks: array.of(object.as({
    title: string.require().min(1).max(60)
  }))
}).test(todo);
// passes!

Here are all the supported types

import valjs, {
  string,
  number,
  bool,
  func,
  object,
  array,
  date,
  any
} from 'valjs';

string API

number API

bool API

func API

object API

array API

date API

any API

Package Sidebar

Install

npm i valjs

Weekly Downloads

35

Version

0.0.28

License

MIT

Last publish

Collaborators

  • kasperpihl