Skip to content

Repository files navigation

🦋 express-openapi-validator

All ContributorsCoverage StatusCodacy BadgeGitpod Ready-to-Code

An OpenApi validator for ExpressJS that automatically validates APIrequests 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 / custom data serialization-deserialization
  • 🧵 optionally auto-map OpenAPI endpoints to Express handler functions
  • ✂️ $ref support; split specs over multiple files
  • 🎈 file upload

Docs:

GitHub starsTwitter URL

NestJSKoa 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
constOpenApiValidator=require('express-openapi-validator');

or

import*asOpenApiValidatorfrom'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 errorres.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 wiki for complete documenation

License

MIT

Buy Me A Coffee

About

🦋 Auto-validates api requests, responses, and securities using ExpressJS and an OpenAPI 3.x specification

Resources

Code of conduct

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages