I am using Zod to validate my schema, but one particular part of the object I am expecting can have one of three different key/values.
For example:
{
results: {
// I'll only ever receive one of these resultTypes at a time
resultType1: { /* sub-object specific to result type 1 */ }
resultType2: { /* sub-object specific to result type 2 */ }
resultType3: { /* sub-object specific to result type 3 */ }
...
}
}
So results
will only contain one of the different resultTypeX
props.
Currently I have added optional
to each of the types, but that leaves me open to validating true
an object that has a results
object with none of those three, when I want to specify that I have exactly one of them…
const resultType1 = z.object({ ... });
const resultType2 = z.object({ ... });
const resultType3 = z.object({ ... });
const schema = z.object({
results: z.object({
resultType1.optional(),
resultType2.optional(),
resultType3.optional()
})
});
I know there is the required
attribute, but I’m not entirely sure that it is valid here to do what I’m wanting?
Is there a way to make this specification? Could it be an Enum?