【问题标题】:jsonschema Required properties inoperative with $refjsonschema 必需的属性与 $ref 无效
【发布时间】:2021-04-19 04:51:14
【问题描述】:

我将编写 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 和 jsonschema 进行了测试。

【问题讨论】:

  • 在 draft-07 之前,我会避免在架构的根目录中使用 $ref。在某些实现中,这会导致错误。

标签: json jsonschema


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2019-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-10
    • 1970-01-01
    • 2016-12-07
    相关资源
    最近更新 更多