I have seen answers to similar questions that do not quite match this particular case, so apologies if I missed a relevant answer.
I have a heterogeneous array of objects that I would like to validate. These objects have the same format at the top level, but the child objects are quite different and can only be identified by the attributes present in each child.
The problem maps to validating the following data, though I have more than two object types in the array:
{
"heterogeneous_array": [{
"arbitrary_name": "foobar",
"params": {
"aa": "foo",
"ab": "bar"
}
},
{
"arbitrary_name": "barfoo",
"params": {
"ba": "baz",
"bb": "bot"
}
}
]
}
I am using the following schema, which claims to validate the input json even when the objects under the “params” key are invalid. How can I fix the json schema?
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"heterogeneous_array": {
"$ref": "#/definitions/heterogeneous_array"
}
},
"definitions": {
"heterogeneous_array": {
"type": "array",
"items": {
"arbitrary_name": {
"type": "string"
},
"params": {
"oneOf": [{
"$ref": "#/definitions/schema_a"
},
{
"$ref": "#/definitions/schema_b"
}
]
},
"required": ["arbitrary_name", "params"]
}
},
"schema_a": {
"properties": {
"aa": {
"type": "string"
},
"ab": {
"type": "string"
}
},
"additionalProperties": false,
"required": ["aa", "ab"]
},
"schema_b": {
"properties": {
"ba": {
"type": "string"
},
"bb": {
"type": "string"
}
},
"additionalProperties": false,
"required": ["ba", "bb"]
}
}
}
Thank you in advance!