Using Jsonschema draft 6, I'm trying to create a schema that conforms to the following:
- Properties A, B1, B2, and B3 are either numbers or
null
- If property A is present and non-null, then properties B1, B2, and B3 must be either absent or null
- If any of properties B1, B2, and B3 are present and non-null, then property A must be null or absent.
- A, B1, B2, and B3 may all be absent
Examples of conforming documents:
{}
{"A": 1}
{"A": 1, "B2": null}
{"B1": 1}
{"B1": 1, "B2": 1, "B3": 1}
{"A": null, "B1": 1, "B2": 1, "B3": 1}
Examples of non-conforming documents:
{"A": 1, "B1": 2}
{"A": 1, "B1": null, "B2": 1}
I've seen some related questions that help but don't fully answer the question:
- How to make anyOf a set of mutually exclusive properties except one
- Use json-schema to require or disallow properties based on another property value?
- jsonSchema attribute conditionally required
- How to define choice element in json schema when elements are optional?
- How to define a JSON schema that requires at least one of many properties
Here is my current schema, which only enforces constraint #1 and #4:
{
"$schema": "http://json-schema.org/draft-06/schema#",
"properties": {
"A": {"oneOf": [{"type": "null"}, {"type": "number"}],
"B1": {"oneOf": [{"type": "null"}, {"type": "number"}],
"B2": {"oneOf": [{"type": "null"}, {"type": "number"}],
"B3": {"oneOf": [{"type": "null"}, {"type": "number"}]
}
}
What is the right approach here? Am I asking for something unreasonable?