Automatically validate API requests and responses with OpenAPI 3 and Express.
Express-openapi-validator is an essential npm package for developers looking to implement robust API validation within their Express.js applications. This node module leverages the OpenAPI 3 specification to automatically validate incoming requests and outgoing responses, ensuring they adhere to the defined API schema. By integrating express-openapi-validator, developers can dramatically reduce the amount of boilerplate code required for error handling and schema validation. This not only streamlines the development process but also enhances the reliability and consistency of the API, providing a better experience for both developers and end-users.
For developers eager to enhance their Express.js applications with automatic API validation, the command npm install express-openapi-validator is the first step. Installation is straightforward and integrates seamlessly into existing projects. Once installed, express-openapi-validator takes over the task of ensuring that all API requests and responses match the predefined OpenAPI (formerly known as Swagger) specifications. This package supports a wide array of validation scenarios including request body, parameters, and response structures, thereby helping to catch discrepancies and errors early in the development cycle, which can save significant time and resources.
One of the standout features of express-openapi-validator is its ability to provide detailed, context-specific error messages when validation fails. These error messages are invaluable for debugging and can be easily interpreted by developers to quickly pinpoint issues. Additionally, the package is highly configurable, allowing developers to customize the validation process to suit their specific needs, such as customizing error responses or skipping validation under certain conditions. The use of express-openapi-validator not only enforces better coding practices but also ensures that APIs are more secure and performant by adhering strictly to the OpenAPI 3 specifications.
Core dependencies of this npm package and its dev dependencies.
@apidevtools/json-schema-ref-parser, @types/multer, ajv, ajv-draft-04, ajv-formats, content-type, json-schema-traverse, lodash.clonedeep, lodash.get, media-typer, multer, ono, path-to-regexp, @types/cookie-parser, @types/express, @types/mocha, @types/morgan, @types/node, @types/supertest, body-parser, chai, commitizen, cookie-parser, coveralls, express, mocha, morgan, nodemon, nyc, prettier, rimraf, source-map-support, supertest, ts-node, typescript
A README file for the express-openapi-validator code repository. View Code
docs.
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:
Docs:
NestJS Koa and Fastify now available! ๐
npm install express-openapi-validator
## latest beta
npm install express-openapi-validator@4.14.0-beta.1
const OpenApiValidator = require('express-openapi-validator');
or
import * as OpenApiValidator from 'express-openapi-validator';
app.use(
OpenApiValidator.middleware({
apiSpec: './openapi.yaml',
validateRequests: true, // (default)
validateResponses: true, // false by default
}),
);
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