【发布时间】:2020-02-25 03:07:36
【问题描述】:
我正在使用 Ajv 版本 6.10.2 来验证一个简单的 Json 架构,该架构分隔在两个文件中,但问题是即使我用来测试的 json 在进行验证时我也没有收到错误错了。
这是架构的两个部分:
root.json
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://test.com/schemas/root.json",
"title": "test",
"description": "test",
"type": "object",
"properties": {
"entrypoint": { "$ref": "entrypoint.json" }
},
"additionalProperties": false,
"required": ["entrypoint"]
}
入口点.json
{
"$id": "http://test.com/schemas/entrypoint.json",
"description": "test object",
"type": "string"
}
我像这样实例化 Ajv
import Ajv from 'ajv';
import root from './root.json';
import entrypoint from './entrypoint.json';
const ajv = new Ajv({
allErrors: true,
schemas: [
test,
entrypoint,
],
});
这是验证调用
const validate = ajv.getSchema('http://test.com/schemas/root.json');
这是用于测试架构的 json
{
entrypoint: '',
incorrect: {}
}
它是无效的,但它没有显示任何错误,我已经研究了很长时间,但我没有找到原因。
提前致谢
【问题讨论】:
-
看起来你正在加载你正在测试的文件作为模式对象
-
你会怎么做?
-
模式定义了 JSON 应该是什么样子——类似于“入口点”。然后根据入口点验证 test.json。从模式列表中删除测试。
-
我想我把你弄糊涂了,test.json 是模式的根,而不是测试对象,我要修复示例。
-
您以正确的方式添加了架构,但我在您的代码中看不到您已将架构加载到变量
test和entrypoint的位置。请更新您的问题以包含所有相关代码。
标签: json jsonschema ajv