Imagine you have the following type:
type oneOfTwoPossibleArrays =
| [1, 2]
| [3, 4]
If you were to create a schema for it, what would it look like? This is my initial attempt that didn't work as expected:
<Edit: this code works with ajv: ^8.2.0
. I used 7.2.6 when I opened this post.>
const schema: JSONSchemaType<oneOfTwoPossibleArrays> = {
oneOf: [
{
type: 'array',
minItems: 2,
maxItems: 2,
items: [{ type: 'number', const: 1 }, { type: 'number', const: 2 }]
},
{
type: 'array',
minItems: 2,
maxItems: 2,
items: [{ type: 'number', const: 3 }, { type: 'number', const: 4 }]
}
]
}
Interestingly, defining only one of the arrays in the schema does not result in TypeScript errors:
const schema: JSONSchemaType<oneOfTwoPossibleArrays> = {
type: 'array',
minItems: 2,
maxItems: 2,
items: [{ type: 'number', const: 1 }, { type: 'number', const: 2 }]
}