express-openapi-validator:基于 ExpressJS 的 OpenAPI 验证器项目

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

分支121Tags205
文件最后提交记录最后更新时间
1 个月前
2 年前
28 天前
5 个月前
2 个月前
7 个月前
4 年前
5 年前
7 年前
5 年前
6 年前
5 年前
7 年前
5 年前
7 个月前
6 年前
1 个月前
2 年前
1 年前
1 年前
6 年前
2 年前
5 年前
1 个月前
5 个月前
7 年前
2 年前

🦋 express-openapi-validator

build 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.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:

GitHub stars Twitter URL

Express 5 support available in >=v5.5.0!

OAS 3.1 support available in >=v5.4.0!

NestJS Koa and Fastify now available! 🚀

Install

npm install express-openapi-validator

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

项目介绍

🦋 使用 ExpressJS 和 OpenAPI 3.x 规范自动验证 API 请求、响应和安全性【此简介由AI生成】

定制我的领域
181.01 K239访问 GitHub