繁体   English   中英

如果那么JSON Schema嵌套

[英]JSON Schema Nested If Then

我似乎找不到在枚举上应用多个if / then逻辑的工作方式。

anyOf不应用条件逻辑,但是它表示如果它们中的任何一个匹配那么那么好。

allOf再次不应用条件逻辑,但测试属性/必填字段的超集。

这是一个JSON Schema示例:

{
  "definitions": {},
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "http://example.com/root.json",
  "type": "object",
  "title": "The Root Schema",
  "required": [
    "type"
  ],
  "properties": {
    "type": {
      "$id": "#/properties/type",
      "enum": [
        "a",
        "b",
        "c"
      ],
      "title": "The Type"
    },
    "options": {
      "$id": "#/properties/options",
      "type": "object",
      "title": "The Options Schema",
      "oneOf": [
        {
          "if": { "properties": { "type": { "const": "a" } }
          },
          "then": {
            "required": [ "option1" ],
            "properties": {
              "option1": {
                "$id": "#/properties/options/properties/option1",
                "type": "boolean",
                "title": "The option1 Schema"
              }
            }
          }
        },
        {
          "if": { "properties": { "type": { "const": "b" } }
          },
          "then": {
            "required": [ "option2" ],
            "properties": {
              "option2": {
                "$id": "#/properties/options/properties/option2",
                "type": "boolean",
                "title": "The option2 Schema"
              }
            }
          }
        },
        {
          "if": { "properties": { "type": { "const": "c" } }
          },
          "then": {
            "required": [],
            "properties": {}
          }
        }
      ]
    }
  }
}

如果您对此JSON进行验证:

{
  "type": "a",
  "options": {
    "option1": true
  }
}

它失败,因为需要option2

如果将其更改为anyOf则会成功,但如果将JSON更改为无效:

{
  "type": "a",
  "options": {
    "option2": false
  }
}

它仍然成功。

我没有设法嵌套如果/ then / else / if / then / else也工作。

如何检查每种type的属性集,并且不能将它们混合在一起? 这实际上是可行的,还是我应该改变我的设计。

首先,您可以在此处测试您的模式 互联网上有几个这样的网站。

其次,引入了if / then / else构造来替换这些枚举场景的oneOf ,而不是与它结合。

这个子模式

"if": { "properties": { "type": { "const": "a" } } },
"then": {
  "required": [ "option1" ],
  "properties": {
    "option1": {
      "$id": "#/properties/options/properties/option1",
      "type": "boolean",
      "title": "The option1 Schema"
    }
  }
}

type不是a时,实际上不会失败。 它只是说如果 type=a ,则应用then子模式。 如果type 不是 a ,它没有说什么验证,所以它通过了。 如果你添加一个else:false ,它会更符合你的想法,但我鼓励你以不同的方式思考它。

使用oneOf if / then / else ,但不能同时使用两者。 我建议更改您的子模式以使用此格式:

{
  "properties": {
    "type": { "const": "a" },
    "option1": {
      "$id": "#/properties/options/properties/option1",
      "type": "boolean",
      "title": "The option1 Schema"
    }
  },
  "required": [ "option1" ],
}

这断言option1是必需的,并且必须是布尔值,并且type=a 如果type 不是 a ,则此架构将失败,这就是您想要的。

这个答案更详细地描述了您需要做的事情。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM