Create Zod Validation Schemas

backend
TypeScript
code_review
strict_senior
Remix

Generate type-safe Zod schemas for forms, API endpoints, and environment variables.

12/8/2025

Prompt

Create comprehensive Zod validation schemas for [APPLICATION_NAME] with the following specifications:

Requirements

  • Application name: [APPLICATION_NAME]
  • Entities to validate: [ENTITY_1], [ENTITY_2], [ENTITY_3]
  • Form validation needs: [FORM_1], [FORM_2]
  • API endpoints to validate: [ENDPOINT_1], [ENDPOINT_2], [ENDPOINT_3]
  • Environment variables: [ENV_VAR_1], [ENV_VAR_2], [ENV_VAR_3]
  • Custom validation rules: [RULE_1], [RULE_2]
  • Error message customization: [YES/NO]
  • Transformation requirements: [STRING_TO_NUMBER/TRIM/LOWERCASE/etc.]

Deliverables

Generate complete Zod validation code with:

Entity schemas for each [ENTITY]:

  • z.object() definition with all fields
  • Field types (string, number, boolean, date, etc.)
  • Validation constraints (min, max, email, url, regex)
  • Optional vs required fields
  • TypeScript type inference with z.infer<>
  • Custom error messages for each field

Form validation schemas for [FORMS]:

  • Complete form field definitions
  • Custom validation messages
  • Default values where appropriate
  • Nested object validation
  • Array validation with min/max items
  • Conditional field validation with .refine()
  • Cross-field validation rules

API endpoint validation for [ENDPOINTS]:

  • Request body schema
  • Query parameter schema with coercion
  • Path parameter schema
  • Integration with Express/Fastify/Next.js handlers
  • safeParse() usage for error handling
  • Error response formatting with flatten()

Environment variable validation:

  • z.object() schema for all [ENV_VARS]
  • Type coercion for numbers and booleans
  • Default values for optional variables
  • URL, email, and custom format validation
  • Runtime environment validation
  • Type-safe environment object export

Complex validation patterns:

  • Nested object schemas
  • Array schemas with complex items
  • Union types with z.union() or z.discriminatedUnion()
  • Intersection types with z.intersection()
  • Recursive schemas if needed
  • Custom refinements for [CUSTOM_RULES]

Data transformations:

  • String trimming and normalization
  • Type coercion (string to number, etc.)
  • Date parsing and formatting
  • Conditional transformations with .transform()

Error handling utilities:

  • Function to format Zod errors for API responses
  • Function to convert errors for form libraries
  • User-friendly error message mapping

Output production-ready, type-safe validation schemas ready to integrate.

Tags

zod
validation
typescript
type-safety

Tested Models

gpt-4
claude-3-5-sonnet

Comments (0)

Sign in to leave a comment

Sign In