Zod

From Esposter/Esposter · View interactive version

Zod is an AI agent skill from the GitHub repository Esposter/Esposter. Apply when writing Zod schemas. Esposter's Zod conventions for schema shape, boundary parsing and constraints. Schemas a schema form renders have extra rules — see the ui-library skill.

How to use this skill

Skill file

Zod Conventions

Settled — do not re-propose

Deep Dives

Imports and Inferred Types

String Normalization — `references/string-normalization.md`

.transform(fn).pipe(refinedSchema), never .refine() for normalisation, with every string constraint on the single final .pipe() output. Reach for createNameSchema(maxLength) (@esposter/db-schema, required) or createNormalizedStringSchema(maxLength) (@esposter/shared, optional prose) before writing the chain by hand; the page says which one fits.

Arrays — Always `createUniqueArraySchema` or `createUniquePrimitiveArraySchema`

Never .array() directly — createUniqueArraySchema(schema, key) for an array of objects, createUniquePrimitiveArraySchema(schema) for an array of primitives, both from @esposter/shared, unless duplicates are genuinely valid (references/arrays.md).

Records — Always `createRecordSchema`

Never a bare z.record(…) — createRecordSchema(keySchema, valueSchema, maxEntries) from @esposter/shared, since Zod never bounds how many entries a record holds (references/records.md).

Zod 4 APIs

Zod 4's top-level builders (z.email(), z.int(), z.strictObject), z.enum(MyValueSet), a refinement's text under error, and an issue pushed onto error.issues (references/zod-4-apis.md).

Validate, Never Cast

Runtime data crossing any trust boundary (queue messages, webhook bodies, subprocess stdout, committed config files) gets a co-located Zod schema parsed at the edge — never x as unknown as SomeType or hand-rolled guards + casts. Full rules: references/boundary-payloads.md.

Persisted Data — Latest Shape Only

Schemas for persisted client-authoritative data (save blobs, localStorage state) and D1 rows model only the latest shape — no union arms for an older shape, no .default()s covering fields older data lacks, no migration code, no read-side inference of a field a pre-change row lacks. A parse failure resets the data to a fresh default; the reset is the migration, and the old shape is deleted in the same commit. Standard: openspec/specs/architecture/persisted-data-latest-shape-only/design.md.

Tightest Possible Constraints — `references/numeric-constraints.md`

Every field carries the tightest constraint its domain allows — a bare z.number() / z.string() is only correct where any value is valid. On a tRPC input the string, array and record half is enforced: apps/web/server/trpc/routers/index.test.ts walks every procedure's input as JSON Schema and fails on a string with no length, an array with no item cap and a record with no entry cap — a format is no length (a URL or an email has no ceiling), and a pattern counts only when it fixes the size — and the webhook payload is held to the same walk (apps/web/server/services/request/webhookPayloadSchema.test.ts). The mapping from a field's meaning to its constraint is that page.

Schema Rules

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.