Capítulo 37 de 61
.encode() runs the schema "in reverse," and different schema features behave differently under reversal: refinements and mutating checks still apply both ways, but defaults, prefaults, and unidirectional transforms only make sense going forward and either get skipped or throw during encode.
.refine(), .min(), .max(), etc. are checked whether you .decode() or .encode(). Internally, .encode() runs two passes — first confirming the input matches the expected type, then running refinement logic — so custom refinements don't see unexpected shapes..trim(), .toLowerCase()) apply during encode too: schema.encode(" hello ") trims just like .decode() does.undefined is not a valid argument to .encode() — attempting it throws..catch() applies only going forward: encoding an invalid value does not fall back to the catch value; it throws a ZodError instead.z.stringbool() encodes using the first truthy/falsy value: if you customized truthy/falsy arrays, .encode(true)/.encode(false) use the first element of the respective array..transform() is strictly unidirectional: any transform anywhere in the schema makes .encode() throw a runtime Error (not a ZodError) — transforms have no defined reverse operation.const schema = stringToDate.refine((date) => date.getFullYear() >= 2000, "Must be this millennium");
schema.encode(new Date("1999-01-01")); // throws ZodError — refinement fails both ways
const stringWithDefault = z.string().default("hello");
stringWithDefault.decode(undefined); // => "hello"
stringWithDefault.encode(undefined); // throws — undefined isn't a valid output-side input
const stringbool = z.stringbool({ truthy: ["yes", "y"], falsy: ["no", "n"] });
stringbool.encode(true); // => "yes" (first truthy value)
stringbool.encode(false); // => "no" (first falsy value)
const transformed = z.string().transform(val => val.length);
transformed.encode(1234); // throws a plain Error, not ZodError — transforms can't reverse
encode) direction unchanged, and which ones either no-op-fail or throw..transform() and expecting .encode() to work on it: it can't — any unidirectional transform anywhere in the schema makes encoding throw. Use a codec instead if you need both directions..catch()'s fallback applies during .encode(): it doesn't — encode-side failures throw normally..transform() anywhere in it — use z.codec() for the genuinely two-way piece instead.