Reputation: 23
I have a JSON request I'm trying to validate against a JSON schema. I'm wondering is it possible to require a field in a subschema base off the value of a field within that same subschema? I've tried anyOf, OneOf and IF, Then, Else to no avail. anyOf returns error "only 1 subschema matches out of 2" and OneOf return "2 subschemas matched instead of one".
{
"field1":"aaa",
"field2":"bbb"
"field3":{
"isTrue":true,
"inner1":"1",
"inner2":"1"
}
}
So for the above Json, can I only require fields inner1 and inner2 if field isTrue is true?
"field3": {
"type": "object",
"properties": {
"isTrue": {
"type":
"boolean"
},
"inner1": {
"type":
"integer"
},
"inner2": {
"type":
"string"
}
},
"anyOf": [
{
"properties": {
"isTrue": {
"const": "true"
}
},
"required": [
"inner1",
"inner2"
]
},
{
"properties": {
"isTrue": {
"const": "false"
}
},
"required": [
"isTrue"
]
}
]
}
The above is my offending Json schema
Upvotes: 0
Views: 2093
Reputation: 53966
Your schema should work if you remove the quotes around "true"
and "false"
-- they are being treated as strings, rather than booleans.
(Also, your data is using strings for both inner1 and inner2, but the schema wants the first of those to be an integer -- perhaps that was a transcription error.)
Upvotes: 1