This library contains an Open API v3.0 validator that validates http requests and responses.
- Early version: Bugs most likely exist. Bug fixes welcome and more test cases very welcome, too. :-)
- Only supports v3.0 of the Open API specifications (v3.1 has not been tested, contributions welcome)
- Only supports content of type
application/json
(and notmultipart/form-data
for example) - Does not support external schemas (inline schemas are supported)
- Does not (yet?) validate headers
- Does not (really) validate path params, but supports them in the definition and request route
- Does not support references to properties (e.g.
$ref: '#/components/schemas/Test1/properties/bar/allOf/0/properties/baz'
) - Does not support
readOnly
orwriteOnly
. - The default config sets AJV's
removeAdditional
tofalse
, otherwiseallOf
validation may cause unexpected results - The default config sets AJV's
coerceTypes
tofalse
, otherwiseanyof
validation may cause unexpected results- Query params are usually string values on the other hand, so this library coerces those by default prior to validation
- This library does not validate the Open API specification itself. This might be added in future.
To check out what is supported, take a look at the test fixtures
Because the Open API specification can come in different flavors and from different sources, loading the specification file is not in scope
of this library. To load a YAML based spec, you can, for example, use js-yaml
as follows:
import * as fs from 'fs'
import * as yaml from 'js-yaml'
const openApiContent = fs.readFileSync('openapi.yaml', 'utf8')
const openApiSpec = yaml.load(openApiContent)
Once you've loaded the specification, create an instance of AJV (for example by using the factory) and the validator.
import { AjvOpenApiValidator, createAjvInstance } from '@restfulhead/ajv-openapi-request-response-validator'
const ajv = createAjvInstance()
const validator = new AjvOpenApiValidator(spec, ajv)
You can then use the different validation functions such as validateQueryParams
, validateRequestBody
and validateResponseBody
.
For examples, refer to the unit tests.
The scripts and documentation in this project are released under the MIT License
Some of the validation test cases are based on the tests from openapi-request-validator by Kogo Software LLC released under MIT.