Simple Node.js validation module modelled after the Laravel validation. (http://laravel.com/docs/validation) The actual validation is done using the validator module from chriso (https://github.com/chriso/validator.js)
So far I only ported the following rules:
- required
- url
- numeric
- in:val1,val2,val3
- not_in:val1,val2,val3
- same:fieldname
- min:number
- max:number
If you like to add more you're more than welcome to do so!
Usage:
- Install the validator module with npm (npm install validator)
- Require this file
Examples:
var validate = require('./obj-validator');
var input = {
email: '[email protected]',
firstname: 'John',
lastname: 'Cooper',
age: 32,
password: 'supersecret',
password_repeat: 'supersecret',
gender: 'm'
}
var rules = {
email: 'required|email',
firstname: 'required',
lastname: 'required',
age: 'required|numeric',
password: 'required|min:5|max:30',
password_repeat: 'required|same:password',
gender: 'in:m,f'
}
validate(input, rules, function(validates, errors) {
console.log('Validates:', validates);
console.log('Errors:', errors);
});