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

More Zod tasks

Back to the full Zod cheat sheet