middleware to check request parameters
middleware to validate parameters
npm install veto
var veto = require'veto';appuseveto;appget'/'// assert that the email parameter is an email// will throw a veto.ValidationError if it isn'tvar email = req.assert'email'isEmailget;// asserts can be chainedvar foo = req.assert'foo'isAlphalen4 10get;// and also accept custom messagesvar baz = req.assert'baz' 'must be a number'isNumericget;// we can be sure email is of email format;// an express error handler will receive the thrown errorappuse// err.statusCode will be 400// err instanceOf veto.ValidationError;
You can add your own things to veto by providing an object to the
ownProperty of the object which is a function will be added.
this in the function will refer to the parameter to assert.
To indicate an error, return a string. Otherwise don't return anything.
var additional =if this !== 'foobar'return 'should equal foobar';;appusevetoadditional;appget'/'var foo = req.assert'foo'isFooBarget;;
Veto asserts throw. This means that the error handler middleware will be called as soon as a veto fails. You can easily handle the error there and send any relevant response back to the user.
appuseveto;appget'/'var foo = req.assert'foo'isEmailget;;// error handler for failed assertionsappuse// veto errors are of instance veto.ValidationError// err.statusCode is 400// err.param is the name of the parameter that failed// err.value is the user specified parameter value// err.message is an error message// you can check specifically for veto errors witherr instanceof vetoValidationError;