I'm trying to implement this condition: if a particular property exists, then another property is required; but if it does not exist, another one is not required.
Also, in JSON schemas, can we use not in dependencies?
Here is a sample schema
var schema = {
"properties": {
"smaller": {
"type": "number"
},
"larger": { "type": "number" },
"medium":{'type':'string'},
"bulky":{'type':'string'}
},
require:['smaller','larger'],
additionalProperties:false
};
If "medium" is present, then "bulky" is required. Otherwise, "bulky" is not required.
Here "not required" means that if "medium" doesn't exist, then bulky must not be present.
JSON Schema Draft-07 has included these new keywords
if
,then
andelse
which allow you to have conditional schemas.In this example:
foo
property is requiredfoo
is set to"bar"
then thebar
property also becomes requiredHopefully this makes sense and you can adapt your code accordingly.
PS: in your schema you have the
require
property which I'm not sure is a valid JSON Schema keyword. You probably meantrequired
instead.if you have multiple properties that dependent on respective values, you can use property dependencies.
There are several ways to achieve required effect even not using JSON Schema draft-07 if-then-else.
logical operator and implication (draft-04 and above)
A logical implication here: if "medium" present then "bulky" is required can be translated to "medium" not present OR "bulky" is "required" (the latter implicates "medium" is present) which can be further elaborated to "medium" not required OR "bulky" is "required" (since if "medium" is present, it will satisfy condition of being required). See below schema:
Check here for reference:
JSON schema - valid if object does *not* contain a particular property
http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.7
"anyOf" - logical OR, "oneOf" - XOR, "allOf" - AND, "not" - negation, yet pay attention to spec:
draft-06 - dependencies + propertyNames
Most obvious. I am not sure if you excluded this one in your question, so putting here just in case. Please note, that instead of "additionalProperties", if you wan't simply to limit valid keys, "propertyNames" could be used (and is actually what it was added for).
Check here for reference: http://json-schema.org/latest/json-schema-validation.html#rfc.section.6.5.7
Update
After clarification in comment:
"must not" means preventing bulky being present.
I will rephrase your condition:
1. if "medium" exists "bulky" must be present -> both keys must be present at the same time
2. if "medium" does not exist "bulky" must not be present as well -> both keys must not be present at the same time
Can "bulky" exist and "medium" does not exist?
No. See 2. And vice versa (see 1.). Boolean equality (complementary to logical XOR).
Thus if "bulky" exists - it means "medium" must be always there... It implies that both are required or both must not be required (or even allowed).
Since it's draft-06, you can use also "propertyNames" for defining allowed property names (kind of shortcut to this logic).
logical operator and implication (draft-06 and above)
The proper logical operation translated to JSOn Schema would look like:
An XOR - EITHER (both required) OR (medium not required AND bulky not required).
Please note I am not doing "not" : { "required" : ["medium","bulky"] } as when just one of those keys is present, "required" schema would fail which would mean "not" would return successfull validation result. One needs to rephrase it using de Morgans laws:
However using "propertyNames" will also do the trick. See following schema:
Does it answer your question?