【问题标题】:Json schema validation shows no errorsJson 模式验证显示没有错误
【发布时间】: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 是模式的根,而不是测试对象,我要修复示例。
  • 您以正确的方式添加了架构,但我在您的代码中看不到您已将架构加载到变量testentrypoint 的位置。请更新您的问题以包含所有相关代码。

标签: json jsonschema ajv


【解决方案1】:

您的问题是,您没有 $id 为http://test.com/schemas/test.json 的架构。您需要将要用于验证的架构的标识符(在您的情况下为完整的 URI)传递到 getSchema

我可以看到您已经从文档中复制了作为示例显示的 URI。

【讨论】:

  • 这假设您已正确导入 JSON。我已对您的问题添加了评论。
猜你喜欢
  • 2018-10-22
  • 2015-06-27
  • 2017-08-02
  • 1970-01-01
  • 2016-08-06
  • 1970-01-01
  • 2013-12-17
  • 2023-03-13
  • 1970-01-01
相关资源
最近更新 更多