An express.js middleware for node-validator.
This is basically a copy of a gist by node-validator author chriso.
npm install express-validator
var util = require('util'),
express = require('express'),
expressValidator = require('express-validator'),
app = express.createServer();
app.use(express.bodyParser());
app.use(expressValidator);
app.post('/:urlparam', function(req, res) {
req.assert('postparam', 'Invalid postparam').notEmpty().isInt();
req.assert('getparam', 'Invalid getparam').isInt();
req.assert('urlparam', 'Invalid urlparam').isAlpha();
req.sanitize('postparam').toBoolean();
var errors = req.validationErrors();
if (errors) {
res.send('There have been validation errors: ' + util.inspect(errors), 500);
return;
}
res.json({
urlparam: req.param('urlparam'),
getparam: req.param('getparam'),
postparam: req.param('postparam')
});
});
app.listen(8888);
Which will result in:
$ curl -d 'postparam=1' http://localhost:8888/test?getparam=1
{"urlparam":"test","getparam":"1","postparam":true}
$ curl -d 'postparam=1' http://localhost:8888/t1est?getparam=1
There have been validation errors: [
{ param: 'urlparam', msg: 'Invalid urlparam', value: 't1est' } ]
$ curl -d 'postparam=1' http://localhost:8888/t1est?getparam=1ab
There have been validation errors: [
{ param: 'getparam', msg: 'Invalid getparam', value: '1ab' },
{ param: 'urlparam', msg: 'Invalid urlparam', value: 't1est' } ]
You can extend the Validator
and Filter
objects to add custom validation
and sanitization methods:
var expressValidator = require('express-validator');
expressValidator.Filter.prototype.toLowerCase = function(){
this.modify(this.str.toLowerCase());
return this.str;
};
You have two choices on how to get the validation errors:
req.assert('email', 'required').notEmpty();
req.assert('email', 'valid email required').isEmail();
req.assert('password', '6 to 20 characters required').len(6, 20);
var errors = req.validationErrors();
var mappedErrors = req.validationErrors(true);
errors:
[
{param: "email", msg: "required", value: "<received input>"},
{param: "email", msg: "valid email required", value: "<received input>"},
{param: "password", msg: "6 to 20 characters required", value: "<received input>"}
]
mappedErrors:
{
email: {
param: "email",
msg: "valid email required",
value: "<received input>"
},
password: {
param: "password",
msg: "6 to 20 characters required",
value: "<received input>"
}
}
Example:
<input name="user[fields][email]" />
Provide an array instead of a string:
req.assert(['user', 'fields', 'email'], 'valid email required').isEmail();
var errors = req.validationErrors();
console.log(errors);
Output:
[
{
param: "user_fields_email",
msg: "valid email required",
value: "<received input>"
}
]
Alternatively you can use dot-notation to specify nested fields to be checked:
req.assert(['user', 'fields', 'email'], 'valid email required').isEmail();
Express allows you to define regex routes like:
app.get(/\/test(\d+)/, function() {});
You can validate the extracted matches like this:
req.assert(0, 'Not a three-digit integer.').len(3, 3).isInt();
req.validationErrors()
now returnsnull
instead offalse
if there are no errors.
- Support for regex routes (@Cecchi)
- Fix checkHeader() (@pimguilherme)
- Add dot-notation for nested input (@sharonjl)
- Add validate() alias for check()
- Fix chaining validators (@rapee)
- Added
validationErrors()
method (by @orfaust) - Added support for nested form fields (by @orfaust)
- Added test cases
- Readme update
- Expose Filter and Validator instances to allow adding custom methods
- Use req.param() method to get parameter values instead of accessing req.params directly.
- Remove req.mixinParams() method.
- Initial release
- Christoph Tavan dev@tavan.de - Wrap the gist in an npm package
- @orfaust - Add
validationErrors()
and nested field support
Copyright (c) 2010 Chris O'Hara cohara87@gmail.com, MIT License