Express middleware that leverages your OpenAPI document to automatically validate requests and responses, mock responses in development, and route requests to your controller functions. Version 2.2.0 supports OpenAPI 2.0 and 3.0.x. Requires openapi-enforcer (>=1.2.0) and express (>=4.0.0) as peer dependencies. Compared to alternatives like express-openapi-validator, this middleware enforces both request and response validation, and offers built-in mocking. Ships TypeScript types. Releases are stable with periodic updates.
npm install openapi-enforcer-middlewareVerified import paths — ran on the pinned version, not inferred.
Shows initializing OpenApiEnforcerMiddleware with an OpenAPI file, starting the enforcer, applying middleware, and defining a route that matches an operationId.
Use 'import' syntax or dynamic import().
Use the 'start()' method to resolve the document.
Always await enforcer.start() before using enforcer.middleware().
Add all possible response schemas to your OpenAPI document or disable response validation with option validateResponse: false.
Disable mock mode in production production: set mock: false.
Use import { OpenApiEnforcerMiddleware } from 'openapi-enforcer-middleware' with 'type': 'module' in package.json or run with --experimental-modules.Run npm install openapi-enforcer openapi-enforcer-middleware.