首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在AJV中使用超模式?

如何在AJV中使用超模式?
EN

Stack Overflow用户
提问于 2020-06-24 22:58:25
回答 1查看 622关注 0票数 0

我需要基于使用hyper-schema的现有模式实现一个json模式验证器,因此我尝试利用了ajv库(版本6.12.2),下面是我的实现:

代码语言:javascript
复制
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

获取以下错误:

代码语言:javascript
复制
<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]);,也可以选择这个

代码语言:javascript
复制
<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)跳过模式验证,但在导入我的模式时失败,并出现以下错误(在验证超模式时):

代码语言:javascript
复制
<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中的超模式处理?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-25 16:52:02

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62557999

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档