【问题标题】:Json-schema required/non-required & allowed valeus for fields depend on the values of other fields字段的 Json-schema 必需/非必需和允许值取决于其他字段的值
【发布时间】:2021-06-11 09:22:25
【问题描述】:

我有三个字段,foo、bar、baz。 bar 依赖于 foo,baz 依赖于 bar:

  1. foo 是布尔值
  2. 如果未提供 foo,则禁止使用 bar 和 baz
  3. foo = true: bar 是必需的枚举值 bar1 & bar2
  4. foo = false:禁止使用 bar 和 baz
  5. foo = true & bar = bar1:baz 是必填对象,必填字段 baz1 和非必填字段 baz2 均为字符串
  6. foo = true & bar = bar2:baz 是必填对象,必填字段 baz3 字符串

所以我开始迭代地构建它。到目前为止,我有

{
  "$schema": "http://json-schema.org/draft-07/schema#",  
  "type": "object",
  "properties": {
    "foo": {
      "type": "boolean"
    }
  },
  "allOf": [
    {
      "if": {
        "not": {
          "required": ["foo"]
        }
      },
      "then": {
        "not": {
          "required": ["bar", "baz"]
        }
      }
    },
    {
      "if": {
        "properties": {
          "foo": {
            "const": true
          }
        },
        "required": ["foo"]
      },
      "then": {
        "properties": {
          "bar": {
            "enum": ["bar1", "bar2"]
          }
        },
        "required": ["bar"]
      }
    },
    {
      "if": {
        "properties": {
          "foo": {
            "const": false
          }
        },
        "required": ["foo"]
      },
      "then": {
        "not": {
          "required": ["bar", "baz"]
        }
      }
    },
    {
      "if": {
        "properties": {
          "bar": {
            "const": "bar1"
          }
        },
        "required": ["bar"]
      },
      "then": {
        "properties": {
          "baz": {
            "type": "object",
            "properties": {
              "baz1": { "type": "number" },
              "baz2": { "type": "string" }
            },
            "required": ["baz1", "baz2"],
            "additionalProperties": false
          }
        },
        "required": ["baz"]
      }
    },
    {
      "if": {
        "properties": {
          "bar": {
            "const": "bar2"
          }
        },
        "required": ["bar"]
      },
      "then": {
        "properties": {
          "baz": {
            "type": "object",
            "properties": {
              "baz3": { "type": "number" },
              "baz4": { "type": "string" }
            },
            "required": ["baz3", "baz4"],
            "additionalProperties": false
          }
        },
        "required": ["baz"]
      }
    }
  ]
}

它正确验证了我迄今为止尝试过的所有组合,除了当 baz 存在时没有 foo & bar 或 foo = false & no bar 时,它们都验证为 true,即使我希望它作为两个 bar 都是 false如果不需要 foo 并且如果 foo 为假,则 & baz 设置为不需要。我错过了什么?

【问题讨论】:

    标签: jsonschema json-schema-validator


    【解决方案1】:

    您应该拆分 required 子句,以便它们一次检查一个关键字。

    "required": ["bar", "baz"] 如果 'bar' 和 'baz' 都不存在,则为假,这是您想要的,但如果一个属性存在而另一个属性不存在,则它也将是假的,而不是(因为你然后用“not”包装该检查,使“if”条件为真)。

    因此,将该检查更改为:

    "allOf": [
      { "required": ["bar"] },
      { "required": ["baz"] }
    ]
    

    【讨论】:

      【解决方案2】:

      根据 Ether 的回答,我得到了这个,它似乎按预期工作,所以我提供它是为了方便将来可能需要它的任何人参考:

        "$schema": "http://json-schema.org/draft-07/schema#",  
        "type": "object",
        "properties": {
          "foo": {
            "type": "boolean"
          }
        },
        "allOf": [
          {
            "if": {
              "not": {
                "required": ["foo"]
              }
            },
            "then": {
              "allOf": [
                {
                  "not": {
                    "required": ["bar"]
                  }
                },
                {
                  "not": {
                      "required": ["baz"]
                  }
                }
              ]
            }
          },
          {
            "if": {
              "properties": {
                "foo": {
                  "const": true
                }
              },
              "required": ["foo"]
            },
            "then": {
              "properties": {
                "bar": {
                  "enum": ["bar1", "bar2"]
                }
              },
              "required": ["bar"]
            }
          },
          {
            "if": {
              "properties": {
                "foo": {
                  "const": false
                }
              },
              "required": ["foo"]
            },
            "then": {
              "allOf": [
                {
                  "not": {
                    "required": ["bar"]
                  }
                },
                {
                  "not": {
                      "required": ["baz"]
                  }
                }
              ]
            }
          },
          {
            "if": {
              "properties": {
                "bar": {
                  "const": "bar1"
                }
              },
              "required": ["bar"]
            },
            "then": {
              "properties": {
                "baz": {
                  "type": "object",
                  "properties": {
                    "baz1": { "type": "number" },
                    "baz2": { "type": "string" }
                  },
                  "required": ["baz1", "baz2"],
                  "additionalProperties": false
                }
              },
              "required": ["baz"]
            }
          },
          {
            "if": {
              "properties": {
                "bar": {
                  "const": "bar2"
                }
              },
              "required": ["bar"]
            },
            "then": {
              "properties": {
                "baz": {
                  "type": "object",
                  "properties": {
                    "baz3": { "type": "number" },
                    "baz4": { "type": "string" }
                  },
                  "required": ["baz3", "baz4"],
                  "additionalProperties": false
                }
              },
              "required": ["baz"]
            }
          }
        ]
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-01-21
        • 2023-02-07
        • 1970-01-01
        • 2018-06-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多