restify-json-schema-validation-middleware
Middleware to validate incoming requests using json schema and respond with restify error. Inspired by json-schema-validation-middleware. Based on tv4 and tv4-formats.
Usage
Partial request validation
const validator = require( 'restify-json-schema-validation-middleware' )(); const headersSchema = { type: 'object' };const bodySchema = { type: 'object' };const querySchema = { type: 'object' };const paramsSchema = { type: 'object' };const filesSchema = { type: 'object' }; server.post( '/path', validator.headers( headersSchema ), validator.body( bodySchema ), validator.query( querySchema ), validator.params( paramsSchema ), validator.files( filesSchema ), function( req, res, next ) { ... });
Full request validation
const validator = require( 'restify-json-schema-validation-middleware' )(); const schema = { type: 'object', properties: { headers: { type: 'object' }, body: { type: 'object' }, query: { type: 'object' }, params: { type: 'object' }, files: { type: 'object' } }, required: [ 'headers' ]}; server.post( '/path', validator( schema ), function( req, res, next ) { ... });
Options
banUnknownProperties
: boolean of whether or not to ban unknown properties
const validator = require( 'restify-json-schema-validation-middleware' )( { banUnknownProperties: true} );