I have two fields in my schema - one is a required property called "name" and the other is optional (used to define a sorting property) called "nameSort" and I want to express
If the "nameSort" field is defined, the "name" field should also be defined as the same value.
Is it possible to express such an "inter-element" constraint with JSON schema? My cursory read of JSON Schema here http://json-schema.org/latest/json-schema-validation.html says no.
Old question, but this can now be done with json schema v5/v6 using a combination of the
constant
and$data
(JSON pointer or relative JSON pointer) keywords.Example:
Where
"1/password"
is a relative JSON pointer saying "go up one level, then look up the key password".You can express one property must be defined when another is present, e.g.:
However, you cannot specify that two properties must have equal values.
Also, why do you have a separate property at all, if it's always going to be equal? And if it's always equal, could you just have a boolean flag to reduce redundancy?