I am getting an error when using Ajv to load a schema that uses $ref imported from a yaml file inside an OpenAPI description.
This is my yaml file:
openapi: 3.0.3
info:
title: Demo
version: 1.0.0
paths:
/carousel:
get:
responses:
"200":
description: Successful operation
content:
application/json:
schema:
type: array
items:
$ref: "#/components/schemas/test"
components:
schemas:
other:
type: object
properties:
component:
type: object
test:
type: object
properties:
slides:
type: array
items:
type: object
properties:
contents:
type: array
items:
anyOf:
- $ref: "#/components/schemas/other"
This is my code (JS + Vite)
import Ajv from 'ajv'
import YamlContent from '../Config/API.yaml'; //vite.config.js with package @modyfi/vite-plugin-yaml
const validate =
new Ajv({
schemas: YamlContent.components.schemas
}).
getSchema(YamlContent.components.schemas.test);
I also tried:
const validate =
new Ajv().
addSchema(YamlContent.components.schemas.other).
compile(YamlContent.components.schemas.test);
But it always gives the same error. What am i missing here? Thanks.
no reason to use
anyOffor a single schema, just pass that schema as the referenceAPI.yaml
OpenAPI 3.0.x requires the use of
ajv-draft-04and theajv-formatspackages.Because you are using a yaml file, you need a way to load it with
js-yamlpackage.json
We use
strict:falseto make sure the ajv validator abides by the JSON Schema specification correctly.The idea is to use the
addSchema()method to add the entire OpenAPI description; then use thevalidatefunction and provide an object schema with a$refto the component schema you want to validate against in the first argument and pass the data instance as the second argument. Then output the results.index.js