在JSON模式中需要其他几个属性之一

发布于 2025-02-07 09:37:09 字数 1198 浏览 1 评论 0原文

这里没有经验的JSON模式用户。我有一个JSON模式,我想检查以下情况。我已经想知道的大多数人都没有锻炼。

我可以拥有属性A,B,C,D,E,F。一些规则:

  1. 如果存在E,则需要F,反之亦然。 (获得)
  2. 如果A,B或C存在,则需要
  3. D。 >
  4. 如果d出现,则必须具有A,B或C。(最好只有A,B或C之一,但如果需要,我会在下游处理该。

这是架构:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "patternProperties": {
    "^*": {
      "type": "object",
      "properties": {
        "A": {
          "type": [ "string", "null" ]
        },
        "B": {
          "type": [ "string", "null" ]
        },
        "C": {
          "type": [ "string", "null" ]
        },
        "D": {
          "type": [ "string", "null" ]
        },
        "E": {
          "type": [ "string", "null" ]
        },
        "F": {
          "type": [ "string", "null" ]
        }
      },
      "dependentRequired": {
        "E": [ "F" ],
        "F": [ "E" ],
        "A": [ "D" ],
        "B": [ "D" ],
        "C": [ "D" ]
      },      
      "oneOf": [
          { "required": [ "D" ] },
          { "required": [ "F" ] }
      ]
    }
  }
}

如果您仅提交“ D.”,则验证应失败。但是在这个模式中,它将通过。

我已经尝试与If/thens,Anyofs一起玩...只是无法使规则4工作。一些建议会有所帮助!先感谢您。

Inexperienced JSON Schema user here. I have a JSON Schema that I would like to check for the following cases. Most I've figured out, but one just isn't working out.

I could have properties A, B, C, D, E, F. Some rules:

  1. If E exists, it needs F, and vice versa. (Got it)
  2. If A, B, or C exist, they need D. (Got it)
  3. D or F must appear (Got it)
  4. If D appears, it must have either A, B, or C. (Preferably only one of A, B, or C, but I'll handle that downstream if needed.) (This one is eluding me.)

Here's the schema:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "patternProperties": {
    "^*": {
      "type": "object",
      "properties": {
        "A": {
          "type": [ "string", "null" ]
        },
        "B": {
          "type": [ "string", "null" ]
        },
        "C": {
          "type": [ "string", "null" ]
        },
        "D": {
          "type": [ "string", "null" ]
        },
        "E": {
          "type": [ "string", "null" ]
        },
        "F": {
          "type": [ "string", "null" ]
        }
      },
      "dependentRequired": {
        "E": [ "F" ],
        "F": [ "E" ],
        "A": [ "D" ],
        "B": [ "D" ],
        "C": [ "D" ]
      },      
      "oneOf": [
          { "required": [ "D" ] },
          { "required": [ "F" ] }
      ]
    }
  }
}

The validation should fail if you only submit "D." But in this schema it will pass.

I've tried playing around with if/thens, anyOfs, ... just not able to get Rule 4 working. Some advice would be helpful!! Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧瑾黎汐 2025-02-14 09:37:09

必需的关键字在属性存在时验证为true,并且在不存在时为false。因此,如果和则可以在中使用从句:

"if": {
  "required": ["D"]
},
"then": {
  "oneOf": [
    { "required": ["A"] },
    { "required": ["B"] },
    { "required": ["C"] }
  ]
}

ps。您可以缩短“ pattern properties”:{“^*”:{...}} to “ fromeproperties”:{...}(和^**)无论如何都不是有效的正则义务,因此您的验证器确实应该对此有所错误)。

The required keyword validates to true when the property exists, and is false when it does not. Therefore it can be used in if and then clauses:

"if": {
  "required": ["D"]
},
"then": {
  "oneOf": [
    { "required": ["A"] },
    { "required": ["B"] },
    { "required": ["C"] }
  ]
}

PS. you can shorten "patternProperties": { "^*": { ... } } to "additionalProperties": { ... } (and ^* isn't a valid regex anyway, so your validator really ought to be erroring about that).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文