【问题标题】:JSON Schema: How to check every array item that has a specific value has the required properties?JSON Schema:如何检查每个具有特定值的数组项是否具有所需的属性?
【发布时间】:2020-10-18 17:05:54
【问题描述】:

我如何检查属性field1Value1中的每个数组项是否需要属性field2? 如果field1 的值不是Value1,则只需要field1

这是一个例子:

{
  "property_abc":[
    {
      "field1":"Value1",
      "field2": "Value2"
    },
    {
      "field1":"Value2"
    },
    {
      "field1":"Value3"
    }
  ]
}

这是我的架构:

{
  "$schema": "http://json-schema.org/draft-07/schema",
  "additionalProperties": false,
  "properties": {
    "property_abc": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "field1": {
            "enum": [
              "Value1",
              "Value2",
              "Value3"
            ],
            "type": "string"
          },
          "field2": {
            "enum": [
              "Value1",
              "Value2",
              "Value3"
            ],
            "type": "string"
          }
        },
        "allOf": [
          {
            "if": {
              "properties": {
                "property_abc": {
                  "items": {
                    "properties": {
                      "field1": {
                        "const": "Value1"
                      }
                    }
                  }
                }
              }
            },
            "then": {
              "required": [
                "field1",
                "field2"
              ]
            },
            "else": {
              "required": [
                "field1"
              ]
            }
          }
        ]
      }
    },
    "property_xyz": {
      "type": "number"
    }
  },
  "type": "object"
}

上面的例子是正确的。

但是下面的会抛出一个错误,因为property_abc中的第一项属性field2是必需的,但不存在:

{
  "property_abc":[
    {
      "field1":"Value1"
    },
    {
      "field1":"Value2"
    },
    {
      "field1":"Value3"
    }
  ]
}

【问题讨论】:

    标签: json jsonschema json-schema-validator


    【解决方案1】:

    您拥有的if 架构在错误的位置查找 - 此架构适用于 property_abc 数组值内的对象。我在下面粘贴了更正,并将其移到 allOf 之外,这在这里没有用处。

    您还可以查看 dependencies 关键字,它可能会有所帮助,但需要进行一些重构来表达您的约束。

    {
      "$schema": "http://json-schema.org/draft-07/schema",
      "additionalProperties": false,
      "properties": {
        "property_abc": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "field1": {
                "enum": [
                  "Value1",
                  "Value2",
                  "Value3"
                ],
                "type": "string"
              },
              "field2": {
                "enum": [
                  "Value1",
                  "Value2",
                  "Value3"
                ],
                "type": "string"
              }
            },
            "if": {
              "properties": {
                "field1": {
                  "const": "Value1"
                }
              }
            },
            "then": {
              "required": [
                "field1",
                "field2"
              ]
            },
            "else": {
              "required": [
                "field1"
              ]
            }
          }
        },
        "property_xyz": {
          "type": "number"
        }
      },
      "type": "object"
    }
    
    

    【讨论】:

    • 你救了我的命。我花了 4 个多小时试图做到这一点,但无济于事。谢谢。
    猜你喜欢
    • 2019-11-15
    • 2018-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-12
    相关资源
    最近更新 更多