express-req-validate
Mongoose-inspired req.query and req.body validator for express/connect servers.
Installation
npm install --save express-req-validate
Load the validator middleware after the body parser.
var bodyParser = ;var queryParser = ;var reqValidate = ; app;app;app;app;
req.body
Validating By default the module will only attempt to validate req.query
. To validate req.body
:
req;
To validate both req.query
and req.body
:
req;
Model
model.type
Checks if request parameter is created by the specified constructor.
req;// ?a=b => true// ?a[0]=b => false
Note: By default, numbers in query parameters will be strings. To use { type: Number }
you will need a parser such as express-query-int.
model.required
Checks if request parameter is present.
req;// ?a => true// ?a=0 => true// ?a=b => true
model.min
If parameter is a Number, checks that it is greater than or equal to min
.
req;// ?a=10 => true// ?a=11 => true// ?a=9 => false
If parameter is a String, checks that the string is longer or equal length to min
.
req;// ?a=abc => true// ?a=abcd => true// ?a=ab => false
If parameter is an Array, checks that the array.length is greater than or equal to min
.
req;// ?a[0]=b&a[1]=c => true// ?a[0]=b&a[1]=c&a[2]=d => true// ?a[0]=b => false
If no type is specified, validator will do its best to guess. To ensure that you get the expected result, it is recommended to provide a type.
model.max
If parameter is a Number, checks that it is less than or equal to max
.
req;// ?a=10 => true// ?a=9 => true// ?a=11 => false
If parameter is a String, checks that the string is shorter or equal length to max
.
req;// ?a=abc => true// ?a=ab => true// ?a=abcd => false
If parameter is an Array, checks that the array.length is less than or equal to max
.
req;// ?a[0]=b&a[1]=c => true// ?a[0]=b => true// ?a[0]=b&a[1]=c&a[2]=d => false
If no type is specified, validator will do its best to guess. To ensure that you get the expected result, it is recommended to provide a type.
model.each
Checks that each value of the Object/Array meets condition. Return:
true
- passes validationfalse
- fails validationnew Error('msg')
- fails validation with custom message
req;// ?a[0]=b&a[4]=c => true// ?a[0]=b&a[2]=c => false
model.validator
Checks that value passes custom function validation. Return:
true
- passes validationfalse
- fails validationnew Error('msg')
- fails validation with custom message
req;// ?a => false
Note: You could use a custom validation library like Validator.js for each
and validate
.
var validator = ;req;// ?a => false
Errors
The validator will not throw an error, instead it will set req.validateError
to an error object. If there are no validation errors, req.validateError
will be null
.
req if reqvalidateError return ;
Custom Error Format
You can set a custom error format when initializing the module. See lib/error.js for the default constructor.
{ thismessage = message;} ErrorGeneratorprototype = Errorprototype; app
License
Copyright (c) 2015 Marius Craciunoiu. Licensed under the MIT license.