json - Json 模式验证显示没有错误
问题描述
我正在使用 Ajv 版本 6.10.2 来验证一个简单的 Json 模式,该模式分隔在两个文件中,但问题是即使我用来测试的 json 错误,我在进行验证时也没有收到错误。
这是架构的两个部分:
根.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: {}
}
它显示为无效但没有显示任何错误,我已经研究了很长时间但我还没有找到原因。
提前致谢
解决方案
您的问题是,您没有 $id 的模式http://test.com/schemas/test.json
。您需要将要用于验证的架构的标识符(在您的情况下为完整的 URI)传递到getSchema
.
我可以看到您已经从文档中复制了作为示例显示的 URI。
推荐阅读
- flutter - 当 ChangeNotifiers 触发时,sliverpersistentheader 中的刷新过多
- python - Tornado API 代理和记录响应到 Kafka
- excel-formula - Vlookup 根据最早日期获取相邻值
- javascript - 如果回答错误,如何不停止问题的进展
- c++ - C ++通过有效位查找范围IP地址
- api - docker 在哪里上传 server.py 文件?
- sql - 在查找期间行未产生匹配
- javascript - 流本地文件以使用 d3 绘制地图
- swift - 如何访问通过 iOs 中的活动表共享的 URL
- sql - 根据sql server中的日期范围将单行拆分为多行