The Validator!
Capable of leaping across language barriers, validating data in a flash, and rescuing small kittens.
npm install the_validator --save
By using a JSON interface, Validator can be implemented in any programming language. Validator prevents duplication of validation logic on the client and server.
Create a schema
tests are of the form
testType
required
Required will fail if the tested value is null, undefined, or the empty string.
illegalField
ensures that the test data does not contain the given key. illegalField should be the first and only test on a field.
minimumLength
string must be of the specified minum length
maximumLength
analogous logic to minimumLength
regex
must match the supplied regex
type
can test for types number, string, boolean, and object.
comparison operators
perform the <, >, <=, >=, and == operators.
test if matches a proper email format
uuid
test if matches a proper uuid format
match
tests both values of a passed array of two values match
enumerated
tests that value is a member of a set of values
numeric
Numeric strings. accepts values such as "123", "1.2", ".5", "05", "1."
integer
Integer strings. Only characters 0-9
alphabetical
Strings of only letters (upper or lower case)
alphanumeric
Strings of only letters and digits
allowNull
Allow null values to pass through, regardless of other validation rules applied to the field.
Run
var validator = schema; var errors = validator;
Strict Mode
Only allow fields explicitly listed in the schema
var validator = schema; var errors = validator;
customMessageMaps
Create your own error messages
var errors = foo: '<:5' customMessageMaps: { return 'The field ' + name + ' should be less than ' + testValue; } ;