I'm thinking about the following validation with zod and I have no clue on how to do it (or if it's possible with zod). I want an array of objects, all with the same shape, with some of them with literal props, I need these always present in the array.
Example: I need always in the array the objects those with name required1 and required2, and then other objects optionals following the same shape.
[
{
name: z.literal('required1'),
otherprop: z.number()
},
{
name: z.literal('required2'),
otherprop: z.number()
},
// I want to include one or more of the following too (optionals).
{
name: z.string(),
otherprop: z.number()
},
]
This other example needs to throw because required2 is missing
[
{
name: z.literal('required1'),
otherprop: z.number()
},
// I want to include one or more of the following too.
{
name: z.string(),
otherprop: z.number()
},
]
Any clue?