What is validate-obj?
validate-obj is a javascript validation framework, easy to validate object or form, and is very easy to extend. validate-obj mainly for node.js server-side, but also work for front-end.
Why I create validate-obj?
I started this project just because I cannot find existing one fits my requirements. There are many other validation projects, but many of them is very easy to end up code like the following:
var validator = ;var myUserForm = name: 'John' // required, string email: 'Jonh@emailDomain.com' // required, email age: 27 // int gender: 'male' // male or female createdOn: '10/12/1987'; //Start validatingvar errs = if typeof myUserFormname === undefined errs;else if !validator errs; if typeof myUserFormemail === undefined errs; else if myUserFormemail !== undefined && !validator errs; if typeof myUserFormage !== undefined && !validator errs; if typeof myUserFormgender !== undefined && !validator errs;
Are you tired of the above? How about we just simply do the following?
var v = ;var myUserForm = name: 'John' // required, string email: 'Jonh@emailDomain.com' // required, email age: 27 // int gender: 'male' // male or female createdOn: '10/12/1987';var validationExpression = name: vrequired visString email: vrequired visEmail age: visNumber gender: v createdOn: visDate; var errs = v;
It is beautiful, isn't it? and even we save this validation sytax object and reuse it.
How to install?
For node.js
npm install validate-obj
For front end (coming soon)
bower install validate-obj
APIs
Overview
var v = ; //load the library, to make it short, I will not include it in the following examples// target: the object going to be validated// validationExpression: the validation defination indicating how to validate// return value: null means no errors or array of error stringv
To validate simple variables
v // ==> nullv // ==> ['it is not number']v // ==> nullv // ==> ['it must be one of (male, female)']
To override the error message
v // ==> ['age should be a number']v // ==> ['gender has to be male or female']v // ==> ['gender has to be male or female']
Validate with multiple validators
v) // ==> nullv // ==> ['it is required']
To validate array
v // ==> it is not arrayv // ==> null
To validate object
v // ==> nullv // ==> ['it.age is not number']
To validate complex object
v// ==> nullv// ==> ['it.items[0].sku is not number']
Validate logic which have multiple properties involved
var schema =password: visString vrequiredmatchedPassword: visString vrequired{if objpassword !== objmatchedPassword return 'passwords do not match';return undefined;};v // => nullv // => ['passwords do not match']// The selfCrossValidators can also be array like the belowvar schema =password: visString vrequiredmatchedPassword: visString vrequiredselfCrossValidators : {if objpassword !== objmatchedPassword return 'passwords do not match';return undefined;};
Extend your custom validators
- With auto error message
v;v // ==> nullv // ==> ['it is invalid']
- With error message
v;v; // ==> nullv // ==> ['it is not gender']
Built-in validators
- required
- isDate
- isBool
- isString
- isNumber
- isEmail
- isCreditCard
- isIn
- minLength
- maxLength
- isUrl
- isBefore
- isAfter
- isObject
Progress
The whole framework is done, the only missing is not enough built-in validators. I will add more soon.