The if keyword means that, if the result of the value schema passes validation, apply the then schema, otherwise apply the else schema.
Your schema didn't work because you needed to require "foo" in your if schema, otherwise an empty JSON instance would pass validation of the if schema, and therefore apply the then schema, which requires "bar".
Second, you want "propertyNames":false, which prevents having any keys in the schema, unlike if you were to set "else": false which would make anything always fail validation.
{
"type": "object",
"properties": {
"foo": {
"type": "string"
},
"bar": {
"type": "string"
}
},
"if": {
"properties": {
"foo": {
"enum": [
"bar"
]
}
},
"required": [
"foo"
]
},
"then": {
"required": [
"bar"
]
},
"else": false
}
Answer from Relequestual on Stack OverflowThe if keyword means that, if the result of the value schema passes validation, apply the then schema, otherwise apply the else schema.
Your schema didn't work because you needed to require "foo" in your if schema, otherwise an empty JSON instance would pass validation of the if schema, and therefore apply the then schema, which requires "bar".
Second, you want "propertyNames":false, which prevents having any keys in the schema, unlike if you were to set "else": false which would make anything always fail validation.
{
"type": "object",
"properties": {
"foo": {
"type": "string"
},
"bar": {
"type": "string"
}
},
"if": {
"properties": {
"foo": {
"enum": [
"bar"
]
}
},
"required": [
"foo"
]
},
"then": {
"required": [
"bar"
]
},
"else": false
}
Can't you just use the "else" property ?
{
"type": "object",
"properties": {
"foo": { "type": "string" },
"bar": { "type": "string" }
},
"if": {
"properties": {
"foo": {
"enum": ["bar"]
}
}
},
"then": {
"required": ["bar"]
},
"else": {
"required": []
}
}
Json Schema validation using If-Else - "conditionally choosing required element" not working - General - JSON Forms community
Need help implementing conditional requirements in JSON schema?
JSON schema form does not support "If Then Else"
Is “if-then-else” supported in schema validation?
Videos
I'm using AWS's API Gateway to expose an HTTP API. It allows me to use JSON schema's to map and validate the input body, and I'd like to implement conditional requirements, however, I'm not sure how. Also, I'm not sure whether I should be doing this, or creating a second JSON schema.
In this example, I have an API that can take in two completely different things, apples & oranges if you will. I have the below schema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Apples2Oranges",
"type": "object",
"properties": {
"apples": {
"type": "array",
"items": {
"type": "string"
}
},
"oranges": {
"type": "array",
"items": {
"type": "string"
}
},
"apple-property": { "type": "string" }
}What I want to do is require that the body contains an array for either apples or oranges, but not both! Additionally, if the user supplies a value for apples, they are also required to submit a value for apples-property, they shouldn't be able to submit a value here if they chose oranges...
Is this sort of conditional logic something I should do with JSON schemas? Now that I'm writing this out, it sounds like this would be much easier if I simply had two separate models...
I do not fully understand the intention of your original schema, but how about this one?
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"previous_employment_section": {
"type": "array",
"items": {
"type": "object",
"properties": {
"companyAddress": {
"type": "string"
},
"companyName": {
"type": "string"
}
}
}
},
"isInexperienced": {
"type": "boolean"
}
},
"if": {
"properties": {
"isInexperienced": {
"const": false
}
}
},
"then": {
"properties": {
"previous_employment_section": {
"items": {
"required": [
"companyName",
"companyAddress"
]
},
"minItems": 1
}
}
}
}
It is not possible. We need to have “if” on a higher level, “properties” can be nested. Leadpony's method can be used.