7

I am using ajv 8.6.2

I have schemas that validate against draft-07 using the default export

When I use the draft-09 export all of my schemas give me this error:

no schema with key or ref "https://json-schema.org/draft-07/schema"

Same error exists if I use this:

no schema with key or ref "https://json-schema.org/draft-09/schema"

Can't seem to figure out what's going on here?

Adam Jenkins
  • 51,445
  • 11
  • 72
  • 100

3 Answers3

6

If you mean by

When I use the draft-09 export all

that you're using Ajv2019 instance

import Ajv2019 from "ajv/dist/2019"
const ajv = new Ajv2019()

then you need to add the draft-07 metaschema:

const draft7MetaSchema = require("ajv/dist/refs/json-schema-draft-07.json")
ajv.addMetaSchema(draft7MetaSchema)

as explained at https://ajv.js.org/guide/schema-language.html#draft-2019-09-and-draft-2020-12

FYI, don't refer to JSON meta schemas with https://, their identifiers do start with http://, ajv doesn't retrieve them, it packages them instead.

Zdenek F
  • 1,649
  • 1
  • 15
  • 27
5

AJV does not seem to support HTTPs versions of the json-schema identifiers; try using http: instead:

{
    "$schema": "http://json-schema.org/draft-07/schema"
}
Yossarian21
  • 149
  • 1
  • 4
0

The reason you are receiving no schema with key or ref is because JSON Schema $id keyword is merely an identifier, it's not expected to resolve to any network location. That's not to say it isn't possible to resolve, only that the spec indicates it's not expected and the behavior is only an identifier. The JSON Schema specification expects the implementation to "load" all schemas prior to validation. This is typically some sort of registry in memory at runtime. So, ajv doesn't recognize the identifier provided because it doesn't match any known schema to the implementation, unless you were to add it manually with the addSchema method.

Jeremy Fiel
  • 140
  • 10