Array items: same type #685
-
How can a schema be defined where an array's items can be of any type but they must all be of the same type? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
You're thinking of an open generic array type. In C#, this would be something like I wrote a blog post on a way to support generic types in JSON Schema a few months ago. It involves There's not a way to leave it open (not define the |
Beta Was this translation helpful? Give feedback.
-
Another option, if you have a limited set of things an item could be is to use an {
"type": "array",
"anyOf": [
{ "items": { "$ref": "#/$defs/item1" } },
{ "items": { "$ref": "#/$defs/item2" } },
{ "items": { "$ref": "#/$defs/item3" } },
// ...
],
"$defs": {
"item1": { /* ... */ },
"item2": { /* ... */ },
"item3": { /* ... */ },
// ...
}
} This would ensure that all items are the same type, but it could be any of the listed types. |
Beta Was this translation helpful? Give feedback.
Another option, if you have a limited set of things an item could be is to use an
anyOf
with a nesteditems
subschema for each kind of thing you have.This would ensure that all items are the same type, but it could be any of the listed types.