Restify request validator

Simple restify middleware which adds validation for all incoming requests.
Use in project
Installation
To install the validator for use in your project, go to your project's main directory, then run the following command:
npm install --production --save @ssense/restify-request-validator
Usage
To add the middleware to your current restify project, follow the snippet below:
Initialization
For javascript project
var restifyValidation = require('@ssense/restify-request-validator');
...
var server = restify.createServer();
...
var validator = new restifyValidation.RequestValidator();
server.use(validator.validate.bind(validator));
By default, on each validation error, the RequestValidator
will throw an Error
object with a 500
HTTP code.
You can throw a specific restify error and http code, by passing it to the constructor:
var validator = new restifyValidation.RequestValidator(restify.BadRequestError);
With this configuration, on each validation error, the RequestValidator
will throw a BadRequestError
with a 400
HTTP code.
For typescript project
import {RequestValidator} from '@ssense/restify-request-validator';
...
const server = restify.createServer();
...
const validator = new RequestValidator();
server.use(validator.validate.bind(validator));
Like for javascript initialization, you can pass a restify error handler in the constructor:
const validator = new RequestValidator(restify.BadRequestError);
Validation
Example usage
Just add a validation
param to the route to validate:
function respond(req, res, next) {
res.send('hello ' + req.params.name);
next();
}
server.get('/hello/:name', respond);
server.get({
url: '/hello/:name',
validation: {
url: {
name: {type: 'string', required: true, min: 3},
},
},
}, respond);
With this validation, the name
param must be a string with minimum length of 3.
Validation object
The validation
object accepts 3 optional properties:
- url to validate parameters in url path
- query to validate url query parameters
- body to validate request body parameters (useful for POST or PUT requests)
To validate inputs, just add properties to url
, query
, and/or body
params with the following syntax :
validation: {
<url/query/body>: {
<property_name>: {
type: 'valid type',
required: true|false,
min: 1,
max: 5,
length: 3,
arrayType: 'valid type',
values: ['value1', 'value2'],
regex: /^Valid regex$/,
terminal: true|false|['type', 'required', 'and so on...']
format: function(data): data
}
}
}
If you need to constrain the payload of the url
, query
, or body
to the properties being validated, a parameter disallowExtraFields can be defined with the following syntax:
validation: {
<url/query/body>: {
<property_name>: {
},
disallowExtraFields: true|false
}
}
Formatting data
Auto formatting
By default, every numeric
, date
and boolean
inputs will be automatically transformed respectively to valid number
, Date
and boolean
objects after validation. So you can directly use valid objects in your code.
If you need to add an extra formatting for one or several fields, just add the format
parameter into your validation
object. This parameter is a function with the following profile:
format<T>(value: T): T;
Let's take an example:
For a given route, let's say we want to transform one parameter called language
to lower case, it could be performed as this:
server.get({
url: '/posts/:language',
validation: {
url: {
language: {type: 'string', required: true, format: function(value) { return value.toLowerCase(); }},
language: {type: 'string', required: true, format: (value: string) => value.toLowerCase()},
},
},
}, respond);
With this parameter, if the validation succeeds, the respond
method will always have a lower case language
parameter, whatever the original input is.
Custom error messages
Example usage
You can pass custom error messages via the validationMessages
param of the route:
function respond(req, res, next) {
res.send('hello ' + req.params.name);
next();
}
server.get({
url: '/hello/:name',
validation: {
url: {
name: {type: 'string', required: true, min: 3},
},
},
validationMessages: {
name: {
type: 'The name must be a string',
required: 'The name is required',
min: 'The name must have a minimum length of 3 characters'
}
},
}, respond);
Behavior
By default the validator will throw an error on the first validation failure, if you want to change this behavior to get all the validation errors, you can call the disableFailOnFirstError
method on your validator
object:
For javascript project
var restifyValidation = require('@ssense/restify-request-validator');
...
var server = restify.createServer();
...
var validator = new restifyValidation.RequestValidator();
validator.disableFailOnFirstError();
server.use(validator.validate.bind(validator));
For typescript project
import {RequestValidator} from '@ssense/restify-request-validator';
...
const server = restify.createServer();
...
const validator = new RequestValidator();
validator.disableFailOnFirstError();
server.use(validator.validate.bind(validator));
It is still possible to determine which errors should be returned individually when disableFailOnFirstError
is activated. In order to do so, one should define the parameter terminal
on each property being validated.
Development
Installation
To install the validator and get a proper development environment, clone/fork the current repository, then run the following command:
npm install
Running the tests
- To check coding style (linting), run
npm run lint
- To run the tests for the project, run
npm t
- To run the tests with code coverage, run
npm run cover
, the result will be available in the folder <your_project_dir>/tests/coverage/index.html
Deployment
Before each commit to this project, don't forget to build javascript files by running the following command:
npm run compile
Authors
- Rémy Jeancolas - Initial work - RemyJeancolas
- Mickael Burguet - Add
disableFailOnFirstError
behavior - rundef - Darlinton Prauchner - Add
disallowExtraFields
constraint - DarlintonPrauchner
License
This project is licensed under the MIT License - see the LICENSE.md file for details.