【问题标题】:How to identify subschemas with a URL URI in JSON Schema?如何在 JSON Schema 中使用 URL URI 识别子模式?
【发布时间】:2018-04-17 08:06:13
【问题描述】:

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 的哪一部分对此进行了定义?

【问题讨论】:

  • 您的问题能具体一点吗?不清楚你在问什么。
  • @Relequestual:规范提供了root.json 定义在/ 路径下的示例。如果在/some/path 路径下定义了root.json,那么正确的基础http://example.com/http://example.com/some/path 是什么?
  • 对不起,我还是不清楚你在问什么。你能修改你的问题以显示一个完整的例子吗?我不明白你所说的“定义在”是什么意思,因为它有点模棱两可。
  • @Relequestual: 完成

标签: url uri jsonschema


【解决方案1】:

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

“基本 URI”在 JSON Schema 规范中引用的 RFC 3986 中定义。

这不是很容易理解,因为它非常复杂。在 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,验证器会抱怨它不能再解析引用。

【讨论】:

  • 一般来说,基础 URI 是解析 URI 引用的 URI。当您在链接中提供引用时,这与 HTML 中使用的标准过程相同,它是根据找到它的文档的 URI 来解析的。使用库来解析 URI 引用,因为它不是微不足道的。
  • @awwright 真正的问题是,“我在哪里可以找到 URI 引用解析的规范”。正如你所说,这不是微不足道的,正如我在查看 RFC 时发现的那样!
  • 请注意,您可以对我们的服务器执行我所做的操作,并在目录blah 中使用DirectoryIndex root.json 规则,这意味着您的 id 不需要提供 root.json,那么您的 C $ref 可以是 other.json,查看 api.thisisbeacon.com/schema,您可以看到它在运行中的完整树设置,我将其用作 DirectoryIndex schema.json,因为它也适用于所有子目录
  • @MartinBarker 您假设 example.com 甚至是网络可访问的 URI,并且服务器已连接到 Internet,但情况可能并非如此,我不确定是否与这个问题。让我们在 JSON Schema Slack 上讨论这个问题。
猜你喜欢
  • 2015-10-01
  • 2020-11-17
  • 2012-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多