jsonschema 必需的属性与 $ref 无效

jsonschema Required properties inoperative with $ref

我将编写 json 模式来验证树数据。 模式由顶部根和下面的块组成。 该区块下方可能还有另一个区块。

用于验证的架构。

schema = {
    "$schema": "http://json-schema.org/draft-04/schema",
    "$ref": "#/definitions/root",
    "definitions":{
        "root": {
            "properties": {
                "name": {
                    "type": "string"
                },
                "children": {
                    "type": "array",
                    "items": [
                        {"$ref":"#/definitions/block"}
                    ]
                }
            },
            "required": ["name", "children"]
        },
        "block": {
            "properties": {
                "name": {
                    "type": "string"
                },
                "children": {
                    "type": "array",
                    "items": [
                        {"$ref":"#/definitions/block"}
                    ]
                }
            },
            "required": ["name"]
        }
    }
}

以下为测试数据不正确。姓氏属性不存在。

{
  "name": "group8", 
  "children": [
    {
      "name": "group7", 
      "children": [
        {
          "name": "group6", 
          "children": [
            {
              "name": "group5", 
              "children": [ 
                { ###### wrong
                  "children": []
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

此数据验证良好,但不适用于稍微复杂的树。

# Error: ValidationError: file /home/gulliver/.local/lib/python2.7/site-packages/jsonschema/validators.py line 934: 'name' is a required property #
{
  "name": "group8", 
  "children": [
    {
      "name": "group7", 
      "children": [
        {
          "name": "group6", 
          "children": [
            {
              "name": "group12", 
              "children": [
                {
                  "name": "group11", 
                  "children": [
                    {
                      "name": "group10", 
                      "children": []
                    }
                  ]
                }
              ]
            }, 
            {
              "name": "group9", 
              "children": [
                {
                  "name": "group5", 
                  "children": [
                    { ####### wrong
                      "children": []
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    }, 
    {
      "name": "group13", 
      "children": [
        {
          "name": "null1", 
          "children": []
        }
      ]
    }
  ]
}

当树底部的数据无效时,它不起作用。 我的猜测是分支分裂并且发生了这种情况,有人知道为什么或如何修复它吗?

我使用 python 和 json 模式进行了测试。

items 是数组时,它将子模式值应用于实例中数组中的相同索引位置。

例如,您定义...

"items": [
  {"$ref":"#/definitions/block"}
]

只会测试数组中的第一项。它与深度嵌套无关。例如,根据您的模式,以下数据是有效的...

{
  "name": "group8", 
  "children": [
    {
      "name": "group7"
    },
    {
      "something": "else",
      "Not": "name"
    }
  ]
}

(现场演示:https://jsonschema.dev/s/etFGE

如果您修改对 items 的使用,那么它会像您预期的那样工作:

"items": {"$ref":"#/definitions/block"}

(两种用途都这样做)

现场演示:https://jsonschema.dev/s/rk1OD