Automatically validate API requests and responses with OpenAPI 3 and Express.
npm install express-openapi-validator         
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.0.x or 3.1.x 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 / custom data serialization-deserialization
- ๐งต optionally auto-map OpenAPI endpoints to Express handler functions
- โ๏ธ \$ref support; split specs over multiple files
- ๐ file upload
- โ๏ธ OpenAPI 3.0.x and 3.1.x spec support
- โจ Express 4 and 5 support
Docs:
- ๐ documentation
 
Express 5 support available in >=v5.5.0!
OAS 3.1 support available in >=v5.4.0!
NestJS
Koa and Fastify now available! ๐
``shell`
npm install express-openapi-validator
1. Require/import the openapi validator
`javascript`
const OpenApiValidator = require('express-openapi-validator');
or
`javascript`
import * as OpenApiValidator from 'express-openapi-validator';
2. Install the middleware
`javascript`
app.use(
OpenApiValidator.middleware({
apiSpec: './openapi.yaml',
validateRequests: true, // (default)
validateResponses: true, // false by default
}),
);
3. Register an error handler
`javascript``
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 doc for complete documenation
_deprecated_ legacy doc