بنقرة واحدة
zod
Zod v4 schema validation 最佳實踐指南。當需要定義 schema、驗證/解析 JSON 資料、type inference、或處理 unknown data 時使用。
التثبيت باستخدام Codex أو Claude انسخ هذا Prompt والصقه في Codex أو Claude أو مساعد آخر ليراجع صفحة Skill ويثبّتها لك.
القائمة
Zod v4 schema validation 最佳實踐指南。當需要定義 schema、驗證/解析 JSON 資料、type inference、或處理 unknown data 時使用。
التثبيت باستخدام Codex أو Claude انسخ هذا Prompt والصقه في Codex أو Claude أو مساعد آخر ليراجع صفحة Skill ويثبّتها لك.
استنادا إلى تصنيف SOC المهني
Svelte 5 + Astro 整合最佳實踐指南。當需要建立 Svelte 元件、使用 runes API、整合 Astro islands、或用 Testing Library 測試 Svelte 元件時使用。
GitHub GraphQL API 最佳實踐指南。當需要使用 GraphQL 查詢使用者資料、處理 cursor pagination、計算 rate limit、或除錯 GraphQL errors 時使用。
gayanvoice/top-github-users 架構參考指南。當需要了解 GitHub 使用者排行榜的資料抓取管線、國家設定、排行計算邏輯、已知問題、或社群需求時使用。
Commander.js v14 CLI 框架最佳實踐。當需要建立 CLI 工具、解析命令列參數、設計 subcommands 時使用。
GitHub Actions CI/CD 最佳實踐指南。當需要設定 workflow、cron 排程、GitHub Pages 部署、使用 Octokit API、或處理 rate limiting 時使用。
Globe.GL 3D 地球視覺化指南。當需要建立互動式 3D 地球、國家熱力圖、點擊導航、或整合 Astro 時使用。
| name | zod |
| description | Zod v4 schema validation 最佳實踐指南。當需要定義 schema、驗證/解析 JSON 資料、type inference、或處理 unknown data 時使用。 |
npm install zod # v4 is latest
需要 TypeScript 5.5+ 且 "strict": true。
import { z } from "zod";
z.string() z.number() z.boolean()
z.null() z.undefined() z.unknown()
z.int() z.int32() z.float64()
// 預設 strip unknown keys
const User = z.object({
name: z.string(),
age: z.number(),
});
// 嚴格模式:拒絕 unknown keys
const StrictUser = z.strictObject({ name: z.string() });
// 寬鬆模式:保留 unknown keys
const LooseUser = z.looseObject({ name: z.string() });
User.extend({ email: z.email() });
// 效能更好的寫法:
z.object({ ...User.shape, email: z.email() });
User.pick({ name: true });
User.omit({ age: true });
User.partial();
User.required();
User.keyof();
z.array(z.string())
z.array(z.string()).min(1).max(10)
const Color = z.enum(["red", "green", "blue"]);
// TypeScript enums(v4 新增,取代 z.nativeEnum)
enum Direction { Up, Down }
z.enum(Direction);
z.string().optional() // string | undefined
z.string().nullable() // string | null
z.string().nullish() // string | null | undefined
z.string().default("hi") // 預設值
z.number().catch(0) // parse 失敗時的 fallback
// 失敗時拋出 ZodError
const data = schema.parse(input);
// 不拋錯,回傳 discriminated union
const result = schema.safeParse(input);
if (result.success) {
result.data; // typed
} else {
result.error; // ZodError
}
type User = z.infer<typeof User>; // output type
type UserInput = z.input<typeof User>; // input type(transforms 前)
最佳實踐:從 schema 推導 type(z.infer),不要另外定義 interface — single source of truth。
z.email() z.url() z.uuid()
z.iso.date() z.iso.datetime()
// 舊寫法 z.string().email() 已 deprecated
z.string().transform(val => val.length) // string → number
z.coerce.number() // String(input) → number
z.string().refine(val => val.length <= 255, { error: "Too long" });
// 跨欄位驗證
z.object({
password: z.string(),
confirm: z.string(),
}).refine(d => d.password === d.confirm, {
error: "Passwords don't match",
path: ["confirm"],
});
const Category = z.object({
name: z.string(),
get children() { return z.array(Category); },
});
const UserId = z.string().brand<"UserId">();
type UserId = z.infer<typeof UserId>;
// v3: message / invalid_type_error / required_error(已移除)
// v4:
z.string({ error: "Must be string" });
z.string({ error: (issue) =>
issue.input === undefined ? "Required" : "Invalid"
});
const jsonSchema = z.toJSONSchema(schema);
z.infer 推導.safeParse() 處理不信任的外部資料z.object() 會 strip — 適合清理舊格式資料z.discriminatedUnion()(O(1) lookup){ ...Base.shape } 優於 .extend() chain.nullable() 對應 null,.optional() 對應 undefined