express-ajv middleware
Very simple wrapper to use ajv(another json validator) as express' route level middleware. This one allows you to keep validation logic as separate concern. Your controller will be reached only when request data pass tests.
Install
npm install --save express-ajv
Usage
//my-validation-schema.js//first you have to register all yous schemas const expressAjv = ;const schema = expressAjvschema; const jsonSchema1 = ;const jsonSchema2 = ;const customKeyword = ; schema;schema;schema; //to prevent raise condition in your routes use this modulemoduleexports = expressAjvvalidatorFactory;
when we have bootstrapped our validation schema then we can aply validators to our routes
const vf = ;const router = ; router;router;
Please notice, this middleware does not stop middleware chain, instead of this just passes errors through next callback so if you want to send any feedback to the user you need add error handler. You can use defaultErrorHandler for this;
//app.js/* * your app configuration here */ app;