ajv-validator/ajv

no schema with key or ref "http://json-schema.org/draft-04/schema#"

mmc41 opened this issue · 15 comments

mmc41 commented

I have upgraded from ajv 4.x to ajv 5.0 and I now get above errors when validating json files that I generate using the excellent 'typescript-json-schema' module that generate json schema with
"$schema": "http://json-schema.org/draft-04/schema#".

What version of Ajv are you using? Does the issue happen if you use the latest version?
5.0

Ajv options object

allErrors: true, verbose: false, addUsedSchema: false, inlineRefs: false,

JSON Schema

{
  ...
  "$schema": "http://json-schema.org/draft-04/schema#"
}

Validation result, data AFTER validation, error messages

 Error: no schema with key or ref "http://json-schema.org/draft-04/schema#"

What results did you expect?

The schema to validate like it did in ajv 4.x

Note, as my schemas are autogenerated they are a little difficult to change, so it would be great if Ajv 5 could accept them (like Ajv 4).

admvx commented

@mmc41 I had this issue too, but found the migration guide under the 5.0.0 release notes: https://github.com/epoberezkin/ajv/releases/tag/5.0.0

@adamayogo that is correct. @mmc41 this guide explains how to use draft-04 schemas with 5.x.x version. By default it only includes meta-schema for draft-06.

I do not understand why is the property $id with the url to the schema required for the schema? The property $schema is for the schema url, or not?

I fixed the error no schema with key or ref by adding the $id with the url to the json-schema.
Now my files looks like (snippet):

{
  "$id": "http://json-schema.org/draft-04/schema#",
  "$schema": "http://json-schema.org/draft-04/schema#"
}

I need $schema for the editor (VS Code). Why is id required?

@Domvel $schema is URI of the meta-schema, $id is recommended rather than required, please see JSON schema spec

@epoberezkin Why is not possible to use ajv without $id? Only with $schema. I just added the id property to work with.

It is possible to use ajv without $id, it is only needed if you refer to a schema.

I 'm also experiencing the issue described at the top of this thread. It seems Ajv doesn't accept the $schema field at all. Whatever I put there, I get Error: no schema with key or ref "..."

@steph643 draft-04 meta-schema has to be added to Ajv instance - please see top of readme.

@epoberezkin , thanks for your answer.

I use draft-06, not draft-04, but the readme says to call addMetaSchema for draft-06 also. Sorry, I didn't see this, I will give it a try.

Thanks again.

I have the same problem. Is there a solution

Ajv v8 no longer supports draft-04, you need to use v6 or migrate your schemas

image
image

Still don't understand. Can you answer my question? Thank you

I spotted another unexpected side effect:

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

So basically if someone dares to use the HTTPS or to remove the "#" from the url, ajv stops recognizing the schema, even if from the HTTP point of view the schema file is perfectly accessible.

Not only this, but that means that AJV users are forced to use the unsecure HTTP protocol for loading the schemas, which opens another can of worms.

So far I am inclined to believe that the only way to make it work in all cases is use exactly http://json-schema.org/draft-07/schema# which happens to be its own exposed $id.

Ajv v8 no longer supports draft-04, you need to use v6 or migrate your schemas

from https://ajv.js.org/json-schema.html#draft-04

You can use JSON Schema draft-04 schemas with Ajv from v8.5.0 and the additional package ajv-draft-04 (both ajv and ajv-draft-04 should be installed).

What am i supposed to do if i get this:


Run walbo/validate-json@v1.1.0
Validating JSON files
Finding files from manifest.json

Error: no schema with key or ref "http://json-schema.org/draft-06/schema#"
{
  "$id": "http://json-schema.org/draft-06/schema#",
  "$schema": "http://json-schema.org/draft-06/schema#",
  ...