如何在 JSON 架构中识别具有 URL URI 的子架构?

How to identify subschemas with a URL URI in JSON Schema?

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 

但是,如果根模式 root.json 定义在 /some/path 而不是 / 路径下,如何识别?

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

如何other.json识别?

http://example.com/other.json

或:

http://example.com/some/path/other.json

SPEC 的哪一部分对此进行了定义?

Schemas can be identified by any URI that has been given to them, including a JSON Pointer or their URI given directly by "$id". In all cases, dereferencing a "$ref" reference involves first resolving its value as a URI reference against the current base URI per RFC 3986 [RFC3986]. (Dereferencing section)[http://json-schema.org/latest/json-schema-core.html#rfc.section.8.3.2] of the spec.

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

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

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

所以具体回答你的问题other.json应该识别为http://example.com/some/path/other.json.

如果您尝试在此 online JSON Schema validator...

中使用以下架构,您可以看到实际效果
{
  "$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" }
  }
}  

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