【发布时间】:2020-06-24 14:58:24
【问题描述】:
我需要基于使用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 中的超模式处理?
【问题讨论】:
标签: javascript node.js json-schema-validator ajv json-hyper-schema