I would like to apply an additional "required" property in an array sub schema based on the presence of a property in the root schema. I have my schema set like this:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"required":[
"isParentDependency",
"subArray"
],
"properties": {
"isParentDependency": {
"$id": "#/properties/isParentDependency",
"type": "boolean"
},
"subArray": {
"$id": "#/properties/subArray",
"type": "array",
"items": {
"$id": "#/properties/subArrayItem",
"required": ["alwaysRequiredProp"],
"dependencies": {
"isParentDependency":{
"required":["requiredPropIfIsParentDependency"]
}
},
"properties": {
"alwaysRequiredProp": {
"$id": "#/properties/subArray/items/properties/alwaysRequiredProp",
"type": "boolean"
},
"requiredPropIfIsParentDependency": {
"$id": "#/properties/subArray/items/properties/requiredPropIfIsParentDependency",
"type": "boolean"
}
}
}
}
}
}
Passing Cases
{
"isParentDependency": false,
"subArray": [{
"alwaysRequiredProp": true
}]
}
"isParentDependency": true,
"subArray": [{
"alwaysRequiredProp": true,
"requiredPropIfIsParentDependency":true
}]
}
Failing Cases
{
"isParentDependency": true,
"subArray": [{
"alwaysRequiredProp": true
}]
}
Clearly this is not going to work but I have been unable to figure out how to make a pointer to the root schema (or apply an if/else type solution with a $ref)
Any guidance greatly appreciated!
With JSON Schema, each level of nested
properties
down the tree cannot see up the tree. Therefore you have to define your conditions at the top most level to which it needs to see. In this case, that's the root level.Take a look at this schema.
subArray
has been moved to a definition and then referenced, so the conditional application logic can be seen easier.The definition of
subArray
only determines the properties of the items structure if they are provided, and requiresalwaysRequiredProp
. It does not requirerequiredPropIfIsParentDependency
.The conditional application comes in using the
if
andthen
keywords.If the
if
schema is true, then theelse
schema is applied.The value of
if
is a schema. It is applicable to the root level. It requiresisParentDependency
betrue
(it does not require the property itself, because that is ALWAYS required. Requiring the property inside the if statement would NOT mean the property is always required, unlesselse
wasfalse
, but we're not even usingelse
here).When
isParentDependency
istrue
, thethen
schema is applied, which looks down the tree tosubArray
's items, requiringrequiredPropIfIsParentDependency
.You can test this working in browser using jsonschema.dev (preloaded with this schema and your fail instance).