Understanding When Zod's superRefine Runs
Contents
While implementing conditional validation with Zod (v4), I ran into a case where superRefine was not called even though the condition was met.
The case was a simple form like the one below, with a cross-field check between the check-in and check-out dates.
const schema = z
.object({
guests: z.number().int().min(1, "人数を選んでください"),
checkIn: z.iso.date("チェックイン日を入力してください"),
checkOut: z.iso.date("チェックアウト日を入力してください"),
})
.superRefine((data, ctx) => {
if (data.checkIn && data.checkOut && data.checkOut <= data.checkIn) {
ctx.addIssue({
code: "custom",
path: ["checkOut"],
message: "チェックアウトはチェックインより後にしてください",
});
}
});

Even though the check-in and check-out dates are reversed, only the validation for the number of guests takes effect.
In this post, I work out from Zod's behavior why only other errors show up when I wanted the check in superRefine to run.
The cause was the following Zod behavior.
- Zod does not always run every validation
- If another validation fails along the way and the result becomes "aborted", the later steps do not run
In other words, it seems superRefine is designed to run only when the earlier validations pass their conditions (when the result is not aborted).
So in what cases do the later steps (superRefine) stop running?
Zod decides whether the result is aborted, and so whether to continue, from the contents of "issues", which hold the validation error information.
https://github.com/colinhacks/zod/blob/v4.3.6/packages/zod/src/v4/core/util.ts#L804-L812
Zod controls the state as follows.
-
Cases where later steps can continue
- There are 0 issues
- There is 1 or more issue, but none of them is one that cannot continue (→ all are treated as continue === true)
-
Cases where later steps cannot continue
- payload.aborted === true, or there is an issue with continue !== true
Next, I checked in what order Zod proceeds (→ where issues are added, and where the aborted check happens).
Note
This post was written against Zod v4.3.6.
Conceptually, Zod seems to proceed in the following stages.
1. parse
- Validate the schema itself
2. runChecks
- Decide whether aborted from the issues added by parse
- Run the checks (refine/superRefine, etc.) in order
- Decide whether aborted each time an issue is added
- Before each check, skip the remaining checks if aborted
- parse
https://github.com/colinhacks/zod/blob/v4.3.6/packages/zod/src/v4/core/schemas.ts#L291
- runChecks
https://github.com/colinhacks/zod/blob/v4.3.6/packages/zod/src/v4/core/schemas.ts#L221-L227
In this form example, the following
guests: z.number().int().min(1)
produces invalid_type, because guests is not a number.
The invalid_type issue does not specify continue, so it matches the continue !== true condition in aborted() and the later steps do not run.
You can fix this by adding the when parameter to refine, as below.
With the when parameter, the skip decision is not made by the usual aborted, and whether to run is decided by explicitlyAborted instead.
explicitlyAborted treats the result as aborted only when there is an issue with continue === false, so in this case adding when makes the later step run too.
const schema = z
.object({
guests: z.number().int().min(1, "人数を選んでください"),
checkIn: z.iso.date("チェックイン日を入力してください"),
checkOut: z.iso.date("チェックアウト日を入力してください"),
})
// superRefine's interface does not let you specify when, so use refine
.refine((d) => !d.checkIn || !d.checkOut || d.checkOut > d.checkIn, {
message: "チェックアウトはチェックインより後にしてください",
path: ["checkOut"],
when: () => true, // add when
});

I looked into why superRefine was not taking effect in Zod. When using refine / superRefine, you need to understand this, or the timing at which validation runs will not be what you expect.
https://github.com/colinhacks/zod
https://zenn.dev/kskfks30/articles/5e8b73b699ee55 (Japanese)
Translated from the Japanese original.
Thanks. I'll look into it.
Sorry, that didn't go through. Please try again later.