json - $ref 不适用于数组类型 json 模式

标签 json schema jsonschema ajv

我有三个 json-schema 定义。 客户、地址和联系方式。

客户端.json

{
  "$id": "client.json",
  "type": "object",
  "definitions": {},
  "$schema": "http://json-schema.org/draft-06/schema#",
  "properties": {
    "name": {
      "$id": "/properties/name",
      "type": "string"
    },
    "id": {
      "$id": "/properties/id",
      "type": "integer"
    },
    "contact": {
            "$ref": "contact.json"
    },
    "address": {
        "$ref": "address.json"
    }
  }
}

地址.json

{
  "$id": "address.json",
  "type": "array",
  "definitions": {},
  "$schema": "http://json-schema.org/draft-06/schema#",
  "items": {
    "$id": "/items",
    "type": "object",
    "properties": {
      "addressId": {
        "$id": "/items/properties/addressId",
        "type": "integer"
      },
      "addressName": {
        "$id": "/items/properties/addressName",
        "type": "string"
      }
    }
  }
}

联系人.json

{
  "$id": "contact.json",
  "type": "array",
  "definitions": {},
  "$schema": "http://json-schema.org/draft-06/schema#",
  "items": {
    "$id": "/items",
    "type": "object",
    "properties": {
      "contactId": {
        "$id": "/items/properties/contactId",
        "type": "integer"
      },
      "contactName": {
        "$id": "/items/properties/contactName",
        "type": "string"
      },
      "address": {
          "$ref": "address.json"
      }
    }
  }
}

待验证对象

var client = {
    "name": "test",
    "id": 12,
    "contact": [
        {
        "contactId": 12212,
        "contactName": "jon",
        "address": [
            {
                "addressId": 64,
                "addressName": "pi"
            }
        ]
    }
    ],
    "address": [
        {"addressId": 4242,
        "addressName": "doe"}
    ]
};

“client.json”中的 $ref 工作正常,但在从“contact.json”中引用“address.json”时出现错误。 我在“additionalItems”中使用 $refs 时没有遇到任何错误,但无法根据 $ref 指向的模式进行验证。

我想知道如何使用数组类型模式定义中的 $ref。 另外,我使用 AJV 进行模式验证。

编辑 1: AJV 设置

var Ajv = require('ajv');
var ajv = new Ajv({
    $data: true,
    allErrors: true,
    useDefaults: true, 
    coerceTypes: true, 
});

ajv.addSchema(client);
ajv.addSchema(contact);
ajv.addSchema(address);

let valid = ajv.validate('client.json', payload);

if(!valid){
    console.log(ajv.errors);
}

最佳答案

我确定问题是 $id 更改了 $ref 的解析范围。我猜测 $ref 解析是通过在文件系统上查找文件来实现的。假设您的三个架构在 file:///path/to/schema 中可用。

  1. 您开始处理 file:///path/to/schema/client.json 模式。
  2. 您遇到了引用 contact.json。这是相对 URI,因此您需要确定它相对的 URI 才能解析它。
  3. 您回溯架构并找到最近的 $id,其值为 client.json
  4. 这是一个相对 URI,没有更多的 $id,所以文件的路径,file:///path/to/schema/client.json被使用。
  5. 您现在可以根据 file:///path/to/schema/client.json 解析 client.json 并获取 file:///path/to/schema/client.json.
  6. 您现在可以根据 file:///path/to/schema/client.json 解析 contact.json 并获取 file://path/到/schema/contact.json.

这就是它开始变得奇怪的地方。

  1. 您检索 file:///path/to/schema/contact.json 架构。
  2. 您遇到了引用 address.json。这是一个相对 URI,因此您需要确定它相对的 URI 才能解析它。
  3. 您回溯架构并找到最近的 $id,其值为 /items
  4. 这是一个相对 URI,因此您不断回溯并找到 contact.json
  5. 这是一个相对 URI,没有更多的 $id,所以文件的路径 file:///path/to/schema/contact.json被使用。
  6. 现在您可以根据 file:///path/to/schema/contact.json 解析 /items 并获得 file:///items
  7. 现在您可以针对 file:///items 解析 address.json 并获得 file:///address.json
  8. 您尝试检索 file:///address.json 架构,但它不存在。

因为 $id 改变了 $ref 的解析范围,所以强烈建议不要像在模式中那样给所有东西一个 $id .此功能适用于将多个小模式组合成一个这样的用例。除了在文档的根目录之外,你真的不应该使用它,除非你有充分的理由并理解其中的含义。

关于json - $ref 不适用于数组类型 json 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49473116/

相关文章:

json - 使用 ajv 验证 json 模式时获取错误类型/值

java - 如何用GSON解析变量类型的json?

java - JSON 模式验证 - Java POJO 与单独的 .json 文件

ios - 如何从字符串中的json获取数组响应?

mongodb - MongoError : The dotted field .。对存储无效

Json 架构未验证必需的属性

javascript - Angular-Schema-Form 在 SCHEMA 更改时更改 FORM

ruby-on-rails - rails : :save returns NoMethodError

php - 每月租金发票和付款数据库架构

mysql - 如果其中一个字段已经是唯一的,我可以添加一个复合唯一键吗