Below is a simplified / sanitized view of my actual schema. Is it possible to implement a constraint on fieldD (lets say a constraint on array size) using the value of fieldA?
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"fieldA": {
"type": "boolean"
},
"fieldB": {
"type": "object",
"$ref": "#/$defs/fieldB"
}
},
"$defs": {
"fieldB": {
"type": "object",
"properties": {
"fieldC": {
"type": "string"
},
"fieldD": {
"$ref": "#/$defs/fieldD"
}
}
},
"fieldD": {
"type": "array"
}
}
}
You can use if, then
syntax to check conditions on fieldA
and apply constraints on fieldD
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"fieldA": {
"type": "boolean"
},
"fieldB": {
"type": "object",
"$ref": "#/$defs/fieldB"
}
},
"$defs": {
"fieldB": {
"type": "object",
"properties": {
"fieldC": {
"type": "string"
},
"fieldD": {
"$ref": "#/$defs/fieldD"
}
}
},
"fieldD": {
"type": "array"
}
},
"if": {
"required": ["fieldA"],
"properties": {
"fieldA": {
"const": true
}
}
},
"then": {
"properties": {
"fieldB": {
"properties": {
"fieldD": {
"minItems": 1
}
}
}
}
},
"else": {
"properties": {
"fieldB": {
"properties": {
"fieldD": {
<some other constraint or omit `else` completely for a default behavior> }
}
}
}
}
}