# OpenAPI schema validator
A JSON schema validator for [OpenAPI](https://www.openapis.org/) specifications,
it currently supports:
- [2.0](https://spec.openapis.org/oas/v2.0)
- [3.0.x](https://spec.openapis.org/oas/v3.0.3)
- [3.1.x](https://spec.openapis.org/oas/v3.1.0)
## Install
```bash
npm install @platformatic/openapi-schema-validator
```
## Usage
This module is ESM only, if you need to use commonJS please see below.
```javascript
// ESM
import { Validator } from "@platformatic/openapi-schema-validator";
console.log(Validator.supportedVersions.has("3.1"));
// prints true
const validator = new Validator();
const res = await validator.validate("./petstore.json");
const specification = validator.specification;
// specification now contains a Javascript object containing the specification
if (res.valid) {
console.log("Specification matches schema for version", validator.version);
const schema = validator.resolveRefs();
// schema now contains a Javascript object containing the dereferenced schema
} else {
console.log("Specification does not match Schema");
console.log(res.errors);
}
```
This module can be used in CommonJS code via:
```javascript
// CommonJS
const { Validator } = await (import("@platformatic/openapi-schema-validator"));
```
## API
- [`new Validator(ajvOptions)`](#newValidator)
- [`.validate(specification)`](#validate)
- [`.specification`](#specification)
- [`.version`](#version)
- [`.resolveRefs(options)`](#resolveRefs)
- [`.addSpecRef(subSpecification, uri)`](#addSpecRef)
- [`.validateBundle([specification,subspecification, ...])`](#validateBundle)
- [`Validator.supportedVersions`](#supportedVersions)
### `new Validator(ajvOptions)`
The constructor returns an instance of `Validator`. By passing an ajv options
object it is possible to influence the behavior of the
[AJV schema validator](https://ajv.js.org/). AJV fails to process the openApi
schemas if you set `strict:true` therefore this set to `false` if present. This
is not a bug but a result of the complexity of the openApi JSON schemas.
### `.validate(specification)`
This function tries to validata a specification against the OpenApi schemas.
`specification` can be one of:
- a JSON object
- a JSON object encoded as string
- a YAML string
- a filename
External references are _not_ automatically resolved so you need to inline them
yourself if required e.g by using `.addSpecRef()` The result is an
object:
```
{
valid: ,
errors: // only present if valid is false
}
```
### `.specification`
If the validator managed to extract data from the specification parameter then
the extracted specification is available in this property as javascript object.
E.g. if you supplied a filename of a YAML file and the file was sucessfully read
and its YAML decoded then the contents is here. Even if validation failed.
### `.version`
If validation is succesfull this will return the openApi version found e.g.
("2.0","3.0","3.1). The openApi specification only specifies major/minor
versions as separate schemas. So "3.0.3" results in "3.0".
### `.resolveRefs(options)`
This function tries to resolve all internal references. External references are
_not_ automatically resolved so you need to inline them yourself if required e.g
by using `.addSpecRef()`. By default it will use the last
specification passed to `.validate()` but you can explicity pass a
specification by passing `{specification: