hueitan / isomorphic-validation

An isomorphic validation engine for browser client and node app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ISOMORPHIC VALIDATION Build Status Coverage Status

An isomorphic validation engine for browser client and node app.

Engine - the core engine inside the validation

Node.js

npm install --save isomorphic-validation
import isomorphicValidation from isomorphicValidation;

const iv = new isomorphicValidation();

// implement
iv.addValidation({
  'required': {
    'validator': /^.+$/
  }
});

iv.validate({
  name: 'name',
  value: 'huei90',
  type: 'required'
})
.then((val) => console.log('form is valid'))
.catch((val) => console.log('form is invalid'));

Browser Client

Babel

Usage

Constructor

given parameter types

const iv = new isomorphicValidation(types)

Adding validation type

validator type accepts RegExp and Function

// adding multiples:
iv.addValidation({'required': {validator: /^.+$/}, age: {validator: /\d+/}})

// adding function:
iv.addValidation({'required': {validator: (v) => !!v})

type format

{
  'required': { // validation type name
    'validator': /^.+$/ // validation type - RegExp or Function
  }
}

validate form

validate the form(s) and return Promise

// validate form
iv.validate([{
    name: 'name',
    value: 'huei',
    type: 'required'
  },{
    name: 'age',
    value: 30,
    type: 'age']
  }])
  .then(/* ... */)
  .catch(/* ... */);

form format

[{
  name: 'name', // form name
  value: 'huei', // value of the form name
  type: 'required' // type name (given in type format)
}]

return all types

iv.getAllTypes(); // => all types

License

MIT

About

An isomorphic validation engine for browser client and node app


Languages

Language:JavaScript 100.0%