zod-validation-expert – skill Lập trình cho agent AI
Chuyên gia về Zod — thư viện kiểm tra schema ưu tiên TypeScript. Bao gồm parse, lỗi tùy chỉnh, refinement, suy luận kiểu và tích hợp với React Hook Form, Next.js và tRPC.
Cài skill zod-validation-expert bằng một lệnh
Chọn công cụ bạn đang dùng, copy lệnh và dán vào terminal. Skill sẽ nằm trong thư mục skills của công cụ đó và sẵn sàng dùng ở phiên làm việc tiếp theo.
Claude Code
mkdir -p ~/.claude/skills && curl -fsSL https://skill.dn1nguoi.com/z/zod-validation-expert-ce3bf.tgz | tar xz -C ~/.claude/skills
Gọi trong Claude Code: /zod-validation-expert – rồi mô tả công việc bạn muốn làm.
Codex
mkdir -p ~/.codex/skills && curl -fsSL https://skill.dn1nguoi.com/z/zod-validation-expert-ce3bf.tgz | tar xz -C ~/.codex/skills
Gọi trong Codex: $zod-validation-expert – rồi mô tả công việc bạn muốn làm.
Hermes Agent
mkdir -p ~/.hermes/skills && curl -fsSL https://skill.dn1nguoi.com/z/zod-validation-expert-ce3bf.tgz | tar xz -C ~/.hermes/skills
Gọi trong Hermes Agent: /zod-validation-expert – rồi mô tả công việc bạn muốn làm.
DeepSeek
mkdir -p ~/.deepcode/skills && curl -fsSL https://skill.dn1nguoi.com/z/zod-validation-expert-ce3bf.tgz | tar xz -C ~/.deepcode/skills
Gọi trong DeepSeek: /zod-validation-expert – rồi mô tả công việc bạn muốn làm.
Nội dung SKILL.md
Nội dung SKILL.md gốc bằng tiếng Anh.
Zod Validation Expert
You are a production-grade Zod expert. You help developers build type-safe schema definitions and validation logic. You master Zod fundamentals (primitives, objects, arrays, records), type inference (z.infer), complex validations (.refine, .superRefine), transformations (.transform), and integrations across the modern TypeScript ecosystem (React Hook Form, Next.js API Routes / App Router Actions, tRPC, and environment variables).
When to Use This Skill
- Use when defining TypeScript validation schemas for API inputs or forms
- Use when setting up environment variable validation (
process.env) - Use when integrating Zod with React Hook Form (
@hookform/resolvers/zod) - Use when extracting or inferring TypeScript types from runtime validation schemas
- Use when writing complex validation rules (e.g., cross-field validation, async validation)
- Use when transforming input data (e.g., string to Date, string to number coercion)
- Use when standardizing error message formatting
Version and authorization scope
The error-map/formatting examples use Zod 3 conventions; verify the installed major version and its migration guide before copying them into Zod 4. Parsing input does not authenticate the caller or authorize fields such as role/published; enforce those separately on the server and bound input sizes. z.coerce.boolean() uses JavaScript truthiness: the nonempty string "false" becomes true. See [Zod coercion](https://zod.dev/api?id=coercion).
Core Concepts
Why Zod?
Zod eliminates the duplication of writing a TypeScript interface *and* a runtime validation schema. You define the schema once, and Zod infers the static TypeScript type. Note that Zod is for parsing, not just validation. safeParse and parse return clean, typed data, stripping out unknown keys by default.
Schema Definition & Inference
Primitives & Coercion
import { z } from "zod";
// Basic primitives
const stringSchema = z.string().min(3).max(255);
const numberSchema = z.number().int().positive();
const dateSchema = z.date();
// Coercion (automatically casting inputs before validation)
// Highly useful for FormData in Next.js Server Actions or URL queries
const ageSchema = z.coerce.number().min(18); // "18" -> 18
const activeSchema = z.coerce.boolean(); // "true" -> true
const dobSchema = z.coerce.date(); // "2020-01-01" -> Date objectObjects & Type Inference
const UserSchema = z.object({
id: z.string().uuid(),
username: z.string().min(3).max(20),
email: z.string().email(),
role: z.enum(["ADMIN", "USER", "GUEST"]).default("USER"),
age: z.number().min(18).optional(), // Can be omitted
website: z.string().url().nullable(), // Can be null
tags: z.array(z.string()).min(1), // Array with at least 1 item
});
// Infer the TypeScript type directly from the schema
// No need to write a separate `interface User { ... }`
export type User = z.infer<typeof UserSchema>;Advanced Types
// Records (Objects with dynamic keys but specific value types)
const envSchema = z.record(z.string(), z.string()); // Record<string, string>
// Unions (OR)
const idSchema = z.union([z.string(), z.number()]); // string | number
// Or simpler:
const idSchema2 = z.string().or(z.number());
// Discriminated Unions (Type-safe switch cases)
const ActionSchema = z.discriminatedUnion("type", [
z.object({ type: z.literal("create"), id: z.string() }),
z.object({ type: z.literal("update"), id: z.string(), data: z.any() }),
z.object({ type: z.literal("delete"), id: z.string() }),
]);Parsing & Validation
parse vs safeParse
const schema = z.string().email();
// ❌ parse: Throws a ZodError if validation fails
try {
const email = schema.parse("invalid-email");
} catch (err) {
if (err instanceof z.ZodError) {
console.error(err.issues);
}
}
// ✅ safeParse: Returns a result object (No try/catch needed)
const result = schema.safeParse("[email protected]");
if (!result.success) {
// TypeScript narrows result to SafeParseError
console.log(result.error.format());
// Early return or throw domain error
} else {
// TypeScript narrows result to SafeParseSuccess
const validEmail = result.data; // Type is `string`
}Customizing Validation
Custom Error Messages
const passwordSchema = z.string()
.min(8, { message: "Password must be at least 8 characters long" })
.max(100, { message: "Password is too long" })
.regex(/[A-Z]/, { message: "Password must contain at least one uppercase letter" })
.regex(/[0-9]/, { message: "Password must contain at least one number" });
// Global custom error map (useful for i18n)
z.setErrorMap((issue, ctx) => {
if (issue.code === z.ZodIssueCode.invalid_type) {
if (issue.expected === "string") return { message: "This field must be text" };
}
return { message: ctx.defaultError };
});Refinements (Custom Logic)
// Basic refinement
const passwordCheck = z.string().refine((val) => val !== "password123", {
message: "Password is too weak",
});
// Cross-field validation (e.g., password matching)
const formSchema = z.object({
password: z.string().min(8),
confirmPassword: z.string()
}).refine((data) => data.password === data.confirmPassword, {
message: "Passwords don't match",
path: ["confirmPassword"], // Sets the error on the specific field
});Transformations
// Change data during parsing
const stringToNumber = z.string()
.transform((val) => parseInt(val, 10))
.refine((val) => !isNaN(val), { message: "Not a valid integer" });
// Now the inferred type is `number`, not `string`!
type TransformedResult = z.infer<typeof stringToNumber>; // numberIntegration Patterns
React Hook Form
import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { z } from "zod";
const loginSchema = z.object({
email: z.string().email("Invalid email address"),
password: z.string().min(6, "Password must be 6+ characters"),
});
type LoginFormValues = z.infer<typeof loginSchema>;
export function LoginForm() {
const { register, handleSubmit, formState: { errors } } = useForm<LoginFormValues>({
resolver: zodResolver(loginSchema)
});
const onSubmit = (data: LoginFormValues) => {
// data is fully typed and validated
// Pass validated credentials to the authorized login operation; never log passwords.
};
return (
<form onSubmit={handleSubmit(onSubmit)}>
<input {...register("email")} />
{errors.email && <span>{errors.email.message}</span>}
{/* ... */}
</form>
);
}Next.js Server Actions
"use server";
import { z } from "zod";
// FormData values can be strings or Files. Validate each expected field explicitly.
const createPostSchema = z.object({
title: z.string().min(3),
content: z.string().optional(),
published: z.enum(["on"]).optional().transform(value => value === "on"), // unchecked => false
});
export async function createPost(prevState: any, formData: FormData) {
// Convert FormData to standard object using Object.fromEntries
const rawData = Object.fromEntries(formData.entries());
const validatedFields = createPostSchema.safeParse(rawData);
if (!validatedFields.success) {
return {
errors: validatedFields.error.flatten().fieldErrors,
};
}
// Proceed with validated database operation
const { title, content, published } = validatedFields.data;
// ...
return { success: true };
}Environment Variables
// Make environment variables strictly typed and fail-fast
import { z } from "zod";
const envSchema = z.object({
DATABASE_URL: z.string().url(),
NODE_ENV: z.enum(["development", "test", "production"]).default("development"),
PORT: z.coerce.number().default(3000),
API_KEY: z.string().min(10),
});
// Fails the build immediately if env vars are missing or invalid
const env = envSchema.parse(process.env);
export default env;Best Practices
- ✅ Do: Co-locate schemas alongside the components or API routes that use them to maintain separation of concerns.
- ✅ Do: Use
z.infer<typeof Schema>everywhere instead of maintaining duplicate TypeScript interfaces manually. - ✅ Do: Prefer
safeParseoverparseto avoid scatteredtry/catchblocks and leverage TypeScript's control flow narrowing for robust error handling. - ✅ Do: Use
z.coercewhen accepting data fromURLSearchParamsorFormData, and be aware thatz.coerce.boolean()converts standard"false"/"off"strings unexpectedly without custom preprocessing. - ✅ Do: Use
.flatten()or.format()onZodErrorobjects to easily extract serializable, human-readable errors for frontend consumption. - ❌ Don't: Rely exclusively on
.partial()for update schemas if field types or constraints differ between creati
… (nội dung đầy đủ có trong gói tải về)
Skill liên quan
- zustand-store-ts – Tạo store Zustand theo các mẫu đã thiết lập, với kiểu TypeScript và middleware phù hợp.
- ab-testing – Khi người dùng muốn lên kế hoạch, thiết kế hoặc triển khai A/B test hoặc thử nghiệm…
- adaptyv – Dùng Adaptyv Bio Foundry API và Python SDK để thiết kế thí nghiệm mô tả protein, ước…
- ab-test-setup – Khi người dùng muốn lên kế hoạch, thiết kế hoặc triển khai A/B test hoặc thử nghiệm…
- access-lifecycle – Dùng khi chuẩn bị hoặc chạy đợt rà soát quyền truy cập người dùng, kiểm tra người đã…
- aspnet-core – Xây dựng, review, tái cấu trúc hoặc thiết kế kiến trúc ứng dụng web ASP.NET Core theo…
- deepcode-self-refer – Trả lời câu hỏi về chính Deep Code CLI, bao gồm tính năng, tùy chọn cấu hình, lệnh…
- api-noauth-hunt – Dùng khi một API có thể để lộ dữ liệu hoặc thao tác đặc quyền mà không cần xác thực.