I have a use case where I want to check the keys present in JSON, depending on the value of a different key.
Example JSON-1:
{
"key_name" : "value1",
"foo" : "random_value1"
}
Example JSON-2:
{
"key_name" : "value2",
"bar" : "random_value2"
}
As per these examples,
Rule 1. If the value of "key_name" is "value1" then only "foo" key should be present in JSON.
Rule 2. If the value of "key_name" is "value2", then only "bar" key should be present in JSON.
I have written the following JSON Schema for validating these JSON:
{
"type": "object",
"properties": {
"key_name": {
"type": "string",
"enum": [
"value1",
"value2"
]
},
"foo": {
"type": "string"
},
"bar": {
"type": "string"
}
},
"required": [
"key_name"
],
"additionalProperties": false,
"allOf": [
{
"if": {
"properties": {
"key_name": {
"enum": [
"value1"
]
}
}
},
"then": {
"required": [
"foo"
]
}
},
{
"if": {
"properties": {
"key_name": {
"enum": [
"value2"
]
}
}
},
"then": {
"required": [
"bar"
]
}
}
]
}
Now, as per the rules, the following JSON's are invalid, and should raise an error.
{
"key_name" : "value1",
"foo" : "random_value1",
"bar" : "random_value2"
}
OR
{
"key_name" : "value2",
"bar" : "random_value2",
"foo" : "random_value"
}
But, the above JSON Schema fails to do so. It only checks whether "foo"/"bar" key or not, as per the value of "key_name". It fails to check for existence of any new key.
How to go about it?