Skip to content

Array items: same type #685

Answered by gregsdennis
mfulton26 asked this question in Q&A
Discussion options

You must be logged in to vote

Another option, if you have a limited set of things an item could be is to use an anyOf with a nested items subschema for each kind of thing you have.

{
  "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.

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by mfulton26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants