Reposting the question How to implement conditional nested properties with JSON Schema (marked as duplicate though its a completely different problem) My JSON schema reads below tried based on : JSON Schema if/then require nested object
{
"$id": "myschema.json",
"if":{
"type":"object",
"properties": {
"common_data": {
"type":"object",
"properties":{
"remote_os": {
"type":"object",
"required":["common_data"],
"const": "Linux"
}
},
"required":["remote_os"]
}
}
},
"then": {
"type":"object",
"properties": {
"file": {
"type": "string",
"pattern": "^(.*.)(bin)$"
}
}
},
"else": {
"type":"object",
"properties": {
"file": {
"type": "string",
"pattern": "^(.*.)(exe)$"
}
}
}
}
Basically adding the if-else
logic to make sure for remote_os=Linux
file
should end up with .bin
and remote_os=Windows
file
should end up with .exe
Now I am trying to validate against below data
{
"common_data": {
"remote_os": "Linux"
},
"file": "abc.bin"
}
Getting error : [<ValidationError: "'abc.bin' does not match '^(.*.)(exe)$'">]
When tried to debug what properties python
jsonschema
is trying build on top of this schema to validate my data. Got this
properties {'common_data': {'type': 'object', 'properties': {'remote_os': {'type': 'object', 'required': ['common_data'], 'const': 'Linux'}}, 'required': ['remote_os']}}
properties {'remote_os': {'type': 'object', 'required': ['common_data'], 'const': 'Linux'}}
properties {'file': {'type': 'string', 'pattern': '^(.*.)(exe)$'}}
So its always matching against the 'pattern': '^(.*.)(exe)$'
irrespective of remote_os
. Looking for some guidance, how to address this issue.