README
express-validator-helper
Simple express-validator wrapper for easier work with validation error handling.
Installation
$ npm i express-validation-helper -S
// add expressValidator before helper
app.use(expressValidator());
app.use(expressValidatorHelper());
Usage
Just do a check with express-validator as usual. Then instead req.validationErrors(true)
call req.validate()
. This method returns a Validator
object.
Validator object
add(fieldName, msg)
Add error with msg
to fieldName
.
isValid()
Returns true
if all data is valid. Otherwise false
.
errors
Object with all validation errors. It should be used only if the isValid
method returns false
.
Values contain an array with a list of errors. Example:
{
email: ['required', 'invalid format', 'already taken'],
password: ['required']
}
Customization
You can pass name
option to middleware with a custom method name:
app.use(expressValidatorHelper({
name: 'doValidate'
});
// ...
var validator = req.doValidate();
Before & After
Confirmation handler example.
Before
exports.create = function(req, res, next) {
// checks with reverse order
req.assert('email', 'invalid format').isEmail();
req.assert('email', 'required').notEmpty();
var errors = req.validationErrors(true);
var email = req.body.email;
return User.findOne({
email: new RegExp('^' + email + '