restify-validation-engine
Validation middleware for restify powered by validator.
Get it
This module is available on npm under the name of restify-validation-engine
.
npm install restify-validation-engine
Load it
Then you can load it as a simple middleware.
- You can add your own validators in the
customValidators
key of the options. - You can specify if you want a list of error or just one with
multipleErrors
. - You can change the way the error looks with the
formatter
.
var restify = restifyValidator = ; var server = restify; server;
Use it
Now you can use the module in the definitions of your routes e.g.
server;
You can also add a formatter
to the validate object for a route specific format. This will override the default formatter
server;
In the validate key of the route you can define your fields in the params
or body
key.
Each field can have a set of validators. The validators available are the required
validator,
every validator from the (validator)[https://github.com/chriso/validator.js] module and your custom ones.
A validator can be set to:
- true/false
- a custom error message
- an Error object
- an object with:
msg
a custom error messageparams
when the validator need some
server;
In this example, the filter
param doesn't have a required
validator but have a isIn
validator.
In this case, if the field isn't given, the module will not check the other validators.
isIn
is defined as :
isIn(str, values) - check if the string is in a array of allowed values.
The str
param is provided by restify-validation-engine, but it will need the values param.
You can give this param by putting it in the params
option.
Another example could be:
isLength(str, min [, max]) - check if the string's length falls in a range. Note: this function takes into account surrogate pairs.
In this case, if you want to validate your field being between 4 and 24 characters long you will set the params
options to [4, 24]
.
Love it
Now that your validation engine is in place, you will get a response like this when a field fails the validation:
Now in your middleware, you can be sure that your fields are valid.
Make it better
Please give me some feedback about it. And if you have some idea, make a pull request.