🦋 express-openapi-validator
An OpenApi validator for ExpressJS that automatically validates API requests and responses using an OpenAPI 3 specification.
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
Install
npm install express-openapi-validator
Usage
- 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.
Documentation
See the full documentation
License
MIT