npm install express-openapi-validator

Automatically validate API requests and responses with OpenAPI 3 and Express.

About express-openapi-validator

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.

More from cdimascio

cdimascio npm packages

Find the best node modules for your project.

Search npm

express-openapi-validator

Automatically validate API requests and responses with OpenAPI 3 and Express...

Read more

Dependencies

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

Documentation

A README file for the express-openapi-validator code repository. View Code

๐Ÿฆ‹ express-openapi-validator

docs.

example workflow All Contributors Coverage Status Codacy Badge Gitpod Ready-to-Code

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:

GitHub stars Twitter URL

NestJS Koa and Fastify now available! ๐Ÿš€

Install

npm install express-openapi-validator

## latest beta
npm install express-openapi-validator@4.14.0-beta.1

Usage

  1. Require/import the openapi validator
const OpenApiValidator = require('express-openapi-validator');

or

import * as OpenApiValidator from 'express-openapi-validator';
  1. Install the middleware
app.use(
  OpenApiValidator.middleware({
    apiSpec: './openapi.yaml',
    validateRequests: true, // (default)
    validateResponses: true, // false by default
  }),
);
  1. 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 doc for complete documenation

deprecated legacy doc

License

MIT

Buy Me A Coffee