Zod 4: Same Schemas, Faster Types, and a Mini Build
Zod sits at every boundary in my apps, from env vars to API bodies to form input. Version 4 kept the API I know and fixed the two things I'd started to work around, editor speed and bundle size.
Zod is in almost every project I touch. Env vars get parsed at startup, request bodies get parsed in route handlers, and form input gets parsed before it reaches an action. If data crosses a boundary, a Zod schema is usually standing there.
Zod 4 landed this month, published under a zod/v4 subpath inside the 3.x package so libraries can support both while everyone moves over. I've moved two projects so far.
Where it shows up in my apps
Because schemas are shared between client and server, their cost shows up in two places: how fast the editor and tsc feel, and how much JavaScript ships to the browser.
The two things that got better
Type checking got lighter. Big schemas with lots of .extend() and .omit() used to make editor hints lag in larger files. Zod 4 reworked its types to need far fewer instantiations. The difference I noticed was simple: hover hints on large schemas stopped hesitating.
There's a mini build. zod/mini has the same core with a functional, tree-shakable API. It's meant for client bundles where every kilobyte counts:
import { z } from "zod/v4";
export const note = z.object({
title: z.string().min(1, { error: "Give it a title" }),
email: z.email(),
});import * as z from "zod/v4/mini";
export const note = z.object({
title: z.string().check(z.minLength(1)),
email: z.email(),
});I keep the full build on the server and only reach for mini in client code where size matters.
Small changes I liked
| Change | Why it helps |
|---|---|
z.email(), z.url() at top level | Shorter, and the formats are tree-shakable |
One error option | Replaces message, invalid_type_error, errorMap |
z.toJSONSchema() built in | No extra package to generate OpenAPI or tool schemas |
The migration
Most schemas didn't change at all. What I did touch:
- Error options.
messagestill works buterroris the new single place for custom messages. - String formats.
z.string().email()still works and is deprecated, so I switched as I went. - Anything reading error internals. Code that walked
ZodErrorby hand needed a look. Code that used.flatten()or a form library integration mostly didn't.
Why it matters
Validation at the boundaries is the cheapest insurance in a TypeScript app, because types disappear at runtime and the outside world doesn't respect them. Anything that makes that insurance faster to type-check and cheaper to ship makes it easier to put a schema everywhere it belongs.