Json Schema 中 $dynamicRef $dynamicAnchor 的解释(相对于 $ref 和 $anchor)

And*_* H. 11 jsonschema

有人可以解释一下JSON Schema 中 $dynamicRef 关键字的用途吗

有关实际使用,请参阅 JSON Schema 元架构本身。

它使用 $dynamicAnchor 和 $dynamicRef。

核心架构如下所示

{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "$id": "https://json-schema.org/draft/2020-12/schema",
      ----   <snip>
    "$dynamicAnchor": "meta",      <-- first usage here
      ----   <snip>

    "allOf": [
        {"$ref": "meta/core"},
         ----   <snip>
    ]
      ----   <snip>
}
Run Code Online (Sandbox Code Playgroud)

meta/core(它被“包含”在allOf看起来像这样的

{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "$id": "https://json-schema.org/draft/2020-12/meta/core",
      ----   <snip>

    "$dynamicAnchor": "meta",   <-- second usage here
    
      ----   <snip>
    "properties": {
          ----   <snip>

        "$defs": {
            "type": "object",
            "additionalProperties": { "$dynamicRef": "#meta" }    <-- reference here
              ----   <snip>
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么这么复杂?它是如何运作的?尽管我阅读了规范,但我无法真正理解它。

Jas*_*ers 29

当扩展模式可能需要覆盖引用将解析到的位置时,使用动态引用。这在扩展递归模式(如元模式)中最常见,但也有其他用途。

这可能还没有意义,所以让我们从一个例子开始来说明为什么存在动态引用。该模式描述了一个递归树结构,其值为字符串。

{
  "$id": "https://example.com/schemas/string-tree",

  "type": "array",
  "items": {
    "anyOf": [
      { "type": "string" },
      { "$ref": "#" }
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

这是一个针对该架构进行验证的实例。

["a", ["b", "c", ["d"], "e"]]
Run Code Online (Sandbox Code Playgroud)

现在,假设我们要扩展此模式,以便每个分支最多有两个节点。您可能会想尝试以下模式。

{
  "$id": "https://example.com/schemas/bounded-string-tree",

  "$ref": "/schemas/string-tree",
  "maxItems": 2
}
Run Code Online (Sandbox Code Playgroud)

但是,maxItems约束仅适用于树的根。中的递归引用/schemas/string-tree仍然指向自身,这不会限制节点的数量。

因此,["a", "b", "c"]由于存在三个节点,验证将按预期失败。但是,["a", ["b", "c", "d"]]不会失败,因为具有三个节点的分支仅针对/schemas/string-tree模式进行验证,而不是/schemas/bounded-string-tree针对模式进行验证。

因此,为了扩展递归模式,我需要一种方法来允许扩展模式 ( /schemas/bound-string-tree) 更改扩展模式 ( ) 中的引用目标/schemas/string-tree。动态引用提供了这种机制。

{
  "$id": "https://example.com/schemas/base-string-tree",
  "$dynamicAnchor": "branch",

  "type": "array",
  "items": {
    "anyOf": [
      { "type": "string" },
      { "$dynamicRef": "#branch" }
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,动态引用和锚点的工作方式与常规引用和锚点相同,只是现在可以根据需要通过扩展模式覆盖引用。如果没有扩展模式,它将转到这个动态锚点。如果存在声明匹配动态锚点的扩展模式,它将覆盖此模式,从而更改动态引用解析的位置。

{
  "$id": "https://example.com/schemas/bounded-string-tree",
  "$dynamicAnchor": "branch",

  "$ref": "/schemas/base-string-tree",
  "maxItems": 2
}
Run Code Online (Sandbox Code Playgroud)

通过在 中设置“branch”动态锚点/schemas/bounded-string-tree,我们可以有效地覆盖未来对“branch”的任何动态引用以解析到此位置。

现在,将按预期["a", ["b", "c", "d"]]失败验证。/schema/bounded-string-tree

您可能还听说过$recursiveRefJSON Schema 2019-09。这是动态引用的先前版本,仅适用于扩展递归模式,如本例所示。与递归引用不同,动态引用允许您在架构中设置多个扩展点。让我们更进一步地了解我们的示例,看看为什么这很有用。

假设我们想要一个描述树的模式,但我们希望扩展模式能够覆盖树的叶子的模式。例如,我们可能想要一棵具有多个叶节点而不是字符串的树。我们可以使用动态引用来允许叶子被覆盖。

{
  "$id": "https://example.com/schemas/base-tree",
  "$dynamicAnchor": "branch",

  "type": "array",
  "items": {
    "anyOf": [
      { "$dynamicRef": "#leaf" },
      { "$dynamicRef": "#branch" }
    ]
  },

  "$defs": {
    "leaf": {
      "$dynamicAnchor": "leaf",
      "type": "string"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

现在我们有两个扩展点,可以创建一个有界数树。

{
  "$id": "https://example.com/schemas/bounded-number-tree",
  "$dynamicAnchor": "branch",

  "$ref": "/schemas/base-tree",
  "maxItems": 2,

  "$defs": {
    "leaf": {
      "$dynamicAnchor": "leaf",
      "type": "number"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

动态引用还有一些更复杂的问题,我现在不会讨论。希望这足以说明为什么存在这种复杂的机制以及您何时想要使用它。我希望这也能让事情看起来不那么复杂。

  • (1) 定义的名称不相关。我可以把它称为“foo”,它会改变任何事情。它甚至不需要位于“$defs”中。它可以是任何模式。(2)限制叶子的数量。没有办法限制分支的数量。 (2认同)