Ajv: Not able to validate draft-04 schema

Created on 3 Dec 2018  路  1Comment  路  Source: ajv-validator/ajv

Ajv version: 6.5.0

Whenever I try to validate draft-04 jsonschema, I get this error:
Error: no schema with key or ref "http://json-schema.org/draft-06/schema#"
at Ajv.validate (ajv.js:92)
at Ajv.validateSchema (ajv.js:178)
at Ajv._addSchema (ajv.js:312)
at Ajv.compile (ajv.js:112)

JSON Schema

{
  "type": "object",
  "title": "Contact",
  "properties": {
    "home": {
      "title": "home",
      "type": "string"
    }
  },
  "$schema": "http://json-schema.org/draft-04/schema#"
}
usage

Most helpful comment

You will need to tell it to use draft 4.

// ...
const Ajv = require('ajv');
// cache the draft 4 json (included in the AJV module).
const JSONSchemaDraft4Definition = require('ajv/lib/refs/json-schema-draft-04.json');
// ...
// create an instance of ajv. note the 'schemaId' property below, it is
// needed for ajv to interpret draft 4 correctly.
const ajv = new Ajv({
    schemaId: 'id', // draft-04 support requirment.
    allErrors: true,
    jsonPointers: true
});
// add the schema to the instance.
ajv.addMetaSchema(JSONSchemaDraft4Definition);
// you can now use the AJV instance to validate against a draft 4 schema.
// ...

Hope this helps!

>All comments

You will need to tell it to use draft 4.

// ...
const Ajv = require('ajv');
// cache the draft 4 json (included in the AJV module).
const JSONSchemaDraft4Definition = require('ajv/lib/refs/json-schema-draft-04.json');
// ...
// create an instance of ajv. note the 'schemaId' property below, it is
// needed for ajv to interpret draft 4 correctly.
const ajv = new Ajv({
    schemaId: 'id', // draft-04 support requirment.
    allErrors: true,
    jsonPointers: true
});
// add the schema to the instance.
ajv.addMetaSchema(JSONSchemaDraft4Definition);
// you can now use the AJV instance to validate against a draft 4 schema.
// ...

Hope this helps!

Was this page helpful?
0 / 5 - 0 ratings