Fill missing settings with safe values
Configuration parsing
Zod · Module
.default() supplies a fallback when a schema receives undefined. It returns that fallback immediately and can also generate defaults through a function.
Call .default() on an existing schema to provide a fallback for undefined input. The fallback is returned immediately rather than being parsed through the wrapped schema, while a function can generate a fresh default each time one is needed.
schema.default(value)| Name | Type | Description |
|---|---|---|
value | output type or () => output type | The fallback value, or a function that produces it when undefined input needs a default; it must be assignable to the schema output type. |
A schema that accepts undefined as input and returns the configured default when the input is undefined.
.default(value) to attach a fallback to an existing schema.z.string().import { z } from "zod";
const name = z.string().default("Anonymous");
console.log(name.parse(undefined)); // "Anonymous"
console.log(name.parse("Ada")); // "Ada"Creates a string schema that supplies "Anonymous" when the parsed input is undefined.
Configuration parsing
Object validation
Dynamic initialization
.default() do when parsing undefined?.default(value) makes undefined input produce the supplied fallback. It also makes the schema's input accept an absent value while preserving the schema's output type.
Follow-up: How does it differ from .prefault()? .default() returns the fallback directly, whereas .prefault() parses the fallback through the schema.
The default is returned eagerly, so the wrapped schema does not validate or transform that fallback. The fallback therefore needs to be assignable to the schema's output type.
Follow-up: Why can a transformed schema require an output-shaped default? Because the default skips the transformation pipeline and is emitted as the result.
Pass a callback instead of a fixed value when defaults must be generated dynamically. Zod re-executes that callback whenever it needs to create a default.
Follow-up: What would you use for a default that must still undergo validation? .prefault() supplies a pre-parse value and keeps parsing active.
z.prefaultz.optionalz.objectz.string