Registry / web-framework / express-openapi-validate

express-openapi-validate

JSON →
library0.6.1jsnpmunverified

Express middleware for validating request bodies, query parameters, path parameters, and headers against an OpenAPI 3.0 document. Uses Ajv v8 for JSON Schema draft-04/05 validation as required by OpenAPI 3. Supports automatic request matching via validator.match(), optional allowNoMatch for missing routes, and custom Ajv options. Current stable version 0.6.1, released 2022, with infrequent releases. Key differentiator: simple integration with Express, TypeScript types included, support for discriminator keywords and extra OAS keywords. Alternative: express-openapi-validator.

npm install express-openapi-validate
INSTALL
IMPORT
SIG · EXPRESS-OPENAPI-VA
E
express-openapi-validate
web-frameworkjavascriptv0.6.1
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

OpenApiValidator
✓ import { OpenApiValidator } from 'express-openapi-validate'
✗ const OpenApiValidator = require('express-openapi-validate')
Default import not supported; must use named import. CommonJS require works with { OpenApiValidator } destructuring.
OpenApiDocument
✓ import type { OpenApiDocument } from 'express-openapi-validate'
✗ import { OpenApiDocument } from 'express-openapi-validate'
OpenApiDocument is a TypeScript type, only import as type to avoid runtime errors.
validator.validate
✓ import { OpenApiValidator } from 'express-openapi-validate'; const validator = new OpenApiValidator(doc); router.post('/path', validator.validate('post', '/path'))
✗ validator.validate('post','/path')(req,res)
validator.validate returns middleware, not invoked directly.

Express server with OpenAPI validation using validator.validate() and custom Ajv options.

const fs = require('fs'); const express = require('express'); const { OpenApiValidator } = require('express-openapi-validate'); const YAML = require('js-yaml'); const app = express(); app.use(express.json()); const apiDoc = YAML.load(fs.readFileSync('./openapi.yaml', 'utf8')); const validator = new OpenApiValidator(apiDoc, { ajvOptions: { coerceTypes: true }, }); app.post('/echo', validator.validate('post', '/echo'), (req, res) => { res.json({ output: req.body.input }); }); app.use((err, req, res, next) => { res.status(err.statusCode || 500).json({ error: err.message }); }); app.listen(3000);
Debug
Known issues
breakingAjv upgrade from v6 to v8 in v0.6.0: custom Ajv options may require changes per Ajv migration guide.
fix
Review Ajv v8 migration guide (https://ajv.js.org/v6-to-v8-migration.html) for changes in options like $data, formats, keywords.
affects: >=0.6.0
breakingvalidator.match() throws error if no matching route spec found in v0.6.0. Use match({ allowNoMatch: true }) for old behavior.
fix
Pass allowNoMatch option: app.use(validator.match({ allowNoMatch: true }))
affects: >=0.6.0
breakingNode 10 support dropped in v0.6.0.
fix
Upgrade Node.js to v12 or higher.
affects: >=0.6.0
breakingJSON Schema draft-04/05 used since v0.4.0 instead of draft-07. Some valid draft-07 schemas may fail.
fix
Ensure schemas conform to draft-04/05; avoid draft-07-only features like if/then/else.
affects: >=0.4.0
deprecatedNode 6 and 8 support dropped in v0.5.0.
fix
Upgrade Node.js to v10 or higher (v12+ recommended).
affects: >=0.5.0
Errors
Common errors & fixes
SyntaxError: Unexpected token in JSON at position 0
Passing a JavaScript object instead of a JSON string to OpenApiValidator constructor.
fix
Ensure the document is an object (already parsed from JSON/YAML). If using JSON.parse/require, it's already an object.
TypeError: Cannot destructure property 'OpenApiValidator' of ... undefined
Using default import instead of named import.
fix
Use: import { OpenApiValidator } from 'express-openapi-validate'
Error: No matching operation for POST /unknown
Validator.match() cannot find a matching route specification. Throw occurs if allowNoMatch is not set.
fix
Either add the operation to your OpenAPI document or use: validator.match({ allowNoMatch: true })
ValidationError: request.body should have required property 'input'
Request body does not match schema defined in OpenAPI document.
fix
Ensure request body includes all required properties as defined in the OpenAPI schema.
Upgrade
Version history
0.6.1latest on npm
Audit
Dependencies
ajvrequiredUsed internally for JSON Schema validation. v8 used since v0.6.0, breaking changes from v6.
js-yamloptionalOften used to parse OpenAPI YAML documents, though not a direct dependency of this package.
Agent activity
10 hits · last 30 days
node
8
Amazon
1
OpenAI (training)
1
Resources
express-openapi-validate — npm install express-openapi-validate · libregistry