designing-zod-schemas

Design Zod schemas for runtime validation and TypeScript type inference.

29|5|Updated Jan 21, 2025
One-click install
npx skills add https://github.com/saleor/configurator --skill designing-zod-schemas
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: designing-zod-schemas
Source: https://github.com/saleor/configurator/tree/main/.claude/skills/designing-zod-schemas
Command: npx skills add https://github.com/saleor/configurator --skill designing-zod-schemas

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill guides the design and implementation of Zod schemas following a Zod-first development approach, enabling robust, type-safe data validation and reusable schema patterns.

Core Features & Use Cases

  • Zod-First Development: Define the schema as the single source of truth, infer TS types with z.infer, and reuse the same schema for runtime validation and tests.
  • Schema Patterns: Branded types, discriminated unions, transforms, and refinements for scalable validation.
  • Testing & Documentation: Promote safe parsing and provide patterns for test data builders and validation tests.

Quick Start

Define a new Zod schema in your project (e.g., a user config object). Infer its TypeScript type using z.infer and validate runtime data with safeParse; add tests to verify validation behavior.

Frequently Asked Questions about designing-zod-schemas

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I validate data at runtime with TypeScript type safety?

Zod schemas provide runtime validation while inferring TypeScript types automatically. Define a schema, use z.infer to extract its type, and validate incoming data with safeParse to catch errors at runtime while maintaining type consistency across your application.

What's the best way to handle discriminated unions in validation?

Zod discriminated unions let you define schemas where a specific field determines the structure of the rest. Use z.discriminatedUnion to create type-safe validators for tagged data, ensuring only valid shape combinations pass validation while TypeScript narrows types correctly.

Can I transform and refine data during validation?

Zod transforms and refinements let you modify or validate data during parsing. Use .transform to reshape values and .refine or .superRefine to add custom validation logic, enabling complex validation workflows within a single schema without separate processing steps.

How do I use Zod schemas as the source of truth for my types?

In a Zod-first workflow, define your schema once, then extract TypeScript types with z.infer. The same schema validates runtime data and generates types, eliminating duplication and ensuring validation rules and type definitions stay synchronized throughout your codebase.

How do I test validation behavior with Zod schemas?

Create test data builders using your schemas and use safeParse in tests to verify validation passes and fails as expected. This pattern documents validation constraints, catches regressions, and ensures schema behavior matches your domain requirements.

What are branded types and when should I use them?

Branded types add semantic meaning to primitives at compile time, preventing invalid values from entering your domain model. Zod supports branded types through refinements, letting you encode constraints like valid email formats or positive numbers directly in your type system.