You can phrase your constraints as:
- 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)
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"]}
}
]
}
]
}
Alternative structure
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"]}
]
}
]
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…