Input type is not inferred in error function of .refine()
Author: matthiashermsenCreated Sep 22, 2026Updated Sep 22, 2026
Hi there , I'm currently using v4.6.5
Given the following example schema
z.object({
foo: z.int().min(5),
bar: z.int().min(1)
})If you want to make sure that bar must be smaller than foo using .superRefine() does the job
schema.superRefine((data, ctx) => {
const { foo, bar } = data;
if (bar > foo) {
ctx.addIssue({
code: "too_big",
message: `There is a maximum of ${foo - 1}.`,
path: ["bar"],
});
}
});but I think in my case using .refine() is "good enough" (requires less manual effort)
schema.refine(
({ foo, bar }) => bar < foo,
{
error: ({ input }) => {
const { foo } = input; // input is unknown, not fully typed
return `There is a maximum of ${foo - 1}.`
},
path: ["bar"],
},
)The problem here is that the issue parameter passed to the error function does not provide types for the input field. input is of type unknown although I would expect the type to be z.infer<typeof schema>.
Is this expected behavior...?
Thanks
Source: colinhacks/zod