如何在 AJV 中使用超模式?

How to use hyper-schema with AJV?

我需要根据使用 hyper-schema 的现有模式实施 json 模式验证器 为此,我尝试利用 ajv 库(版本 6.12.2),这是我的实现:

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

出现以下错误:

<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'
}

或者这个,如果我使用 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)

我也尝试过使用 ajv.addSchema([hyperSchema, linkSchema], undefined, true) 跳过架构验证,但是在导入我的架构时失败并出现以下错误(在验证超级架构时):

<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)

这里如何处理超模式?即使 hyper-schema.json 似乎已加载,它也无法引用 links。与 addSchema 对多个文件的错误使用有关,还是由于 ajv 中的超模式处理?

好心人在与 AJV (https://github.com/ajv-validator/ajv/issues/1236) 关联的 Github 中回答了问题。

该错误是由于架构中的错误定义引起的,而不是在使用 AJV 的方式中。