【问题标题】:Mutually exclusive combinations of properties互斥的属性组合
【发布时间】:2018-11-25 15:56:44
【问题描述】:

使用 Jsonschema 草案 6,我正在尝试创建符合以下内容的模式:

  1. 属性 A、B1、B2 和 B3 是数字或 null
  2. 如果属性 A 存在且非 null,则属性 B、C 和 D 必须不存在或为 null
  3. 如果属性 B1、B2 和 B3 中的任何一个存在且非 null,则属性 A 必须为 null 或不存在。
  4. A、B1、B2 和 B3 可能都不存在

合格文件示例:

{}

{"A": 1}

{"A": 1, "B2": null}

{"B1": 1}

{"B1": 1, "B2": 1, "B3": 1}

{"A": null, "B1": 1, "B2": 1, "B3": 1}

不合格文件示例:

{"A": 1, "B1": 2}

{"A": 1, "B1": null, "B2": 1}

我看到了一些相关的问题,但没有完全回答问题:

这是我当前的架构,它只强制执行约束 #1 和 #4:

{
  "$schema": "http://json-schema.org/draft-06/schema#",
  "properties": {
    "A": {"oneOf": [{"type": "null"}, {"type": "number"}],
    "B1": {"oneOf": [{"type": "null"}, {"type": "number"}],
    "B2": {"oneOf": [{"type": "null"}, {"type": "number"}],
    "B3": {"oneOf": [{"type": "null"}, {"type": "number"}]
  }
}

这里的正确方法是什么?我的要求是不合理的吗?

【问题讨论】:

    标签: jsonschema


    【解决方案1】:
    {
      "$schema": "http://json-schema.org/draft-06/schema#",
      "oneOf": [
        {
          "properties": {
            "A": {"type": "number"},
            "B": {"type": "null"},
            "C": {"type": "null"},
            "D": {"type": "null"}
          },
          "required": ["A"]
        },
        {
          "properties": {
            "A": {"type": "null"},
            "B1": {"type": ["number","null"]},
            "B2": {"type": ["number","null"]},
            "B3": {"type": ["number","null"]}
          },
          "anyOf": [
            {"required": ["B1"]},
            {"required": ["B2"]},
            {"required": ["B3"]}
          ]
        },
        {
          "properties": {
            "A": {"type": "null"},
            "B1": {"type": "null"},
            "B2": {"type": "null"},
            "B3": {"type": "null"}
          }
        }
      ]
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-24
      • 1970-01-01
      • 2015-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多