1

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

客户端.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'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);
}
4

1 回答 1

4

我确定问题是$id改变了$ref. 我猜想$ref通过在文件系统上查找文件来解决问题。假设您的三个模式在file:///path/to/schema.

  1. 您开始处理file:///path/to/schema/client.json架构。
  2. 你遇到的参考contact.json。这是相对 URI,因此您需要确定它相对的 URI 才能解析它。
  3. 您回溯模式并找到最接近的模式$idvalue client.json
  4. 这是一个相对 URI,没有更多$id的 s,因此使用文件的路径file:///path/to/schema/client.json
  5. 您现在可以解决client.json反对file:///path/to/schema/client.json并得到file:///path/to/schema/client.json.
  6. 您现在可以解决contact.json反对file:///path/to/schema/client.json并得到file://path/to/schema/contact.json.

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

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

由于$id更改了 的解析范围$ref,因此非常不鼓励将$id您在架构中所做的一切都提供给您。此功能适用于将多个小模式合并为一个等用例。除非您有充分的理由并了解其含义,否则您真的不应该在文档的根目录之外使用它。

于 2018-03-26T22:43:25.173 回答