It would be great to allow adding new formatters like the following
https://ajv.js.org/packages/ajv-formats.html
I think the change could be similar to this.
// --------------------------------------------------------------------------------------
const extensions = new Set();
const formatter = []
const addExtension = (extension) => {
if( extensions.has(extension) ) {
// throw error if extension isn't found
formatter.push(require(extension));
extensions.add(extension);
}
}
/**
- Returns a middleware with compiled ajv validators
- @param {Object} routeSchema An object with ajv schemas {body: Schema1, query: Schema2, prams: Schema3}
*/
const validateRoute = routeSchema => {
let ajv = null;
const validators = {}
// Compiling query schema beforehand
if (Object.prototype.hasOwnProperty.call(routeSchema, 'query')) {
ajv = new Ajv()
formatter.forEach( (module) => require(module)(ajv));
validators.query = ajv.compile(routeSchema.query)
}
// Compiling params schema beforehand
if (Object.prototype.hasOwnProperty.call(routeSchema, 'params')) {
// We coerce types on params because they can only be strings
// since they are part of an url
ajv = new Ajv({ coerceTypes: true })
formatter.forEach( (module) => require(module)(ajv));
validators.params = ajv.compile(routeSchema.params)
}
// Compiling body schema beforehand
if (Object.prototype.hasOwnProperty.call(routeSchema, 'body')) {
ajv = new Ajv()
formatter.forEach( (module) => require(module)(ajv));
validators.body = ajv.compile(routeSchema.body)
}
// The actual middleware that gets loaded by express
// has already-compiled validators
return (req, res, next) => {
let validation = null
if (Object.prototype.hasOwnProperty.call(validators, 'params')) {
validation = ajv.validate(routeSchema.params, req.params)
if (!validation) {
return next(new Errors.BadRequest(`Request url parameters validation failed: ${ajv.errorsText()}`))
}
}
if (Object.prototype.hasOwnProperty.call(validators, 'query')) {
validation = ajv.validate(routeSchema.query, req.query)
if (!validation) {
return next(new Errors.BadRequest(`Request query validation failed: ${ajv.errorsText()}`))
}
}
if (Object.prototype.hasOwnProperty.call(validators, 'body')) {
validation = ajv.validate(routeSchema.body, req.body)
if (!validation) {
return next(new Errors.BadRequest(`Request body validation failed: ${ajv.errorsText()}`))
}
}
return next()
}
}
module.exports = {
validateRoute: validateRoute,
addExtension: addExtension
}