Loading skill
Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Zod schema validation patterns for API types
.claude/skills/ledgerhq-zod-schemas/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-01 | ✗→✓ | ▲ Improved | -16% | 0% |
| case-03 | ✗→✓ | ▲ Improved | 21% | 0% |
| case-04 | ✗→✓ | ▲ Improved | -41% | 0% |
| case-06 | ✗→✓ | ▲ Improved | -34% | 0% |
| case-16 | ✗→✓ | ▲ Improved | -39% | 0% |
Define types in state-manager/types.ts using Zod schemas for runtime validation.
typescript// ✅ GOOD - state-manager/types.ts import { z } from "zod"; // 1. Define tag types as enum export enum MyDataTags { Items = "Items", Item = "Item", } // 2. Define Zod schemas with validation rules const ItemSchema = z.object({ id: z.string().uuid(), name: z.string().min(1), value: z.number().min(0), status: z.enum(["active", "inactive"]), createdAt: z.string().datetime(), }); const ItemResponseSchema = z.object({ data: ItemSchema, meta: z.object({ timestamp: z.string(), version: z.string(), }), }); const ItemListResponseSchema = z.object({ items: z.array(ItemSchema), pagination: z.object({ nextCursor: z.string().optional(), total: z.number(), }), }); // 3. Infer TypeScript types from schemas export type Item = z.infer<typeof ItemSchema>; export type ItemResponse = z.infer<typeof ItemResponseSchema>; export type ItemListResponse = z.infer<typeof ItemListResponseSchema>; // 4. Define query params as interfaces export interface GetItemsParams { search?: string; limit?: number; cursor?: string; } // 5. Export schemas for runtime validation export { ItemSchema, ItemResponseSchema, ItemListResponseSchema };
typescript// state-manager/api.ts import { ItemListResponseSchema, type ItemListResponse } from "./types"; endpoints: (build) => ({ getItems: build.query<ItemListResponse, GetItemsParams>({ query: (params) => ({ url: "items", params }), transformResponse: (response: unknown) => { // Runtime validation return ItemListResponseSchema.parse(response); }, }), }),
.min(), .max(), .uuid(), .email() for field validationz.enum() for fixed string valuesz.union() for multiple possible types.optional() for nullable fieldsz.infer<typeof Schema>Other measured skills in the registry, with their headline benchmark lift.