Validates schemas with support for async callbacks, promises, and thunks This project is still in it's infancy, documentation and a stable API coming soon
Install with NPM:
$ npm install ilsken/validator
Creates a new validator, you may optionally pass an object with names/rules
Adds a new rule and returns itself for chaining. There are 4 main types of rules
v.rule('min', function(value, min_value) {
if (min_value !=== false && value < min_value) throw new TypeError('must be greater than ' + min_value)
})
.validate(18, {min: 21}, function(errors) {
console.log(errors[0].message) // `min`: must be greater than 21
})v.rule('min', function(value, min_value, next) {
// call next(error) to report an error, next() if the value is valid
})v.rule('min', function (value, min_value) {
return functionThatReturnsAPromise(value, min_value)
})v.rule('min', function (value, min_value) {
return function (next) {
next(new Error('something went wrong'))
}
})v.rule('can drink', 'age >= 21')
.enable('can drink')
.validate({age: 18}, callback)Validates a value with the rules/settings from settings.
You may optionally supply a context object which will be used to call the rule functions (rule.call(context, value, settings[rule_name], next))
If you don't pass a settings object it'll use the settings set via Configurable methods such as set, enable, or disable
You can enable rules and set their configuration values the methods defined by visionmedia/configurable.js.
By default validator will execute all the rules you've chosen settings for even if they throw errors (so you can get the full list of errors). If you wish to fail on the first validation error you can use v.enable('strict')
Async validator function must either take 3 arguments (value, options, next), return a promise, or return a thunk
MIT
