如何在 JSON 模式中使用 URL URI 识别子模式?

Eug*_*kov 4 url uri jsonschema

SPEC提供了如何识别模式的下一个示例:

{
    "$id": "http://example.com/root.json",
    "definitions": {
        "B": {
            "$id": "other.json",
        },
    }
}

#/definitions/B   
        http://example.com/other.json
        http://example.com/other.json#
        http://example.com/root.json#/definitions/B 
Run Code Online (Sandbox Code Playgroud)

root.json但是,如果根模式是在路径下定义的,/some/path那么如何识别呢/

{
    "$id": "http://example.com/some/path/root.json",
    "definitions": {
        "B": {
            "$id": "other.json",
        },
    }
}
Run Code Online (Sandbox Code Playgroud)

other.json应该如何鉴别呢?

http://example.com/other.json
Run Code Online (Sandbox Code Playgroud)

或者:

http://example.com/some/path/other.json
Run Code Online (Sandbox Code Playgroud)

SPEC 的哪一部分定义了这一点?

Rel*_*ual 5

模式可以通过给定的任何 URI 来标识,包括 JSON 指针或由“$id”直接给出的 URI。在所有情况下,取消引用“$ref”引用都需要首先根据 RFC 3986 [RFC3986] 将其值解析为针对当前基本 URI 的 URI 引用。(解除引用部分)规范的[http://json-schema.org/latest/json-schema-core.html#rfc.section.8.3.2]。

“基本 URI”在RFC 3986中定义,并在 JSON 架构规范中引用。

它不是很容易理解,因为它非常复杂。在 URL 的情况下,要解析的引用是非哈希片段,基本 URI 是最后一个斜杠之前(但包括)的 URI 部分

(注意:JSON Schema 定义 an 的值$id必须是绝对 URI,没有任何片段。)

因此,具体回答你的问题other.json应该确定为http://example.com/some/path/other.json

如果您尝试在此在线 JSON 模式验证器中使用以下模式,您可以看到它的实际效果...

{
  "$id": "http://example.com/blah/root.json",
  "definitions": {
    "A": {
      "$id": "#foo"
    },
    "B": {
      "$id": "other.json",
      "definitions": {
        "X": {
          "$id": "#bar"
        },
        "Y": {
          "$id": "t/inner.json"
        }
      }
    },
    "C": {
      "$ref": "http://example.com/blah/other.json"
    }
  },
  "properties":{
    "a": { "$ref": "#/definitions/C" }
  }
}  
Run Code Online (Sandbox Code Playgroud)

在“C”中$ref,如果删除/blah,验证器将抱怨它无法再解析引用。