I have been trying to get my JSON schema right. I have a boolean property based on which I have to determine the required properties. Below is my sample JSON which I want to fail the validation with item3 not present.
{
"item1": true,
"item2": "ABC"
}
This is the JSON which I want the validation to pass
{
"item1": true,
"item2": "ABC",
"item3": {
"subItem1": "ABC",
"subItem2": "BAC"
}
}
Similarly, if the item1 is false, then the validation should pass for both the above JSON's.
My JSON schema for the same is as below.
{
"definitions": {},
"type": "object",
"title": "The Root Schema",
"properties": {
"item1": {
"$id": "#/properties/item1",
"type": "boolean",
"title": "The Item1 Schema",
"default": false,
"examples": [
true
]
},
"item2": {
"$id": "#/properties/item2",
"type": "string",
"title": "The Item2 Schema",
"default": "",
"examples": [
"ABC"
],
"pattern": "^(.*)$"
},
"item3": {
"$id": "#/properties/item3",
"type": "object",
"title": "The Item3 Schema",
"required": [
"subItem1",
"subItem2"
],
"properties": {
"subItem1": {
"$id": "#/properties/item3/properties/subItem1",
"type": "string",
"title": "The Subitem1 Schema",
"default": "",
"examples": [
"AAA"
],
"pattern": "^(.*)$"
},
"subItem2": {
"$id": "#/properties/item3/properties/subItem2",
"type": "string",
"title": "The Subitem2 Schema",
"default": "",
"examples": [
"BAC"
],
"pattern": "^(.*)$"
}
}
}
},
"required": ["item1"],
"allOf": [{
"if": {
"properties": {
"item1": {
"enum": [
true
]
}
}
},
"then": {
"required": [
"item2",
"item3"
]
},
"else": {
"required": [
"item2"
]
}
}]
}
My validation always fails.
If item1 is true, subItem2 should be required.
If item1 is false, then item3 is not required, but should still validate if included.
thenandelsethe wrong way round, but I can't tell because your schema and what you say you want seems to be in contradiction. Hence why I asked if you could provide JSON you want to pass and fail, because currently it's unclear.item3has a property ofsubItem1andsubItem2, but it does not.