Perform semantic validation on an OpenAPI specification, just like Swagger Editor!
Swagger Editor performs both structural (schema) and semantic (spec) validation of OpenAPI specifications. While swagger-parser performs structural validation, it does not (yet) support semantic validation:
Note: Validating against the OpenAPI 3.0 Specification is not (yet) supported. For now, the validate.spec option is ignored if your API is in OpenAPI 3.0 format.
Once swagger-parser adds support for semantic validation, this package will be deprecated.
This package performs semantic validation by executing the validators from the swagger-editor package. It does not parse OpenAPI specifications or perform structural validation; use swagger-parser for that.
NOTE: This package does not perform structural validation; use swagger-parser for that.
- should have required property 'X'
- should NOT have additional properties
- etc.
- Operations must have unique operationIds
- Path parameter "X" must have the corresponding {X} segment in the "Y" path
- etc.
yarn add openapi-semantic-validator
This package should be used alongside swagger-parser, which provides structural validation:
const SwaggerParser = require('@apidevtools/swagger-parser')
const { validateOpenapiSemantics } = require('openapi-semantic-validator')
// parse and validate structure
const spec = await SwaggerParser.validate('openapi.yml')
// validate semantics
try {
await validateOpenapiSemantics(spec)
log('no semantic errors!')
} catch (err) {
log(`semantic errors: ${JSON.stringify(err.semanticErrors)}`)
}
-
validateOpenapiSemantics(spec)
- Validates an OpenAPI specification
- Inputs:
-
spec
: Parsed OpenAPI specification
-
- Returns:
Promise
- If no semantic errors are found, resolves
- If semantic errors are found, rejects with an
Error
object containing asemanticErrors
property; this is an array of objects containing:level: 'error' | 'warning'
message: string
path: string[]
yarn
yarn build
yarn test
MIT