Zodデータの変換と詳細な検証
基本的なバリデーションの先へ進み、解析した値を変換し、Zodでカスタムの詳細な検証ルールを追加します。
「Zodデータの変換と詳細な検証」はCoddyKit上の無料tRPC End-to-End Type Safe APIsレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これは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.
よくある質問
「Zodデータの変換と詳細な検証」レッスンは無料ですか?
はい。「Zodデータの変換と詳細な検証」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、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を演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
tRPC End-to-End Type Safe APIsを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのtRPC End-to-End Type Safe APIsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「Zodデータの変換と詳細な検証」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このtRPC End-to-End Type Safe APIsレッスンでコードを書いて実行できますか?
はい。すべてのtRPC End-to-End Type Safe APIsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- Zodスキーマ入門
- 複雑なZodスキーマの定義
- tRPCプロシージャへのZod統合
- Zodデータの変換と詳細な検証