We have the following JSON:
{ "someName" : [1,2,3,4,5] }
or
{ "someName" : ["one","two","three"] }
We want to draft a JSON Schema following the OpenAPI 3.x specification. Our constraint: an array element can be integer or string, but all array elements must be the same type. Our schema looks like this:
{
"type": "array",
"items": {
"oneOf": [
{"type": "string"},
{"type": "integer"}
]
}
}
This does limit the data type inside the array, but still allows to mix strings and integers in one array which we need to avoid.
{"someName" : 1, "two", "three", 4}
We looked at this question, but it didn't address consistent data type
Is there a way in OpenAPI Schema to enforce uniqueness per array?
CodePudding user response:
You need to bring the oneOf
up a level. Also, anyOf
is a better choice in this situation. The result is the same in this case, but anyOf
is more efficient.
{
"type": "array",
"anyOf": [
{ "items": { "type": "string" } },
{ "items": { "type": "integer" } },
]
}