JSON模式:如果至少一个嵌套项目设置了另一个属性,则为require属性 [英] JSON Schema: require property if at least one nested item has another property set

查看:81
本文介绍了JSON模式:如果至少一个嵌套项目设置了另一个属性,则为require属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个数据结构:

{
  "ELEMENTS": {
    "element_1": {
      "requiredPropIfAtLeastOneFlag": "",
      "ITEMS": {
        "item_1": {
          "flag": "flag foo"
        },
        "item_2": {
          "flag": "flag bar"
        }
      }
    }
  }
}

如果 ITEMS 的至少一个设置了 flag 键,则我要强制要求 requiredPropIfAtLeastOneFlag

I am trying to make requiredPropIfAtLeastOneFlag mandatory if at least one of the ITEMS has the flag key set.

我尝试了以下模式:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "ELEMENTS": {
      "type": "object",
      "additionalProperties": false,
      "patternProperties": {
        "^[a-zA-Z](\\w*[a-zA-Z0-9])?$": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "requiredPropIfAtLeastOneFlag": {
              "type": "string"
            },
            "ITEMS": {
              "type": "object",
              "additionalProperties": false,
              "patternProperties": {
                "^[a-zA-Z](\\w*[a-zA-Z0-9])?$": {
                  "type": "object",
                  "additionalProperties": false,
                  "properties": {
                    "flag": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "if": {
            "properties": {
              "ITEMS": {
                "patternProperties": {
                  "^[a-zA-Z](\\w*[a-zA-Z0-9])?$": {
                    "required": [
                      "flag"
                    ]
                  }
                }
              }
            }
          },
          "then": {
            "required": [
              "requiredPropIfAtLeastOneFlag"
            ]
          }
        }
      }
    }
  }
}

它有效,但前提是两者 item_1 item_2 都设置了 flag 属性.换句话说,即使 item_1 设置了 flag ,以下数据也被视为有效:

and it works, but only if both item_1 and item_2 have the flag property set. In other words, the following data is considered valid, even if item_1 has the flag set:

{
  "ELEMENTS": {
    "element_1": {
      // "requiredPropIfAtLeastOneFlag": "",
      "ITEMS": {
        "item_1": {
          "flag": "flag foo"
        },
        "item_2": {
          // "flag": "flag bar"
        }
      }
    }
  }
}

ELEMENTS ITEMS 的所有属性都是动态的,因此"additionalProperties":false 配置和 patterProperties 正则表达式.

All the properties of ELEMENTS and ITEMS are dynamic, therefore the "additionalProperties": false configuration and the patterProperties regular expression.

如果至少一个 item 设置了 flag 并且 requiredPropIfAtAtLeastOneFlag 是丢失.

I haven't been able to figure out how to invalidate the data if at least one item has the flag set and requiredPropIfAtLeastOneFlag is missing.

我使用 https://www.jsonschemavalidator.net 进行测试.

谢谢.

推荐答案

尝试使用此方法,而不要使用 if / then . patternProperties 确保没有一个属性具有"flag".如果那是假的,那么我们知道至少一个属性具有标志",可以通过用 not 否定表达式来表示.

Try this instead of the if/then. patternProperties ensures that none of the properties have "flag". If that is false, then we know that at least one of the properties has "flag", which can be expressed by negating the expression with not.

"not": {
  "patternProperties": {
    "^[a-zA-Z](\\w*[a-zA-Z0-9])?$": {
      "not": { "required": ["flag"] }
    }
  }
}

这篇关于JSON模式:如果至少一个嵌套项目设置了另一个属性,则为require属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆