waechter
simple, functional, sync/async validation for Node.js and browsers
inspired by Prismatic/schema
waechter is german for guardian
npm install waechter
bower install waechter
lib/waechter.js supports AMD.
if AMD is not available it sets the global variable waechter
.
require:
> var waechter = ;
predicates
a predicate is a function that takes a value and returns a boolean indicating whether that value is valid.
is.js
is a big collection of predicates.
waechter doesn't reinvent the wheel and uses is.js predicates:
> var isjs = ; > isjs;false > isjs;true
validators
a validator is a function that takes a value and
returns nothing (null
or undefined
) if the value is valid.
otherwise it returns a value describing the error.
that value is usually a string that is a helpful error message
or an object whose values are error messages.
you can make a validator from a predicate using waechter.predicateToValidator
> var validateEmail = waechter;
you can then use the validator to validate some data:
> ;'must be an email address' > ;null
these validators are builtin
waechter.exist
waechter.string
waechter.stringNotEmpty
waechter.email
waechter.stringMinLength(min)
waechter.number
waechter.numberWithin(min, max)
(range is exclusive)waechter.true
waechter.false
waechter.undefined
waechter.null
waechter.boolean
you can easily make your own validators using waechter.predicateToValidator
.
composing validators
waechter.and(validators...)
returns a validator that returns
null if all validators return null and otherwise returns the first error.
waechter.or(validators...)
returns a validator that returns
null if at least one of the validators returns null and otherwise returns
an array of errors.
use waechter.undefinedOr(validators...)
to make things optional.
schemas
a schema is an object whose values are validators:
> var userSchema = email: waechteremail password: waechterstringNotEmpty;
you can make a validator from a schema:
> var validateUser = waechter;
you can then use that validator to validate the structure of objects:
> ; email: 'must be an email address' password: 'must not be null or undefined'
> ;null
keys that are not present in the schema are not allowed in the data:
> ; is_admin: 'disallowed key'
async validators
an async validator is like a validator but returns a promise.
you can lazily (only when needed) run async validators after sync validators like so:
> var userSchema = email: waechteremail password: waechterstringNotEmpty; > var userSchemaAsync = { return ; }; > validateUser = waechter;
you can mix schemas with sync and async validators in the arguments to
waechter.schemasToLazyAsyncValidator
.
validators in later schemas are only run for keys that have no errors yet:
>;
here the validator userSchemaAsync.email
wasn't called.
>;
this time the validator userSchemaAsync.email
was called.
see the tests for more usage examples.