Here is the JSON Schema and the JSON as provided below in the link for illustartion purpose.
Format: The Individual JSON object (with their additional attributes and may vary with other object in the array) within array can be of any 3 region: 'america', 'asia' and 'europe' and at-least on the type of region object should be there. This can be achieved by array minItems property)
Problem Statement:
The Individual JSON object within array can be of any 3 region: 'america', 'asia' and 'europe' and at-least on the type of region object should be there
==> I am able to solve this by putting all the region objects in the anyOf array as I want to match at-least one of the valid region object.
Either the JSON object 'asia' or 'europe' can exist along with other region type. Both cannot coexist.
==> I tried to use 'oneOf' but its passing the ajv validation. Actually it should fail. Can anyone help. Thanks
JSON Schema
{
"type": "object",
"properties": {
"stat_data": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {},
"anyOf": [{
"required": ["region"],
"properties": {
"region": {
"enum": ["america"]
},
"country": {
"type": "string"
},
"population": {
"type": "string"
}
}
},
{
"oneOf": [
{
"required": ["region"],
"properties": {
"region": {
"enum": ["asia"]
},
"country": {
"type": "string"
},
"details": {
"type": "object",
"properties": {
"language": {
"type": "string"
},
"tz": {
"type": "string"
}
}
}
}
}, {
"required": ["region"],
"properties": {
"region": {
"enum": ["europe"]
},
"country": {
"type": "string"
},
"language": {
"type": "string"
}
}
}
]
}
]
}
}
}
}
JSON Object to FAIL as both "asia" and "europe" type object cannot co-exist.
{
"stat_data": [{
"region": "america",
"country": "USA",
"states": "50"
}, {
"region": "asia",
"country": "Japan",
"details": {
"language": "Japanese",
"tz": "utc+9.00"
}
}, {
"region": "europe",
"country": "finland",
"language": "Finnish"
}
]
}
JSON Object to PASS as ONLY "asia" type object exist.
{
"stat_data": [{
"region": "america",
"country": "USA",
"states": "50"
}, {
"region": "asia",
"country": "Japan",
"details": {
"language": "Japanese",
"tz": "utc+9.00"
}
}
]
}
JSON Object to PASS as ONLY "europe" type object exist.
{
"stat_data": [{
"region": "america",
"country": "USA",
"states": "50"
}, {
"region": "europe",
"country": "finland",
"language": "Finnish"
}
]
}