I need to validate a JSON using JSON schema.
Sample JSON is something like this
{
"props": [
{
"label": "Residential",
"attributes": {
"housingStatus": "Own"
}
},
{
"label": "Communication",
"attributes": {
"canContact": "Yes"
}
}
]
}
Based on the label value I need to validate the fields inside attributes.
Example:
If the label value is Residential, then housingStatus field inside attributes should be mandatory and have the value Own but these validations should apply only on the object which has the label Residential and not on other label values like Communication in this case.
Same for the other label value, If the label value is Communication, then canContact field inside attributes should be mandatory and have the value Yes
I've tried to use the contains keyword of JSON schema but it's not working as expected and it's trying to apply these validations on other label with value Communication as well.
"allOf": [
{
"if": {
"properties": {
"pops": {
"contains": {
"properties": {
"label": {
"type": "string",
"pattern": "^Residential$"
}
}
}
}
}
},
"then": {
"properties": {
"pops": {
"items": {
"contains": {
"properties": {
"label": {
"type": "string",
"pattern": "^Residential$"
},
"attributes": {
"required": ["housingStatus"],
"properties": {
"housingStatus": {
"enum": ["Own"]
}
}
}
}
}
}
}
}
}
}
]
I really appreciate any help you can provide.