การแปลงและปรับแต่งข้อมูล Zod
ก้าวไปไกลกว่าการตรวจสอบพื้นฐานด้วยการแปลงค่าที่แยกวิเคราะห์แล้วและเพิ่มกฎการปรับแต่งแบบกำหนดเองด้วย Zod
การแปลงและปรับแต่งข้อมูล Zod เป็นบทเรียน tRPC End-to-End Type Safe APIs ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน tRPC End-to-End Type Safe APIs และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส tRPC End-to-End Type Safe APIs มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Beyond Pass/Fail
Zod does more than accept or reject data. It can transform valid input into a cleaner shape and apply custom rules that built-in validators cannot express.
The transform Method
.transform() changes a value after it passes validation, producing a new output type.
const trimmed = z.string().transform((s) => s.trim());
trimmed.parse(" hi "); // "hi"Coercing Types
Zod can coerce inputs, useful for query strings that arrive as text but should be numbers.
const page = z.coerce.number().int().positive();
page.parse("5"); // 5 as a numberDefault Values
Provide a fallback when a field is missing.
const schema = z.object({
limit: z.number().default(20),
});
schema.parse({}); // { limit: 20 }Custom Refinements
.refine() adds a custom boolean check with a message when it fails.
const password = z.string().refine(
(val) => val.length >= 8,
{ message: "Too short" }
);Cross-Field Validation
Refine an object to compare two fields, like confirming a password.
const form = z.object({
pw: z.string(),
confirm: z.string(),
}).refine((d) => d.pw === d.confirm, {
message: "Passwords must match",
path: ["confirm"],
});superRefine for Multiple Errors
.superRefine() lets you push several issues in one pass for richer validation.
const s = z.string().superRefine((val, ctx) => {
if (!/[A-Z]/.test(val)) ctx.addIssue({ code: "custom", message: "Need uppercase" });
if (!/[0-9]/.test(val)) ctx.addIssue({ code: "custom", message: "Need digit" });
});Chaining Transforms
Validation and transformation chain in order.
const slug = z.string()
.min(1)
.transform((s) => s.toLowerCase().replace(/\s+/g, "-"));
slug.parse("Hello World"); // "hello-world"Input vs Output Types
After a transform, the input type and output type differ. Use z.input and z.output to read each.
type In = z.input<typeof page>; // string | number
type Out = z.output<typeof page>; // numberSafe Parsing
Use safeParse to get a result object instead of throwing, ideal for handling errors gracefully.
const r = password.safeParse("short");
if (!r.success) console.log(r.error.issues);Pipe for Validate-then-Transform
Use .pipe() to first coerce or transform a value and then run further validation on the result.
const id = z.string().transform(Number).pipe(z.number().int());Quick Check
Test your Zod knowledge.
Recap
You leveled up your Zod schemas:
- transform and coerce reshape valid data
- refine / superRefine add custom and cross-field rules
- safeParse handles errors without throwing
These tools turn Zod into a powerful data shaping and validation layer.
เรียนรู้ tRPC End-to-End Type Safe APIs ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 10
- บทเรียน
- 40
คำถามที่พบบ่อย
บทเรียน “การแปลงและปรับแต่งข้อมูล Zod” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การแปลงและปรับแต่งข้อมูล Zod” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส tRPC End-to-End Type Safe APIs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส tRPC End-to-End Type Safe APIs มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การแปลงและปรับแต่งข้อมูล Zod”
ก้าวไปไกลกว่าการตรวจสอบพื้นฐานด้วยการแปลงค่าที่แยกวิเคราะห์แล้วและเพิ่มกฎการปรับแต่งแบบกำหนดเองด้วย Zod คุณปฏิบัติ tRPC End-to-End Type Safe APIs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน tRPC End-to-End Type Safe APIs หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน tRPC End-to-End Type Safe APIs บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การแปลงและปรับแต่งข้อมูล Zod” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน tRPC End-to-End Type Safe APIs นี้ได้ไหม
ได้ บทเรียน tRPC End-to-End Type Safe APIs ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- รู้จักสคีมา Zod
- การกำหนดสคีมา Zod ที่ซับซ้อน
- การผสานรวม Zod ในกระบวนงาน tRPC
- การแปลงและปรับแต่งข้อมูล Zod