Search code examples
javascriptnode.jsjson-schema-validatorajvjson-hyper-schema

How to use hyper-schema with AJV?


I need to implement a json schema validator based on existing schemas that use hyper-schema To do so I tried to take advantage of the ajv library (version 6.12.2), here is my implementation:

const Ajv = require('ajv');
// Ajv does not allow to import schema by uri, so I download them and put thems in thoses files
// I've download them directly from http://json-schema.org http://json-schema.org/draft-07/hyper-schema# and http://json-schema.org/draft-07/links#
const hyperSchema = require('./schemas/draft07/hyper-schema.json');
const linkSchema = require('./schemas/draft07/links.json');
var ajv = new Ajv();
ajv.addSchema([hyperSchema, linkSchema]); // unsure if I should use addSchema or addMetaSchema, so I tried both without success

Getting the following error:

<path_to_project>\node_modules\ajv\lib\ajv.js:352
    throw e;
    ^
[MissingRefError: can't resolve reference http://json-schema.org/draft-07/links# from id http://json-schema.org/draft-07/hyper-schema#] {        
  message: "can't resolve reference http://json-schema.org/draft-07/links# from id http://json-schema.org/draft-07/hyper-schema#",     
  missingRef: 'http://json-schema.org/draft-07/links',
  missingSchema: 'http://json-schema.org/draft-07/links'
}

Or this one if I use ajv.addSchema([linkSchema, hyperSchema]);

<path_to_project>\node_modules\ajv\lib\ajv.js:93
    if (!v) throw new Error('no schema with key or ref "' + schemaKeyRef + '"');
            ^


Error: no schema with key or ref "http://json-schema.org/draft-07/hyper-schema#"
    at Ajv.validate (<path_to_project>\node_modules\ajv\lib\ajv.js:93:19)
    at Ajv.validateSchema (<path_to_project>\node_modules\ajv\lib\ajv.js:174:20)
    at Ajv._addSchema (<path_to_project>\node_modules\ajv\lib\ajv.js:308:10)
    at Ajv.addSchema (<path_to_project>\node_modules\ajv\lib\ajv.js:137:29)
    at Ajv.addSchema (<path_to_project>\node_modules\ajv\lib\ajv.js:129:46)
    at Object.<anonymous> (<path_to_project>\index.js:6:5)
    at Module._compile (internal/modules/cjs/loader.js:1158:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14)

I've also tried to skip schema validation using ajv.addSchema([hyperSchema, linkSchema], undefined, true) but it then fails when importing my schema with the following error (when validating the hyper-schema):

<path_to_project>\node_modules\ajv\lib\ajv.js:179
    else throw new Error(message);
         ^ 

Error: schema is invalid: data.properties['$ref'] should be object,boolean
    at Ajv.validateSchema (<path_to_project>\node_modules\ajv\lib\ajv.js:179:16)
    at Ajv._addSchema (<path_to_project>\node_modules\ajv\lib\ajv.js:308:10)
    at Ajv.addSchema (<path_to_project>\node_modules\ajv\lib\ajv.js:137:29)
    at Ajv.addSchema (<path_to_project>\node_modules\ajv\lib\ajv.js:129:46)
    at main (<path_to_project>\index.js:33:5)
    at Object.<anonymous> (<path_to_project>\index.js:35:1)
    at Module._compile (internal/modules/cjs/loader.js:1158:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14)

How can I deal with hyper-schema here ? Even when hyper-schema.json seems to be loaded, It can't reference links. Is is related to a wrong usage of addSchema with multiple files, or is it due to hyper-schema handling in ajv ?


Solution

  • A kind person answered the problem in the Github associated to AJV (https://github.com/ajv-validator/ajv/issues/1236).

    The error was caused by a wrong definition in the schema, and not in the way of using AJV.