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 restify-request-validator
Usage
To add the middleware to your current restify project, follow the snippet below:
Initialization
For javascript project
var restifyValidation = require('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 '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$/,
}
}
}
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
License
This project is licensed under the MIT License - see the LICENSE.md file for details.