transform(), pipe() & coerce in Zod
From the Zod cheat sheet ยท Refinements & Transforms ยท verified Jul 2026
transform(), pipe() & coerce
Convert and transform data during validation
typescript
// Transform output
z.string().transform((val) => val.toUpperCase());
// Chain with pipe
z.string()
.transform((val) => parseInt(val))
.pipe(z.number().int().positive());
// Coerce input to type
z.coerce.number().parse("42"); // 42
z.coerce.date().parse("2024-01-01");๐ก z.coerce is a shortcut for the most common preprocess pattern โ type coercion
โก Use coerce for query params and form data where everything starts as strings
๐ .pipe() chains a transform into another schema for additional validation
๐ข Input and output types differ when transforms change the type โ use z.input/z.output
transformcoercepipe