互斥财产组 [英] Mutually exclusive property groups
问题描述
假设我有一个具有四个可能属性的对象:a,b,c,d. a和b只能一起出现(即,当且仅当b出现时,a才出现).如果出现a和b,则不会出现c (也就是说,a/b和c是互斥的).如果没有出现a和b,则c可能会出现(但不是必需的). d可以与a/b,c任意组合出现,也可以单独出现.除了a,b,c或d之外,没有其他属性.
Suppose I have an object with four possible properties: a, b, c, d. a and b can only appear together (i.e., a appears if and only if b appears). If a and b appear, c cannot appear (that is, a/b and c are mutually exclusive). If a and b do not appear, c may appear (but is not required to). d can appear in any combination with a/b, c, or on its own. No properties other than a, b, c, or d may appear at all.
如何将其表示为jsonschema?我怀疑我可以使用oneOf
和required
的某种组合,但是我找不到正确的咒语.
How do I express this as a jsonschema? I suspect I could use some combination of oneOf
and required
, but I can't figure out the proper incantation.
推荐答案
您可以将约束表达为:
- 两个:
"a"
和"b"
都存在,而"c"
不存在 - 或:
"a"
和"b"
都不存在. ("c"
可能存在或可能不存在)
- either: both
"a"
and"b"
are present, and"c"
is not present - or: neither
"a"
nor"b"
is present. ("c"
may or may not be present)
在第二点说都不"有点冗长.在这里,我们用allOf
/not
表示了它. (注意:您不能在这里将它们放在单个required
子句中,因为您需要为每个单独的not
.)
Saying "neither" in the second point is a bit verbose. Here, we've expressed it using allOf
/not
. (Note: you can't factor them into a single required
clause here, because you need a separate not
for each one.)
{
"oneOf": [
{
"required": ["a", "b"],
"not": {"required": ["c"]}
},
{
"allOf": [
{
"not": {"required": ["a"]}
},
{
"not": {"required": ["b"]}
}
]
}
]
}
替代结构
还有另一种说法都不做",实际上是再次使用oneOf
.由于必须通过{}
(通过所有内容),则所有其他选项都将被禁止.
There's also another way to say "neither", which is actually to use oneOf
again. Since you must pass exactly one of a oneOf
clause, if one of the entries is {}
(passes everything), then all the other options are banned.
虽然简洁一些,但阅读起来可能不太直观:
While it's slightly more concise, it's possibly slightly less intuitive to read:
{
"oneOf": [
{
"required": ["a", "b"],
"not": {"required": ["c"]}
},
{
"oneOf": [
{},
{"required": ["a"]},
{"required": ["b"]}
]
}
]
}
这篇关于互斥财产组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!