有关使用$ ref的JSON模式 [英] JSON Schema regarding use of $ref

查看:100
本文介绍了有关使用$ ref的JSON模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道$ ref会将URI移至json模式以使用,但是$ ref:#"指向何处? 这是否仅意味着将当前模式用于此块级别?还是使用根级别ID中定义的根级别架构? 谢谢

I understand that $ref takes a URI to a json schema to use but where does $ref : "#" point to? Does it just mean use the current schema for this block level? Or does it mean to use the root level schema defined in the root level id? Thanks

所以,如果我有

"items": {
        "anyOf": [
            { "$ref": "#" },
            { "$ref": "#/definitions/schemaArray" }
        ],
        "default": {}
    }

由于缺少id字段,它将首先尝试使用根架构验证实例项,然后如果失败,请尝试使用定义架构中定义的schemaArray架构对其进行验证,对吧?

Because it lacks an id field it will attempt to validate the instance items with the root schema first and then if that fails try to validate it with the schemaArray schema defined in the definitions schema, right?

因此,如果我将其更改为:

So if I change it to:

 "items": {
            "id" : "#/items",
            "anyOf": [
                { "$ref": "#" },
                { "$ref": "#/definitions/schemaArray" }
            ],
            "default": {}
        }

那么anyOf数组中的第一个子模式将指向项模式本身吗?

Then the first subschema in anyOf array will point to the items schema itself?

编辑#2:好吧,如果我有:

EDIT #2: Okay so if I had:

 "items": {
        "id" : "itemSchema",
        "anyOf": [
            { "$ref": "#" },
            { "$ref": "#/definitions/schemaArray" }
        ],
        "default": {}
    }

"stringArray": {
        "type": "array",
        "items": { "$ref" : "itemSchema" },
        "minItems": 1,
        "uniqueItems": true
    }

"stringArray"的"items"字段是否可以根据上述"itemsSchema"进行验证?

"stringArray"'s "items" field would be validated against the above "itemsSchema"?

"anyOf"中的第二个$ ref是否也可以通过移至根目录,然后遍历该路径直至到达该模式来工作吗? 谢谢!

Also does the second $ref in 'anyOf' work by going to the root and then traversing down the path till it hits that schema? Thanks!

推荐答案

好:每个$ref都解析为完整的URI.完成此操作后,将通过以下问题回答您所有的问题:如果仅获取该URI,我将得到哪种模式? $ref的位置,如何加载,全部其中不相关-它完全取决于解析的URI.

OK: each $ref is resolved into a full URI. Once that is done, all your questions are answered by asking the question: What schema would I end up with, if I simply fetched that URI? Where the $ref is, how it was loaded, all of that is irrelevant - it's entirely dependent on the resolved URI.

可能具有一些快捷方式(例如缓存文档,因此它们仅被提取一次,或者信任一个模式代表"另一个模式),但是这些都是实现细节.

The library might take some shortcuts (like caching documents so they are only fetched once, or trusting one schema to "speak for" another), but those are all implementation details.

#并不特殊:$ref的所有值都被解析为相对于当前文档的URI(如果存在,则为最接近的"id"值).

# is not special: all values of $ref are resolved as URIs relative to the current document (or the closest value of "id", if there is one).

因此,如果您未使用"id",则#将指向架构文档的根目录.如果您从http://example.com/schema获取架构,则其中的{"$ref": "#"} 任意位置都将解析为http://example.com/schema#,即文档本身.

Therefore, if you haven't used "id", then # will point to the root of the schema document. If you fetched your schema from http://example.com/schema, then a {"$ref": "#"} anywhere inside that will resolve to http://example.com/schema#, which is the document itself.

使用"id"时有所不同,因为它更改了解析$ref的基本"模式:

It is different when you use "id", because it changes the "base" schema against which the $ref is resolved:

{
    "type": "array",
    "items": {
        "id": "http://example.com/item-schema",
        "type": "object",
        "additionalProperties": {"$ref": "#"}
    }
}

在该示例中,$ref解析为http://example.com/item-schema#.现在,如果您的JSON模式设置信任已有的模式,则它可以重新使用项目"中的值.

In that example, the $ref resolves to http://example.com/item-schema#. Now, if your JSON Schema setup trusts the schema it already has, then it can re-use the value from "items".

但是,重点是#没有什么特别的-它只是像其他任何一个一样都解析为URI.

However, the point is there is nothing special about # - it just resolves to a URI like any other.

您的第一个示例是正确的.

Your first example is correct.

但是,不幸的是,您的秒数不是.这是因为片段解析适用于URI的方式:一个片段完全替换.当针对#/items"id"值解析#时,您不会再以#/items结尾-是以#结尾.因此,在您的第二个示例中,与第一个示例一样,"anyOf"中的第一个条目仍将解析为文档的根.

However, your second is unfortunately not. This is because of the way that fragments resolution works for URIs: one fragment completely replaces another. When you resolve the # against the "id" value of #/items, you don't end up with #/items again - you end up with #. So in your second example, the first entry in "anyOf" will still resolve to the root of the document, just as in the first example.

假设文档是从http://example.com/my-schema加载的,则两个$ref的完整URI为:

Assuming the document is loaded from http://example.com/my-schema, the full URIs of your two $refs are:

  • http://example.com/itemSchema#
  • http://example.com/itemSchema#/definitions/schemaArray
  • http://example.com/itemSchema#
  • http://example.com/itemSchema#/definitions/schemaArray

对于第一个库,可以使用其已经拥有的架构,但是它可能不会-毕竟,查看URI,可能不信任http://example.com/my-schema来准确表示.

For the first one, the library may use the schema it already has, but it might not - after all, looking at the URIs, http://example.com/my-schema might not be trusted to accurately represent http://example.com/itemSchema.

对于第二个,这是行不通的,因为"itemSchema"没有"definitions"部分,因此$ref根本无法正确解析.

For the second one - that's not going to work, because the "itemSchema" doesn't have a "definitions" section, so that $ref won't resolve properly at all.

这篇关于有关使用$ ref的JSON模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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