allOf项目的属性可以包含对另一个allOf项目定义的$ ref吗? [英] Can a property of an allOf item contain a $ref to another allOf items definition?

查看:87
本文介绍了allOf项目的属性可以包含对另一个allOf项目定义的$ ref吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

"#/allOf/1/properties/body/properties/foo"是有效的$ ref吗?
本文所述,它解释了$ ref的用法,看起来应该是这样.
但是, AJV

Is "#/allOf/1/properties/body/properties/foo" a valid $ref?
As stated in this article which explains $ref usage, it looks like it should be.
However both AJV and https://www.jsonschemavalidator.net/ seem to disagree.

尝试使用简单的JSON指针似乎绝对有可能: https://repl.it/repls/WretchedSpiritedMammoth

Trying with a plain JSON Pointer it definitely seems to be possible: https://repl.it/repls/WretchedSpiritedMammoth

这是我正在测试的架构.

This is the schema I'm testing with.

{
  "$schema": "http://json-schema.org/draft-06/schema#",
  "type": "object",
  "allOf": [
    {
      "$schema": "http://json-schema.org/draft-06/schema#",
      "$id": "http:/example.org/example.schema.json",
      "type": "object",
      "properties": {
        "type": {
          "type": "string"
        },
        "body": {
          "type": "object"
        }
      },
      "required": [
        "type",
        "body"
      ]
    },
    {
      "properties": {
        "body": {
          "$schema": "http://json-schema.org/draft-06/schema#",
          "$id": "http://example.org/foo.schema.json",
          "type": "object",
          "properties": {
            "foo": {
              "type": "number",
              "minimum": 1
            },
            "bar": {
              "$ref": "#/allOf/1/properties/body/properties/foo"
            }
          },
          "required": [
            "foo",
            "bar"
          ]
        }
      }
    }
  ]
}

这些是我得到的错误:

jsonschemavalidator.net

错误解析架构
消息:解析架构引用'#/allOf/1/properties/body/properties/foo'时出错. 路径'allOf[1].properties.body.properties.bar',行..,位置..

Error parsing schema
Message: Error when resolving schema reference '#/allOf/1/properties/body/properties/foo'. Path 'allOf[1].properties.body.properties.bar', line .., position ..

AJV :

无法从ID http://example.org/foo.schema解析引用#/allOf/1/properties/body/properties/foo .json "

can't resolve reference #/allOf/1/properties/body/properties/foo from id http://example.org/foo.schema.json"

推荐答案

尽管您已经在子模式中设置了$id,但是您的架构基本上是正确的.

Your schema is mostly right, although you've set an $id in a subschema.

"$ id"关键字定义模式的URI以及用于 解析模式中的其他URI引用.

The "$id" keyword defines a URI for the schema, and the base URI that other URI references within the schema are resolved against.

https://tools.ietf.org /html/draft-handrews-json-schema-00#section-9.2

通过在子模式中添加$id,您已重置了其他URI引用的基本URI,包括在子模式及其子级中对$ref的任何使用.

By adding an $id to a subschema, you've reset the base URI for other URI references, which includes any use of $ref within the subschema and its children.

$ref

...根据当前URI基础进行解析,它标识了 使用的模式.

...Resolved against the current URI base, it identifies the URI of a schema to use.

https://tools.ietf.org /html/draft-handrews-json-schema-00#section-8

通过将bar的定义更改为以下内容,您的架构将有效.

By changing your definition of bar to the following, your schema will be valid.

"bar": {
  "$ref": "#/properties/foo"
}

根据Relequestual在其后来的评论中的要求进行进一步的

Further edit as Relequestual requested in his later comment:

使用绝对URI作为子模式$ id是有效的,但是如前所述,它会重置基本URI.对$ id值的限制仅在用于创建本地URI片段标识符时才适用(如果这对您没有任何意义,请不要担心).

It is valid to have an absolute URI as subschema $id, but as noted it resets the base URI. The restrictions on $id values only apply when using it to create local URI fragment identifiers (if this does not mean anything to you, do not worry about it).

直接从另一个属性架构中引用属性架构是有效的,但是更常见的最佳实践是将这样的架构放在"definitions"关键字下,并且两个属性都引用该位置.这是我为最大程度地建议的建议:

Referencing a property schema from another property schema directly is valid, but the more common best practice is to put such a schema under the "definitions" keyword and have both properties refer to that location. This is what I would recommend for maximum clarity:

{
  "$schema": "http://json-schema.org/draft-06/schema#",
  "type": "object",
  "allOf": [
    {
      "$schema": "http://json-schema.org/draft-06/schema#",
      "$id": "http:/example.org/example.schema.json",
      "type": "object",
      "properties": {
        "type": {
          "type": "string"
        },
        "body": {
          "type": "object"
        }
      },
      "required": [
        "type",
        "body"
      ]
    },
    {
      "properties": {
        "body": {
          "$schema": "http://json-schema.org/draft-06/schema#",
          "$id": "http://example.org/foo.schema.json",
          "type": "object",
          "properties": {
            "foo": {
              "$ref": "#/definitions/whateverYouWantToCallIt"
            },
            "bar": {
              "$ref": "#/definitions/whateverYouWantToCallIt"
            }
          },
          "required": [
            "foo",
            "bar"
          ]
        }
      },
      "definitions": {
        "whateverYouWantToCallIt": {
          "type": "number",
          "minimum": 1
        }
      }
    }
  ]
}

这篇关于allOf项目的属性可以包含对另一个allOf项目定义的$ ref吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆