Each App
manages its own Ajv validator instance which you can receive by App#getValidator
.Ajv
constructor options may be propvided through options.validator
option of App
constructor.
Validation & Sanitization as well as Filtering of request / response data can be defined with Route#validate
and Route#respondsWith
methods respectively.
const route = router.buildRoute({
type: 'get',
url : '/',
summary: 'Get users',
});
route.respondsWith({
type: 'array',
additionalProperties: false,
items: {
type: 'object',
additionalProperties: false,
properties: {
id : {type : 'integer'},
email : {type : 'string'}
}
}
});
route.validate({
additionalProperties: false, // will remove any other query parameters
properties: {
limit: {type: 'integer'},
offset: {type: 'integer'},
}
}, 'query'); // possible targets: query | body | params | headers
route.main(function(req, res) {
return fetchUsersInSomeWay(req.query).then(function(users) {
// The filter method will make sure the response body has only
// those data properties which are defined in `respondsWith` schema
res.filter(users).json();
});
});