$ref无法从数组类型json架构工作

$ref无法从数组类型json架构工作,json,schema,jsonschema,ajv,Json,Schema,Jsonschema,Ajv,我有三个json模式定义。 客户、地址和联系方式 client.json { "$id": "client.json", "type": "object", "definitions": {}, "$schema": "http://json-schema.org/draft-06/schema#", "properties": { "name": { "$id": "/properties/name", "type": "string"

我有三个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"
    }
  }
}
address.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可以正常工作,但我在从“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

  • 您开始处理
    file:///path/to/schema/client.json
    schema
  • 您会遇到引用
    contact.json
    。这是相对URI,因此您需要确定它相对的URI才能解析它
  • 回溯模式,找到最接近的
    $id
    ,其值为
    client.json
  • 这是一个相对URI,没有更多的
    $id
    s,因此文件的路径为
    file:///path/to/schema/client.json使用了
  • 您现在可以根据
    file:///path/to/schema/client.json
    并获取
    file:///path/to/schema/client.json
  • 您现在可以根据
    file:///path/to/schema/client.json
    并获取
    file://path/to/schema/contact.json
  • 这就是它开始变得奇怪的地方

  • 您检索
    file:///path/to/schema/contact.json
    schema
  • 您会遇到引用
    address.json
    。这是一个相对URI,因此您需要确定它相对的URI才能解析它
  • 回溯架构,找到最接近的
    $id
    ,其值为
    /items
  • 这是一个相对URI,因此您可以回溯并查找
    contact.json
  • 这是一个相对URI,没有更多的
    $id
    s,因此文件的路径为
    file:///path/to/schema/contact.json使用了
  • 现在您可以根据
    file:///path/to/schema/contact.json
    并获取
    file:///items
  • 现在您可以根据
    file:///items
    并获取
    file:///address.json
  • 您试图检索
    file:///address.json
    schema,但它不存在

  • 因为
    $id
    更改了
    $ref
    的解析范围,所以非常不鼓励像在模式中那样为所有内容提供
    $id
    。该特性适用于将多个小型模式组合成一个模式这样的用例。除非您有很好的理由并理解其含义,否则您不应该在文档的根目录下使用它。

    我确信问题在于
    $id
    更改了
    $ref
    的解析范围。我猜,
    $ref
    通过在文件系统中查找文件来实现解析。假设您的三个模式在
    file:///path/to/schema

  • 您开始处理
    file:///path/to/schema/client.json
    schema
  • 您会遇到引用
    contact.json
    。这是相对URI,因此您需要确定它相对的URI才能解析它
  • 回溯模式,找到最接近的
    $id
    ,其值为
    client.json
  • 这是一个相对URI,没有更多的
    $id
    s,因此文件的路径为
    file:///path/to/schema/client.json使用了
  • 您现在可以根据
    file:///path/to/schema/client.json
    并获取
    file:///path/to/schema/client.json
  • 您现在可以根据
    file:///path/to/schema/client.json
    并获取
    file://path/to/schema/contact.json
  • 这就是它开始变得奇怪的地方

  • 您检索
    file:///path/to/schema/contact.json
    schema
  • 您会遇到引用
    address.json
    。这是一个相对URI,因此您需要确定它相对的URI才能解析它
  • 回溯架构,找到最接近的
    $id
    ,其值为
    /items
  • 这是一个相对URI,因此您可以回溯并查找
    contact.json
  • 这是一个相对URI,没有更多的
    $id
    s,因此文件的路径为
    file:///path/to/schema/contact.json使用了
  • 现在您可以根据
    file:///path/to/schema/contact.json
    并获取
    file:///items
  • 现在您可以根据
    file:///items
    并获取
    file:///address.json
  • 您试图检索
    file:///address.json
    schema,但它不存在

  • 因为
    $id
    更改了
    $ref
    的解析范围,所以非常不鼓励像在模式中那样为所有内容提供
    $id
    。该特性适用于将多个小型模式组合成一个模式这样的用例。除非您有很好的理由并理解其含义,否则您绝对不应该在文档的根目录之外使用它。

    我看不出这会给您带来错误的任何原因。你能提供一些代码在哪里设置和使用ajv吗?也许有人能帮你。哦!是@relequestualseem对我有效!我会打电话给EvgenyI,我看不出这会给你一个错误的原因。你能证明吗