0
Challenge · 68Architecture & State
Zod / Yup
Ecoused in ~60% of apps
Hand-written validation drifts from your TypeScript types: you fix one and forget the other, then bad data slips through at runtime while the compiler swears everything is fine. A schema fixes both from one definition — it validates the data AND becomes the type.
Core idea. Declare your data's shape once as a Zod schema; call schema.safeParse(input) to validate untrusted data at runtime, and z.infer<typeof schema> to get the matching TypeScript type at compile time — one source of truth for both.
poke it, drag it,
try to break it
try to break it
no slides here.
you drive.
you drive.
Playground, this isn't a screenshot
ONE SOURCE OF TRUTH — schema.ts
const PostSchema = z.object({
title: z.string().min(1, "Title is required").max(60),
body: z.string().min(10, "Write at least 10 characters"),
tag: z.enum(["react", "css", "ts"]),
})LIVE INPUT — validated on every keystroke
invalid
Title is required
valid
valid
PostSchema.safeParse(input) → { success: false }
result.error.issues = [
{ path: ["title"], message: "Title is required" },
]
type Post = z.infer<typeof PostSchema>
type Post = {
title: string
body: string
tag: "react" | "css" | "ts"
}Edit the schema once — both the validator and the type update together.
↑ play a little first, then dig in