In my schema I declared these properties:
"index_name": {
"type": "string",
"examples": ["foo-wwen-live", "foo"]
},
"locale": {
"type": "string",
"examples": ["wwen", "usen", "frfr"]
},
"environment": {
"type": "string",
"default": "live",
"examples": [
"staging",
"edgengram",
"test"
]
}
I want a JSON body validated against my schema to be valid only if:
index_name
is present, and bothlocale
andenvironment
are not present;locale
and/orenviroment
are present, andindex_name
is not present
In short, locale
and environment
should never be mixed with index_name
.
Test cases and desired results:
These should pass:
Case #1
{
"locale": "usen"
}
Case #2
{
"environment": "foo"
}
Case #3
{
"environment": "foo",
"locale": "usen"
}
Case #4
{
"index_name": "foo-usen"
}
These should NOT pass:
Case #5
{
"index_name": "foo-usen",
"locale": "usen"
}
Case #6
{
"index_name": "foo-usen",
"environment": "foo"
}
Case #7
{
"index_name": "foo-usen",
"locale": "usen",
"environment": "foo"
}
I created the following rule for my schema, however it does not cover all the cases. For example, if both locale
and environment
are present, validation returns failure if index_name
is also present, which is correct behavior according to case #7. But if only one of locale
and environment
is present, it allows index_name
to also be present (fails at cases #5 and #6).
"oneOf": [
{
"required": ["index_name"],
"not": {"required": ["locale", "environment"]}
},
{
"anyOf": [
{
"required": ["locale"],
"not": {"required": ["index_name"]}
},
{
"required": ["environment"],
"not": {"required": ["index_name"]}
}
]
}
]
I'm getting mixed information on how "not": {"required": []}
declaration works. Some people claim this means that it forbids anything declared in the array to be present, in contrary to what idea does the syntax give. Other claim that this should be taken exactly as it sounds: properties listed in the array are not required - they can be present, but it doesn't matter if they aren't.
Apart from this rule, I also require one non-related property to be present in all cases and I set "additionalProperties": false
.
What is the rule that would satisfy all my test cases?