In Zod 4, what is the difference between .default(), .prefault() and .catch() on a schema?
answer
- undefined versus invalid
- short-circuit or parse
- output type or input type
- Zod 3 default worked differently
basics
~20 sIn Zod 4, .default() returns its value as-is when the input is undefined, skipping validation. .prefault() substitutes its value for undefined and then parses it. .catch() returns its fallback whenever validation fails, whatever the input was.
solid answer
~40 s`.default(value)` fires only on `undefined` and **short-circuits**: the value is returned without running the schema, so it must already match the output type. `.prefault(value)` also fires on `undefined`, but feeds the value into the schema, so checks and transforms run on it: `z.string().trim().toUpperCase().prefault("eur")` yields `"EUR"`, while `.default("eur")` yields `"eur"`. `.catch(value)` fires on any validation failure and discards the issues, so `safeParse()` reports success; its function form gets the caught issues and the original input. Zod 4 changed `.default()`: in Zod 3 it parsed an input-typed value, which is exactly what `.prefault()` now does, so migrating schemas with transforms often means swapping one for the other.
code
typescript · 12 linesimport * as z from "zod";
const currencyDefault = z.string().trim().toUpperCase().default("eur");
const currencyPrefault = z.string().trim().toUpperCase().prefault("eur");
currencyDefault.parse(undefined); // "eur" (returned as-is, not parsed)
currencyPrefault.parse(undefined); // "EUR" (parsed like real input)
const guests = z.number().int().min(1).catch(1);
guests.parse(4); // 4
guests.parse("four"); // 1, the invalid_type issue is discarded
guests.parse(undefined); // 1go deeper
Recall the triggers: default and prefault replace a missing, undefined value, while catch replaces any value that fails validation.
Explain short-circuit versus parse, why default takes an output-typed value and prefault an input-typed one, and what changed from Zod 3.
Spot catch hiding bad input in review, fix shared timestamps from non-function defaults, and plan the default-to-prefault edits in a Zod 3 migration.
Set the team rule for fallbacks: where silently substituting a value is acceptable, and where the caller must always be told their input was wrong.
## Three wrappers, two triggers Zod 4 has three ways to put a value in place of the one you received. They differ in **what triggers them** and in **whether the substitute goes through the schema**. | Wrapper | Triggered by | Value must match | Goes through the schema? | |---|---|---|---| | `.default(value)` | Input is `undefined` | The **output** type | No, returned as-is | | `.prefault(value)` | Input is `undefined` | The **input** type | Yes, parsed like real input | | `.catch(value)` | Validation failed, for any input | The **output** type | No, returned as-is | All three also accept a function instead of a value, and Zod calls it each time a substitute is needed. ## .default(): short-circuit on undefined - **Only `undefined` triggers it.** `null`, an empty string or `0` are real values and are validated normally. A missing object key reads as `undefined`, so `.default()` is how a booking's optional `guests` field becomes `1`. - **The value is returned without parsing.** No check, refinement or transform on the inner schema runs on it, so a default must already be in its final form. - **A function default is re-run per parse.** `z.date().default(() => new Date())` produces a fresh date each time, while `z.date().default(new Date())` reuses the one created when the schema was built. - **It applies inside optional fields in Zod 4.** `z.object({ a: z.string().default("x").optional() }).parse({})` returns `{ a: "x" }`; in Zod 3 it returned `{}`. ## .prefault(): a default that goes through the pipeline `.prefault()` ("pre-parse default") substitutes its value for `undefined` and then lets parsing continue, so every check and transform runs on it. ```ts const currency = z.string().trim().toUpperCase().prefault("eur"); currency.parse(undefined); // "EUR" ``` The same chain with `.default("eur")` returns `"eur"` untouched. Use `.prefault()` when the substitute should be normalised exactly like user input, or when a transform changes the type and you want to write the substitute in the input form: `z.string().transform((s) => s.length).prefault("tuna")` parses `undefined` to `4`. ## .catch(): a fallback for failure `.catch()` reacts to any validation failure, not just a missing value. - **It swallows issues.** `z.number().catch(1).parse("four")` returns `1`, and a `safeParse()` reports success with no issues at all. - **It also covers `undefined`**, because `undefined` fails most schemas. - **A function form receives a context** with the issues that were caught (`ctx.issues`) and the original input (`ctx.value`), so you can log what you threw away. - **It belongs on low-stakes fields**: a display preference, an optional sort order. On a price or a permission it hides bad input the caller needed to hear about. ## One booking request, all three ```ts const BookingRequest = z.object({ guests: z.number().int().min(1).default(1), currency: z.string().trim().toUpperCase().length(3).prefault("eur"), sortReviews: z.enum(["newest", "rating"]).catch("newest"), }); BookingRequest.parse({ sortReviews: "loudest" }); // { guests: 1, currency: "EUR", sortReviews: "newest" } ``` - `guests` is missing, so the default `1` is returned without running `.int()` or `.min(1)`. - `currency` is missing, so `"eur"` is parsed: trimmed, upper-cased and length-checked into `"EUR"`. - `sortReviews` holds an invalid value, so the fallback replaces it and the request succeeds without a word to the caller. ## What changed from Zod 3 1. In Zod 3, `.default()` took a value of the **input** type and parsed it, so `z.string().transform((s) => s.length).default("tuna")` returned `4`. 2. In Zod 4, `.default()` takes a value of the **output** type and returns it immediately, so the same chain needs `.default(4)`. 3. Zod 4 added `.prefault()` to keep the old behaviour, so migrating code with transforms usually means renaming some `.default()` calls to `.prefault()`. 4. Defaults inside optional object fields are now applied, which can break code that checked whether a key existed. ## Picking one - Absent value, final form known: `.default()`. - Absent value that should be normalised or transformed like real input: `.prefault()`. - Any invalid value should quietly become a safe fallback: `.catch()`, and log what it caught. - In two-way codecs, all three apply only in the forward (decode) direction. How a default changes the static input and output types is covered in the lesson on schema-derived types; this lesson keeps the runtime behaviour.
- Your Zod 3 schema z.string().transform((s) => s.length).default("tuna") stopped compiling after upgrading to Zod 4. Why, and what are the two fixes?Zod 4's `.default()` expects a value of the output type, here a number, because it returns the value without parsing. Either write `.default(4)`, the final value, or switch to `.prefault("tuna")`, which keeps the Zod 3 behaviour of parsing an input-typed value through the transform.
- When is .catch() a bad idea?Whenever the caller needs to know their input was wrong. `.catch()` discards the issues, so `safeParse()` reports success and no error reaches the client. That is acceptable for a cosmetic setting, but on a price, a quantity or anything security-relevant it turns invalid input into silently different behaviour. If you do use it, log `ctx.issues` from the function form.
A default is a pre-filled form handed straight to the clerk; a prefault is a blank you fill in yourself, so it goes through the same checks as every other form; a catch is the clerk binning any rejected form and filing a standard one instead.
- Says .default() replaces null and empty strings as well as undefined.
- Believes a Zod 4 default value is validated by the schema's checks.
- Thinks .catch() still reports the issue alongside the fallback value.
- Uses .default(new Date()) expecting a fresh timestamp on every parse.
- Puts .catch() on a payment amount to avoid rejecting requests.
Assumes Zod 4: .default() now short-circuits with an output-typed value, while Zod 3 parsed an input-typed default, the behaviour .prefault() now provides.