Implementing a constraint inside $def using the field value from a higher level schema #598
Answered
by
jdesrosiers
girish-tharwani
asked this question in
Q&A
-
Hello Experts {
"$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"
}
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
jdesrosiers
Jan 30, 2024
Replies: 1 comment 1 reply
-
There's no way to write constraints that make use of values "above" it in the JSON tree. You have to put your "if": {
"properties": {
"fieldA": { "const": true }
},
"required": ["fieldA"]
},
"then": {
"properties": {
"fieldB": {
"properties": {
"fieldD": { "minItems": 1 }
}
}
}
} Notice the nested subschemas in |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
girish-tharwani
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There's no way to write constraints that make use of values "above" it in the JSON tree. You have to put your
if
/then
at a level high enough that every value involved is below theif
/then
.Notice the nested subschemas in
then
to drill down from the top level tofieldD
.