首页 > 解决方案 > 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: {}
}

它显示为无效但没有显示任何错误,我已经研究了很长时间但我还没有找到原因。

提前致谢

标签: jsonjsonschemaajv

解决方案


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

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


推荐阅读