⏱️ Estimated read: 3 min
|
|
Core Schema Types
| Schema | Inferred Type | Notes |
|---|
z.string() | string | Add .min(), .max(), .regex() |
z.number() | number | Add .int(), .positive(), .finite() |
z.boolean() | boolean | — |
z.date() | Date | Validates Date objects only |
z.bigint() | bigint | — |
z.symbol() | symbol | — |
z.undefined() | undefined | — |
z.null() | null | — |
z.any() | any | — |
z.unknown() | unknown | — |
z.never() | never | Rejects everything |
String Formats (v4)
| Schema | Replaces | Checks |
|---|
z.email() | z.string().email() | Email format |
z.url() | z.string().url() | URL format |
z.uuid() | z.string().uuid() | RFC 9562/4122 UUID |
z.ipv4() | z.string().ip() | IPv4 address |
z.ipv6() | z.string().ip() | IPv6 address |
z.jwt() | — | JWT format |
z.base64() | — | Base64 string |
z.iso.date() | — | ISO 8601 date |
z.iso.datetime() | — | ISO 8601 datetime |
z.cuid() | — | CUID |
z.ulid() | — | ULID |
Composite Schemas
| Schema | Inferred Type | Notes |
|---|
z.array(S) | T[] | .nonempty(), .min(), .max() |
z.tuple([A, B]) | [A, B] | Fixed length |
z.tuple([A], B) | [A, ...B[]] | v4 rest argument |
z.object({...}) | {...} | Strips unknown keys |
z.strictObject({...}) | {...} | Rejects unknown keys |
z.looseObject({...}) | {...} | Preserves unknown keys |
z.enum([...]) | Literal union | Also accepts TS enums |
z.literal(v) | v | Single value |
z.union([A, B]) | A | B | Sequential trial |
z.discriminatedUnion(k, [...]) | Tagged union | Direct dispatch |
z.record(K, V) | Record<K, V> | Key-value map |
z.map(K, V) | Map<K, V> | — |
z.set(S) | Set<T> | — |
z.intersection(A, B) | A & B | — |
Field Modifiers
| Modifier | Input Type | Output Type |
|---|
.optional() | T | undefined | T | undefined |
.nullable() | T | null | T | null |
.nullish() | T | null | undefined | Same |
.default(v) | T | undefined | T |
Parsing Methods
| Method | Returns | Async | Throws |
|---|
.parse() | Output | No | Yes |
.safeParse() | Union | No | No |
.parseAsync() | Promise | Yes | Yes |
.safeParseAsync() | Promise union | Yes | No |
.validate() | boolean | No | No (v4.6+) |
.validateAsync() | Promise<boolean> | Yes | No |
Type Inference
| Utility | Returns | Use For |
|---|
z.infer<typeof S> | Output type | Parsed data |
z.input<typeof S> | Input type | Raw data, form values |
z.output<typeof S> | Output type | Same as z.infer |
Refinements & Transforms
| Method | Returns | Purpose |
|---|
.refine(fn, opts?) | Boolean | Custom validation |
.superRefine(fn) | Void + ctx | Multiple issues |
.transform(fn) | New type | Input-to-output conversion |
.pipe(schema) | Chained schema | Stage schemas |
.check(fn) | v4 | Replaces .refine in Mini |
.overwrite(fn) | v4 | Same type transform |
Error Customization (v4)
| v3 | v4 |
|---|
message: "..." | error: "..." |
invalid_type_error | error: (iss) => ... |
required_error | error: (iss) => ... |
errorMap | error: (iss) => ... |
Error Formatting (v4)
| Function | Returns | Use |
|---|
z.flattenError(err) | { formErrors, fieldErrors } | Flat forms |
z.treeifyError(err) | Nested tree | Nested data |
z.prettifyError(err) | String | Logging |
Zod v4 Changes
| v3 | v4 |
|---|
z.nativeEnum(E) | z.enum(E) |
.strict() | z.strictObject() |
.passthrough() | z.looseObject() |
.flatten() | z.flattenError() |
.format() | z.treeifyError() |
z.string().email() | z.email() |
z.string().uuid() | z.uuid() (stricter) |
React Hook Form Integration
| Item | Code |
|---|
| Install | npm i react-hook-form zod @hookform/resolvers |
| Resolver | zodResolver(Schema) |
| Form | <form noValidate> |
| Field array key | field.id |
| Cross-field | register("a", { deps: "b" }) |
| Transforms | useForm<z.input<>, unknown, z.infer<>> |
Server Validation
| Pattern | Code |
|---|
| Safe parse | const result = Schema.safeParse(body) |
| Error response | result.error.issues |
| Success data | result.data |
| Server Action | Schema.safeParse({ field: formData.get("field") }) |
| Flatten errors | z.flattenError(result.error).fieldErrors |
Performance
| Technique | Gain |
|---|
.validate() vs .safeParse() | Skips output + error |
z.compile() (v4.6+) | 2–10x on valid containers |
zod/mini | 64–69% smaller bundle |
| Hoisted schemas | Avoid rebuild per call |
Common Patterns
| Pattern | Code |
|---|
| Derive type | type X = z.infer<typeof S> |
| Discriminated union | z.discriminatedUnion("type", [...]) |
| Optional with default | z.string().default("x") |
| Coercion | z.coerce.number(), z.coerce.date() |
| Nullable | z.string().nullable() |
| Transform | z.string().transform(s => new Date(s)) |
| Async refinement | .refine(async (v) => ...) |
| Custom error | z.string({ error: "Required" }) |
Security Rules
| Rule | Detail |
|---|
| Client validation | UX only, bypassable |
| Server validation | Mandatory, use safeParse |
| Mass assignment | Use z.strictObject() |
| Zod’s limits | Not output encoding, not authz |
Quick How-To Guides & Fast FixesNeed a quick fix or step-by-step solution? Browse our fast answers on HowTo.KandZ.me:
Stop using slow, ad-bloated tool sites! 🤮
🔎 Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
✅ Finance (Mortgage, Interest, Inflation)
✅ Tech (Base64, JSON, Dev Suite, IP)
✅ Health (BMI, BMR, TDEE)
✅ Productivity (Timer, Workspace, QR)
⚡️ Fast & Private
🔒 No data leaves your device
💎 100% Free
🔗 Use it now: https://tools.kandz.me
🔖 Bookmark it—you’ll need it later!