An OpenApi validator for ExpressJS that automatically validates API requests and responses using an OpenAPI 3 specification.
馃express-openapi-validator is an unopinionated library that integrates with new and existing API applications. express-openapi-validator lets you write code the way you want; it does not impose any coding convention or project layout. Simply, install the validator onto your express app, point it to your OpenAPI 3 specification, then define and implement routes the way you prefer. See an example.
Features:
- 鉁旓笍 request validation
- 鉁旓笍 response validation (json only)
- 馃懏 security validation / custom security functions
- 馃懡 3rd party / custom formats
- 馃У optionally auto-map OpenAPI endpoints to Express handler functions
- 鉁傦笍 $ref support; split specs over multiple files
- 馃巿 file upload
npm install express-openapi-validator
- Require/import the openapi validator
const OpenApiValidator = require('express-openapi-validator');
- Install the middleware
app.use(
OpenApiValidator.middleware({
apiSpec: './openapi.yaml',
validateRequests: true, // (default)
validateResponses: true, // false by default
}),
);
- Register an error handler
app.use((err, req, res, next) => {
// format error
res.status(err.status || 500).json({
message: err.message,
errors: err.errors,
});
});
Important: Ensure express is configured with all relevant body parsers. Body parser middleware functions must be specified prior to any validated routes. See an example.
See the full documentation
MIT