Json模式验证未显示任何错误

时间:2019-10-29 20:19:59

标签: json jsonschema ajv

我正在使用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"]
}

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: {}
}

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

预先感谢

1 个答案:

答案 0 :(得分:0)

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

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