我需要基于使用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中的超模式处理?
发布于 2020-06-25 16:52:02
一位好心人在与AJV (https://github.com/ajv-validator/ajv/issues/1236)相关的Github中回答了这个问题。
该错误是由模式中的错误定义引起的,而不是使用AJV的方式。
https://stackoverflow.com/questions/62557999
复制相似问题