TanStackT
TanStack7mo ago
24 replies
colossal-harlequin

Conditional zod schema on onSubmit form validator

Probably a skill issue but Is it possible to handle 2 zod schemas conditionally ?

export const createEmployeeSchema = z.object({
email: z.string().email(),
password: z.string().min(8),
firstName: z.string().min(2),
});

export const updateEmployeeSchema = createEmployeeSchema.omit({
password: true
});

and on the form do something like this

validators: { onSubmit: employee ?updateEmployeeSchema : createEmployeeSchema,
        },


Since not much changes besides the password field (which is removed in update form), I'd rather share the same form component

But then on my password form.Field I get this typescript error when trying to render the errors for that field because the error object is unknown
Screenshot_2025-07-11_at_00.13.14.png
Was this page helpful?