如何在JSON模式中指定一个属性或另一个属性,但并非两者?
我有4个属性:A,B,C和D。
我想拥有A或B。其中一个必须存在。为此,我创建了可以正常工作的模式。
"oneOf": [
{
"required": ["A"]
},
{
"required": ["B"]
}
],
现在,我想包含C和D,但从某种意义上说我要么有C或D,但并非两者兼而有之。 我既不能拥有。换句话说,如果我要包含C,那么我将不包括D,反之亦然。但是,我也可以决定不包括其中任何一个。
我的file.json
{
"A": "Prop",
"C": "Prop"
}
或
{
"B": "Prop",
"D": "Prop"
}
{
"B": "Prop"
}
在我的文件中类似的东西不应允许
您不能在一起a和b。
{
"A": "Prop",
"B": "Prop"
}
您不能同时组合C和D。但是,如上所述,您也不必拥有其中之一。
{
"C": "Prop",
"D": "Prop"
}
I have 4 properties: A, B, C, and D.
I'd like to have either A or B. One of them must exist. For this I created this schema that works fine.
"oneOf": [
{
"required": ["A"]
},
{
"required": ["B"]
}
],
Now, I'd like to have C and D included but in a sense that I either have C or D but not both. And I can have neither. In other words, if I'm going to include C, then I can't include D and vice versa. However, I can also decide not to include either of them.
Something like this in my file.json
{
"A": "Prop",
"C": "Prop"
}
OR
{
"B": "Prop",
"D": "Prop"
}
OR
{
"B": "Prop"
}
The following should not be allowed
You can't have A and B together.
{
"A": "Prop",
"B": "Prop"
}
You can't have both C and D together. But, as mentioned above, you don't have to have one of them either.
{
"C": "Prop",
"D": "Prop"
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我只是对JSON模式非常熟悉。但是,假设您可以嵌套亚表壳,只需逻辑地写出它,然后将其转换为布尔代数。然后XOR是单一的,或者是任何人,并且不是Allof,也不是。
您希望其中一个或多个是真实的:
那就是:
是:
I'm only passingly familiar with JSON schema. But assuming you can nest subschema, just write it out logically and then translate it to Boolean algebra. Then XOR is oneOf, OR is anyOf, AND is allOf, and NOT is not.
You want at one or more of these to be true:
That's:
That's:
我最终这样做:
I ended up doing this: