global-validation

majiayu000 的头像作者 majiayu000

使用 Zod 模式实现全面的验证,在客户端和服务器上进行类型安全验证,并将服务器端验证作为安全边界(永远不要信任客户端输入)。在验证用户输入、创建接受数据的 API 端点、实施表单、定义数据架构、验证文件上传、创建验证中间件、实施 Firestore 安全规则或 Supabase RLS 或为任何用户提供的数据编写验证规则时,请使用此技能。在处理 API 路由处理程序、使用 React Hook Form 的表单组件、验证中间件、Zod 架构定义(schemas/*.ts、validation/*.ts)、Firestore 安全规则(firestore.rules)、Supabase RLS 策略或任何接受外部输入的代码时应用。此技能始终确保服务器端验证(客户端仅用于 UX),Zod 使用 TypeScript 类型推断(z.infer<typeof schema>)进行模式验证,用于 Express/Bun API 的验证中间件工厂,用于表单的 React Hook Form + zodResolver,用户友好的错误

📖 中文导读:使用 Zod 模式实现全面的验证,在客户端和服务器上进行类型安全验证,并将服务器端验证作为安全边界(永远不要信任客户端输入)。在验证用户输入、创建接受数据的 API 端点、实施表单、定义数据架构、验证文件上传、创建验证中间件、实施 Firestore 安全规则或 Supabase RLS 或为任何用户提供的数据编写验证规则时,请使用此技能。在处理 API 路由处理程序、使用 React Hook Form 的表单组件、验证中间件、Zod 架构定义(schemas/*.ts、validation/*.ts)、Firestore 安全规则(firestore.rules)、Supabase RLS 策略或任何接受外部输入的代码时应用。此技能始终确保服务器端验证(客户端仅用于 UX),Zod 使用 TypeScript 类型推断(z.infer<typeof schema>)进行模式验证,用于 Express/Bun API 的验证中间件工厂,用于表单的 React Hook Form + zodResolver,用户友好的错误 以下正文为该技能的原始 SKILL.md 内容(英文),上方安装命令可直接使用。

name: Global Validation description: Implement comprehensive validation using Zod schemas for type-safe validation on both client and server, with server-side validation as the security boundary (never trust client input). Use this skill when validating user inputs, creating API endpoints that accept data, implementing forms, defining data schemas, validating file uploads, creating validation middleware, implementing Firestore security rules or Supabase RLS, or writing validation rules for any user-provided data. Apply when working on API route handlers, form components with React Hook Form, validation middleware, Zod schema definitions (schemas/.ts, validation/.ts), Firestore security rules (firestore.rules), Supabase RLS policies, or any code that accepts external input. This skill ensures server-side validation always (client-side is for UX only), Zod for schema validation with TypeScript type inference (z.infer), validation middleware factory for Express/Bun APIs, React Hook Form + zodResolver for forms, user-friendly error messages (not technical jargon), input sanitization with DOMPurify for HTML content, file upload validation (type whitelist, size limits with multer), Firestore security rules with data type and length validation, FluentValidation for .NET APIs, database-level constraints enforcement, fail-early validation principles, and environment variable validation with Zod on application startup.

Global Validation

When to use this skill:

  • When creating API endpoints that accept request bodies
  • When implementing validation middleware factory for Express/Bun/Hono
  • When defining Zod schemas for data validation with type inference
  • When creating forms with React Hook Form and zodResolver
  • When validating file uploads (MIME type whitelist, size limits with multer)
  • When writing Firestore security rules (firestore.rules) for database validation
  • When writing Supabase Row Level Security (RLS) policies
  • When implementing input sanitization with DOMPurify to prevent XSS
  • When adding validation to service layer methods (fail-early)
  • When creating custom Zod refinements (e.g., password complexity) or transforms (e.g., string to Date)
  • When writing user-friendly validation error messages (not technical jargon)
  • When validating environment variables with Zod on application startup
  • When working on any code that accepts user input or external data
  • When implementing FluentValidation for .NET API validation
  • When creating complex nested schemas with arrays and optional fields

This Skill provides Claude Code with specific guidance on how to adhere to coding standards as they relate to how it should handle global validation.

Instructions

For details, refer to the information provided in this file: global validation