if - then - else in json schema #718
-
Hello everyone, I hope you're all doing well! I have a question for the more experienced members here. I'm trying to achieve something with a JSON Schema draft-07, but I'm having trouble and could use some help. My goal is to toggle the I've been working on this for two days and haven't found a solution. I've read the documentation, searched online, and even asked ChatGPT, but nothing has worked so far. Here's the schema: {
"$schema": "http://json-schema.org/draft-07/schema",
"type": "object",
"properties": {
"app": {
"type": "object",
"properties": {
"country": {
"type":"string",
"enum": [
"United States of America",
"Brazil"
]
},
"neighborhoods": {
"type": "object",
"properties": {
"street1_address": {
"type": "string"
},
"streets": {
"type": "object",
"properties": {
"street2_address": {
"type": "string"
},
"street3_address": {
"type": "string"
}
},
"if": {
"properties": {
"app": {
"properties": {
"country": {
"const": "Brazil"
}
}
}
}
},
"then": {
"required": [
"street2_address"
]
},
"else": {
"required": [
"street2_address",
"street3_address"
]
}
}
},
"required": [
"street1_address",
"streets"
]
}
},
"required": [
"country",
"neighborhoods"
]
}
}
} A bit more context: please go to jsonschemavalidator, insert the schema above, and try to validate it with: {
"app": {
"country": "United States of America",
"neighborhoods": {
"street1_address": "abobra",
"streets": {}
}
}
} Notice that the Thanks in advance for your assistance! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The problem you're having is that schemas can only look at what's inside them. They can't look up the schema structure. That means that the if-then that you have inside your You need to move the if-then structures up in the hierarchy so they can see both properties. {
"$schema": "http://json-schema.org/draft-07/schema",
"type": "object",
"properties": {
"app": {
"type": "object",
"properties": {
"country": {
"type": "string",
"enum": [
"United States of America",
"Brazil"
]
},
"neighborhoods": {
"type": "object",
"properties": {
"street1_address": {
"type": "string"
},
"streets": {
"type": "object",
"properties": {
"street2_address": {
"type": "string"
},
"street3_address": {
"type": "string"
}
}
}
},
"required": [
"street1_address",
"streets"
]
}
},
"required": [
"country",
"neighborhoods"
],
"if": {
"properties": {
"country": {"const": "Brazil"}
}
},
"then": {
"properties": {
"neighborhoods": {
"properties": {
"streets": {
"required": [
"street2_address"
]
}
}
}
}
},
"else": {
"properties": {
"neighborhoods": {
"properties": {
"streets": {
"required": [
"street2_address",
"street3_address"
]
}
}
}
}
}
}
}
} (Also, your |
Beta Was this translation helpful? Give feedback.
The problem you're having is that schemas can only look at what's inside them. They can't look up the schema structure.
That means that the if-then that you have inside your
neighborhoods
subschema can't see that there's acountry
property.You need to move the if-then structures up in the hierarchy so they can see both properties.