AgentSkillsCN

api-schema-zod

在后端与前端共享可复用的 API 合约时,可生成 Zod 验证模式。适用于构建类型安全的模式,或在不同项目间共享验证规则。

SKILL.md
--- frontmatter
name: api-schema-zod
description: Generate Zod validation schemas for reusable API contracts shared between backend and frontend. Use when creating type-safe schemas or sharing validation between projects.
allowed-tools:
  - Read
  - Write
  - Edit
  - Glob
  - Grep

Zod Schemas

Purpose

Generate Zod validation schemas that can be used both on the backend for validation and shared with the frontend for type-safe API contracts.

When to Use

  • Creating reusable validation schemas
  • Sharing schemas between backend and frontend
  • Type-safe API contracts
  • Runtime validation with TypeScript inference

What It Generates

Directory Structure

code
packages/shared-schemas/src/schemas/{feature}/
├── {entity}.schema.ts
├── index.ts

Patterns Enforced

Schema Reusability

  • Common schemas (email, password, UUID) imported from shared
  • Schemas composable via .pick(), .omit(), .extend()
  • Transformations for data normalization

Type Inference

  • z.infer<typeof schema> for TypeScript types
  • Types can be exported to frontend

Error Messages

  • Custom validation messages
  • User-friendly error descriptions

Usage Example

bash
/skill schema-zod --name=User --fields='email:email,password:password,name:string,isActive:boolean'

Related Files