fatmatto / express-ajv Goto Github PK
View Code? Open in Web Editor NEWExpress middleware that helps validating user input with AJV library
License: MIT License
Express middleware that helps validating user input with AJV library
License: MIT License
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);
}
}
/**
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
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.