在JSON模式中需要其他几个属性之一
这里没有经验的JSON模式用户。我有一个JSON模式,我想检查以下情况。我已经想知道的大多数人都没有锻炼。
我可以拥有属性A,B,C,D,E,F。一些规则:
- 如果存在E,则需要F,反之亦然。 (获得)
- 如果A,B或C存在,则需要
- D。 >
- 如果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:
- If E exists, it needs F, and vice versa. (Got it)
- If A, B, or C exist, they need D. (Got it)
- D or F must appear (Got it)
- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
必需的
关键字在属性存在时验证为true,并且在不存在时为false。因此,如果和则可以在
中使用
从句: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 inif
andthen
clauses:PS. you can shorten
"patternProperties": { "^*": { ... } }
to"additionalProperties": { ... }
(and^*
isn't a valid regex anyway, so your validator really ought to be erroring about that).