Why does Zod 4 offer z.email(), z.uuid() and z.iso.datetime() as top-level schemas instead of z.string().email(), and what changed in what they accept?
answer
- formats became string subclasses
- method forms still run, deprecated
- RFC variant bits for UUIDs
- datetime wants Z by default
basics
~20 sIn Zod 4 string formats are ZodString subclasses exposed at the top level, shorter and tree-shakable; z.string().email() still works but is deprecated. z.uuid() now enforces RFC variant bits (z.guid() is looser), and z.iso.datetime() rejects offsets unless configured.
solid answer
~40 sZod 4 turned string formats from internal refinements into *subclasses* of `ZodString`, so they are exposed as top-level functions: `z.email()`, `z.uuid()`, `z.url()`, `z.iso.date()`, `z.iso.datetime()` and more. They are shorter, tree-shake better, and keep string methods such as `.max()`. The method forms like `z.string().email()` still work but are deprecated, while `z.string().ip()` and `.cidr()` were removed in favour of `z.ipv4()`, `z.ipv6()`, `z.cidrv4()` and `z.cidrv6()`. Behaviour moved too: `z.uuid()` now enforces the RFC 9562/4122 variant bits, so `z.guid()` is the choice for any 8-4-4-4-12 hex id. `z.iso.datetime()` accepts only `Z`-suffixed timestamps by default, with `{ offset: true }` and `{ local: true }` to widen it. A failure raises an `invalid_format` issue, and the output stays a string, not a `Date`.
code
typescript · 14 linesimport * as z from "zod";
const Email = z.email().max(254);
Email.safeParse("[email protected]").success; // true
const legacyId = "12345678-1234-1234-1234-123456789abc"; // variant character is 1
z.uuid().safeParse(legacyId).success; // false: RFC variant bits required
z.guid().safeParse(legacyId).success; // true
z.iso.datetime().safeParse("2024-05-01T10:00:00Z").success; // true
z.iso.datetime().safeParse("2024-05-01T10:00:00+02:00").success; // false: offsets not allowed by default
z.iso.datetime({ offset: true }).safeParse("2024-05-01T10:00:00+02:00").success; // true
typeof z.iso.datetime().parse("2024-05-01T10:00:00Z"); // "string"go deeper
Recall the top-level spellings, z.email(), z.uuid() and z.iso.datetime(), and that the old z.string().email() style is deprecated but still runs.
Explain why formats became ZodString subclasses, what the default datetime rule rejects, and the invalid_format issue a failed format raises.
Plan the upgrade: replace deprecated method forms, test stored ids against the stricter UUID rule, and widen datetime options only where clients really send offsets.
Decide which formats the public contract promises, such as Z-only timestamps, and let the schema options state that promise instead of accepting everything.
## From methods to top-level schemas A **string format** is a check that a string has a recognised shape: an email address, a UUID, a URL, an ISO 8601 timestamp. In Zod 3 these were methods chained on a string schema, as in `z.string().email()`, and internally each was just another refinement on `ZodString`. Zod 4 changed the model. Each format is now its own **subclass of `ZodString`**, and the API moved to the top-level `z` namespace: - `z.email()` instead of `z.string().email()` - `z.uuid()` instead of `z.string().uuid()` - `z.iso.datetime()` instead of `z.string().datetime()` - `z.iso.date()`, `z.iso.time()` and `z.iso.duration()` likewise The release notes give two reasons: the top-level forms are **less verbose** and **more tree-shakable**, so a bundle only carries the formats it uses. Because each format is still a string schema, it keeps the string methods: `z.email().max(254)` works. The method forms **still exist and work**, but they are marked `@deprecated`, so editors flag them. A few were removed outright: `z.string().ip()` and `z.string().cidr()` are gone, replaced by `z.ipv4()`, `z.ipv6()`, `z.cidrv4()` and `z.cidrv6()`. ## The formats you meet most | Schema | Validates | Worth knowing | |---|---|---| | `z.email()` | email addresses | a deliberately strict regex for ordinary addresses by default | | `z.uuid()` | RFC 9562/4122 UUIDs | checks version and variant bits | | `z.guid()` | any 8-4-4-4-12 hex string | the permissive "UUID-like" form | | `z.url()` | WHATWG URLs | uses `new URL()`, so it is permissive | | `z.iso.date()` | `YYYY-MM-DD` | rejects impossible dates such as `2024-02-30` | | `z.iso.datetime()` | ISO 8601 timestamps | `Z` only unless options widen it | A failed format check raises an issue with code **`invalid_format`** and a `format` field naming the check, for example `"email"`. ## Behaviour changes worth knowing Moving the API was not purely cosmetic. Several formats accept different input in Zod 4: - **Stricter UUIDs.** `z.uuid()` requires a version digit from 1 to 8 and a variant character of `8`, `9`, `a` or `b`, as the RFC specifies; the nil and all-`f` UUIDs are special-cased. Identifiers that merely look like UUIDs fail, and `z.guid()` is the documented escape hatch. You can also pin a version with `z.uuid({ version: "v4" })` or `z.uuidv7()`. - **No padding in `z.base64url()`.** Padded base64url strings now fail. - **IPv6 via the URL parser.** `z.ipv6()` validates with `new URL()` instead of a regex, so some values that passed before now fail. - **Configurable email rules.** `z.email({ pattern })` takes another regex; Zod exports `z.regexes.html5Email`, `z.regexes.rfc5322Email` and `z.regexes.unicodeEmail`. ## z.iso.datetime() options `z.iso.datetime()` accepts a **strict subset** of ISO 8601, and its defaults surprise people: - By default only UTC timestamps ending in `Z` pass, seconds are required, and sub-second precision is arbitrary. - `{ offset: true }` also allows offsets such as `+02:00`; compact forms like `+0200` still fail. - `{ local: true }` also allows timestamps with no zone at all, such as `2024-05-01T10:00`. - `{ precision: n }` pins the time precision: `-1` for minutes only, `0` for whole seconds, or a fixed number of fractional digits. The validation is regex-based. The docs say plainly that it is not as strict as a full date-time library, but it is convenient for validating user input. ## What a format does not do A format schema **validates text and returns the same text**. `z.iso.datetime().parse("2024-05-01T10:00:00Z")` returns a string, not a `Date`, and `z.date()` rejects strings outright. If you need a `Date`, convert after the format check, for example by piping the checked string into `z.coerce.date()`. How to order those steps is covered with transforms and pipes. Formats also do not **normalise**. `z.email()` neither trims nor lowercases, so `" [email protected]"` with a leading space fails, and `"[email protected]"` passes unchanged. If you store addresses, decide where normalisation happens instead of assuming the format check did it. ## Migrating a Zod 3 codebase 1. Replace `z.string().email()` and friends with the top-level forms; the deprecation warnings list them for you. 2. Replace `z.string().ip()` and `.cidr()`, which no longer exist. 3. Run your stored identifiers through `z.uuid()`; switch the ones that fail to `z.guid()` rather than loosening by hand. 4. Check which clients send offsets or local times before adding `offset` or `local` to datetime schemas.
- After upgrading to Zod 4, ids from a legacy system start failing the UUID check. Why, and what do you change?Zod 4's UUID check follows RFC 9562/4122: the version digit must be 1 to 8 and the variant character 8, 9, a or b, with the nil and all-f UUIDs special-cased. Systems that generate UUID-shaped random hex break that rule. Switch those fields to `z.guid()`, which accepts any 8-4-4-4-12 hex pattern.
- Does Zod 4's z.iso.datetime() give you a Date object?No. It validates the text and returns the same string. `z.date()` expects a `Date` instance and rejects strings, so to end up with a `Date` you convert after the format check, for example by piping the checked string into `z.coerce.date()`. Checking the shape first matters because `new Date()` alone accepts many loose shapes.
- How do you loosen Zod 4's z.email() check for international addresses?Pass a different regex through the `pattern` option. Zod exports several: `z.regexes.html5Email` (the browser's rule for email inputs), `z.regexes.rfc5322Email`, and `z.regexes.unicodeEmail`, which allows Unicode, as in `z.email({ pattern: z.regexes.unicodeEmail })`. The default is a deliberately strict rule for ordinary addresses.
- z.string().email() was removed in Zod 4 and no longer compiles
- z.iso.datetime() accepts +02:00 offsets by default
- z.iso.datetime() returns a Date object
- z.uuid() accepts any 8-4-4-4-12 hex string in Zod 4
- A failed z.email() check raises an invalid_type issue
Assumes Zod 4; string formats moved to top-level functions in 4.0, the method forms are deprecated, and z.uuid() became stricter.