I have a Zod schema defined as follows:
const QuestionSchema = z.object({
Question: z.string(),
QuestionLevel: z.nativeEnum(QuestionLevel),
Metadata: z.object({
Curriculum: z.object({ Name: z.string(), Id: z.number() }),
Qualification: z.array(z.object({ Name: z.string(), Id: z.number() })),
Subjects: z.array(z.object({ Name: z.string(), Id: z.number() })).nonempty(),
Modules: z.array(z.object({ Name: z.string(), Id: z.number() })).nonempty(),
PastPaperReference: z
.object({
Papers: z.array(z.string()).optional(),
Months: z.array(z.string()).optional(),
Years: z.array(z.string()).optional(),
})
.optional()
.nullable(),
}),
});
I need to add a conditional validation to Metadata.Modules
such that if QuestionLevel === "SUBJECT"
, then it’s okay for the Modules array to be empty or null. However, if QuestionLevel is any other value, the Modules array should be non-empty and validated according to the original schema.
How can I achieve this conditional validation for the Modules array?