zod4-guide

Use when writing, reviewing, or debugging Zod schemas. Covers Zod 4 breaking changes from Zod 3, new APIs, and common pitfalls. Triggers on z.object, z.string, z.enum, z.record, z.refine, ZodError, schema validation, form validation, API response parsing.

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "zod4-guide" with this command: npx skills add netfishx/zod4-guide/netfishx-zod4-guide-zod4-guide

Zod 4 API Guide

Overview

Zod 4 is a major rewrite with breaking changes from Zod 3. Claude's training data contains Zod 3 patterns — always prefer the Zod 4 syntax below. When unsure, consult https://zod.dev/v4/changelog.

Breaking Changes (Zod 3 → 4)

CategoryZod 3 (WRONG)Zod 4 (CORRECT)
Error msg{ message: "..." }{ error: "..." }
Error params{ invalid_type_error, required_error }{ error: (issue) => issue.input === undefined ? "Required" : "Invalid" }
Error format.format() / .flatten()z.treeifyError(err)
Pretty printmanual formattingz.prettifyError(err)
String formatsz.string().email()z.email()
UUIDz.string().uuid()z.uuidv4() (strict RFC 9562) or z.guid() (lenient)
IP addressz.string().ip()z.ipv4() or z.ipv6()
CIDRz.string().cidr()z.cidrv4() or z.cidrv6()
Recordz.record(valueSchema)z.record(keySchema, valueSchema)
Native enumz.nativeEnum(MyEnum)z.enum(MyEnum)
Enum access.Enum / .Values.enum only
Object merge.merge(other).extend(other.shape)
Object strict.strict()z.strictObject({...})
Object loose.passthrough()z.looseObject({...})
Deep partial.deepPartial()Removed — flatten manually
Refinement chainz.string().refine(...).min(5) brokez.string().refine(...).min(5) works
Type predicate.refine((v): v is T => ...) narrowedNo longer narrows inferred type
Functionz.function().args(...).returns(...)z.function({ input: [...], output: ... })
Promisez.promise(schema)Removed — await before parsing
Default + optionalz.string().default("x").optional(){}{ a: "x" } (default applies)
PrefaultN/A.prefault(val) replicates v3 .default() input behavior
Coerce input typeMatched output typeAlways unknown
InfinityPassed z.number()Rejected by z.number()
.int() / .safe()Different behaviorBoth restrict to safe integer range
Intersection errorThrew ZodErrorThrows regular Error
ZodTypeAnyAvailableRemoved — use z.ZodType
._defschema._defschema._zod.def
Symbols as literalSupportedRemoved
z.ostring() etc.AvailableRemoved

New APIs in Zod 4

Recursive types — use getter instead of z.lazy():

const Category = z.object({
  name: z.string(),
  get subcategories() { return z.array(Category); }
});

Template literals — type-safe string templates:

const css = z.templateLiteral([z.number(), z.enum(["px", "em"])]);
// Type: `${number}px` | `${number}em`

File validation: z.file().min(1024).max(5_000_000).mime(["image/png"])

Number formats: z.int32(), z.uint32(), z.float32(), z.int64()

String boolean: z.stringbool() — parses "true"/"yes"/"1" → true

Overwrite (type-preserving transform): z.number().overwrite(v => v ** 2).max(100)

Metadata: schema.meta({ title: "Email", examples: ["a@b.com"] })

JSON Schema: z.toJSONSchema(schema)

Zod Mini (1.88kb): import * as z from "zod/mini" — functional API, tree-shakable

Common Pitfalls

  1. $ZodIssue has no received field — Zod 4 removed it. Access issue.input instead.
  2. z.record(schema) fails — must be z.record(keySchema, valueSchema).
  3. UUID rejects valid v4 UUIDsz.uuidv4() enforces RFC 9562 variant bits. Use z.guid() for lenient.
  4. Enum keys in record are requiredz.record(z.enum([...]), val) requires ALL keys. Use z.partialRecord() for optional.
  5. .default() applies inside .optional()z.string().default("x").optional().parse(undefined)"x" not undefined.
  6. Error map precedence reversed — schema-level error now beats parse-time handler (opposite of v3).
  7. Refinements no longer narrow typesz.unknown().refine((v): v is string => ...) stays unknown.

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

General

image-gen

Generate AI images from text prompts. Triggers on: "生成图片", "画一张", "AI图", "generate image", "配图", "create picture", "draw", "visualize", "generate an image".

Archived SourceRecently Updated
General

explainer

Create explainer videos with narration and AI-generated visuals. Triggers on: "解说视频", "explainer video", "explain this as a video", "tutorial video", "introduce X (video)", "解释一下XX(视频形式)".

Archived SourceRecently Updated
General

asr

Transcribe audio files to text using local speech recognition. Triggers on: "转录", "transcribe", "语音转文字", "ASR", "识别音频", "把这段音频转成文字".

Archived SourceRecently Updated