【问题标题】:$ref not working from array type json schema$ref 不适用于数组类型 json 模式
【发布时间】:2018-09-03 12:21:39
【问题描述】:

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

client.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"
      }
    }
  }
}

contact.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's 工作正常,但在从 '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);
}

【问题讨论】:

  • 我看不出这会给您带来错误的任何原因。你能提供一些你设置和使用ajv的代码吗?那么有人可能会帮助你。
  • 哦!是的@Relequestual
  • 对我来说似乎有效!我会 ping Evgeny

标签: json schema jsonschema ajv


【解决方案1】:

我确定问题在于$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,没有更多的 $ids,因此使用文件的路径 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/to/schema/contact.json

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

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

因为$id 更改了$ref 的解析范围,所以强烈建议不要像在架构中所做的那样为所有内容提供$id。此功能适用于将多个小模式合并为一个等用例。除非您有充分的理由并理解其中的含义,否则您真的不应该在文档的根目录之外使用它。

【讨论】:

  • 谢谢! @杰森。 $ref 删除项目 $id 的工作。我一直在使用在线模式生成器,所以 $ids 本身就带有模式,并且从未想过删除 $ids 考虑到它们无害。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-20
  • 1970-01-01
相关资源
最近更新 更多