Transforming and Refining Zod Data
Go beyond basic validation by transforming parsed values and adding custom refinement rules with Zod.
Transforming and Refining Zod Data is a free tRPC End-to-End Type Safe APIs lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the tRPC End-to-End Type Safe APIs learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Beyond Pass/Fail
Zod does more than accept or reject data. It can transform valid input into a cleaner shape and apply custom rules that built-in validators cannot express.
The transform Method
.transform() changes a value after it passes validation, producing a new output type.
const trimmed = z.string().transform((s) => s.trim());
trimmed.parse(" hi "); // "hi"Coercing Types
Zod can coerce inputs, useful for query strings that arrive as text but should be numbers.
const page = z.coerce.number().int().positive();
page.parse("5"); // 5 as a numberDefault Values
Provide a fallback when a field is missing.
const schema = z.object({
limit: z.number().default(20),
});
schema.parse({}); // { limit: 20 }Custom Refinements
.refine() adds a custom boolean check with a message when it fails.
const password = z.string().refine(
(val) => val.length >= 8,
{ message: "Too short" }
);Cross-Field Validation
Refine an object to compare two fields, like confirming a password.
const form = z.object({
pw: z.string(),
confirm: z.string(),
}).refine((d) => d.pw === d.confirm, {
message: "Passwords must match",
path: ["confirm"],
});superRefine for Multiple Errors
.superRefine() lets you push several issues in one pass for richer validation.
const s = z.string().superRefine((val, ctx) => {
if (!/[A-Z]/.test(val)) ctx.addIssue({ code: "custom", message: "Need uppercase" });
if (!/[0-9]/.test(val)) ctx.addIssue({ code: "custom", message: "Need digit" });
});Chaining Transforms
Validation and transformation chain in order.
const slug = z.string()
.min(1)
.transform((s) => s.toLowerCase().replace(/\s+/g, "-"));
slug.parse("Hello World"); // "hello-world"Input vs Output Types
After a transform, the input type and output type differ. Use z.input and z.output to read each.
type In = z.input<typeof page>; // string | number
type Out = z.output<typeof page>; // numberSafe Parsing
Use safeParse to get a result object instead of throwing, ideal for handling errors gracefully.
const r = password.safeParse("short");
if (!r.success) console.log(r.error.issues);Pipe for Validate-then-Transform
Use .pipe() to first coerce or transform a value and then run further validation on the result.
const id = z.string().transform(Number).pipe(z.number().int());Quick Check
Test your Zod knowledge.
Recap
You leveled up your Zod schemas:
- transform and coerce reshape valid data
- refine / superRefine add custom and cross-field rules
- safeParse handles errors without throwing
These tools turn Zod into a powerful data shaping and validation layer.
Frequently asked questions
Is the “Transforming and Refining Zod Data” lesson free?
Yes — the full text of “Transforming and Refining Zod Data” is free to read here on the web, and the tRPC End-to-End Type Safe APIs course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the tRPC End-to-End Type Safe APIs course, upgrade to CoddyKit PRO.
What will I learn in “Transforming and Refining Zod Data”?
Go beyond basic validation by transforming parsed values and adding custom refinement rules with Zod. You practise tRPC End-to-End Type Safe APIs with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start tRPC End-to-End Type Safe APIs?
No prior experience is required. tRPC End-to-End Type Safe APIs on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Transforming and Refining Zod Data” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this tRPC End-to-End Type Safe APIs lesson?
Yes. Every tRPC End-to-End Type Safe APIs lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Introduction to Zod Schemas
- Defining Complex Zod Schemas
- Integrating Zod in tRPC Procedures
- Transforming and Refining Zod Data