You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
How can i add a custom validation where Metadata.Subjects can be empty or undefined if QuestionLevel !== SUBJECT other wise it should follow the default validations.
I've tried adding superRefine to the SubPartQuestionZodSchema but it was showing an error saying
constSubPartQuestionZodSchema=QuestionSchema.extend({Type: z.literal(QuestionTypes.SUB_PART),Questions: z.array(z.lazy(()=>QuestionZodSchema)),}).superRefine((data,ctx)=>{if(data.QuestionLevel!==QuestionLevel.SUBJECT){if(!data.Metadata.Subjects||data.Metadata.Subjects.length<=0){ctx.addIssue({code: z.ZodIssueCode.invalid_type,expected: "array",received: typeofdata.Metadata.Subjects,message: "Subjects can't be empty",});}}});
one solution i came up with wa getting rid of the Schemas that extends QuestionSchema and add all the extra fields to the QuestionSchema and add custom validations for all of them, kinda getting rid of the discriminatedUnion and replacing it with a large superRefined.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi,
I'm trying to add a conditional validation to my schema. But i've few schema's extending from a main schema.
await QuestionZodSchema.parseAsync(question)
How can i add a custom validation where
Metadata.Subjects
can be empty or undefined ifQuestionLevel
!==SUBJECT
other wise it should follow the default validations.I've tried adding
superRefine
to theSubPartQuestionZodSchema
but it was showing an error sayingone solution i came up with wa getting rid of the Schemas that extends
QuestionSchema
and add all the extra fields to theQuestionSchema
and add custom validations for all of them, kinda getting rid of thediscriminatedUnion
and replacing it with a largesuperRefined
.Is there a better way to do this?
Beta Was this translation helpful? Give feedback.
All reactions