zod/valibot schemas + inference
Validate untrusted data with schemas; infer TS types directly from validators; compare zod and valibot; wire into handlers.
zod/valibot schemas + inference is a free TypeScript Academy lesson on CoddyKit — lesson 1 of 2. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 2 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Intro
Goal: Treat external input as unknown, validate with a schema, and infer TS types from that schema. You will use zod, see valibot syntax, and make a tiny helper for handlers.
- One source of truth
- safeParse vs parse
- Reuse inferred types
zod infer
Define a schema once. type User = z.infer<typeof userSchema> mirrors validation rules exactly.
import { z } from "zod"
export const userSchema = z.object({
id: z.string().uuid(),
name: z.string().min(1),
age: z.number().int().min(0).optional()
})
export type User = z.infer<typeof userSchema>
// User is kept in sync with runtime rules.safeParse vs parse
safeParse gives a success/failed result without throwing, perfect for HTTP handlers; parse throws on invalid input.
import type { Request, Response } from "express"
import { userSchema, type User } from "./schema"
export function createUser(req: Request, res: Response<User | { error: string; issues?: unknown }>) {
const r = userSchema.safeParse(req.body)
if (!r.success) {
return res.status(400).json({ error: "Invalid body", issues: r.error.format() })
}
const user = r.data
return res.status(201).json(user)
}
// parse() throws; safeParse() returns a discriminated unionvalibot infer
valibot offers a compact validator with Infer<typeof schema>. Pick one library and standardize across the team.
import { object, string, number, optional, minLength, uuid, minValue } from "valibot"
import type { Infer } from "valibot"
export const VUser = object({
id: string([uuid()]),
name: string([minLength(1)]),
age: optional(number([minValue(0)]))
})
export type VUserT = Infer<typeof VUser>
// Similar idea: one schema, inferred type.Validation helper
Wrap parsing in a tiny helper so handlers get a simple { ok,data } vs { ok:false,issues } structure.
import { ZodTypeAny } from "zod"
export function validate<T extends ZodTypeAny, Out = unknown>(schema: T, value: unknown, onError?: (issues: unknown) => never | void): { ok: true; data: Out } | { ok: false; issues: unknown } {
const r = schema.safeParse(value)
if (!r.success) {
const issues = r.error.format()
if (onError) onError(issues as unknown as never)
return { ok: false, issues }
}
return { ok: true, data: r.data as unknown as Out }
}Tips
Tips:
- Keep schemas near the boundary (HTTP, CLI, DB IO).
- Infer types and export them—avoid hand-written duplicates.
- Return 400 with issue details; never trust client input.
Inference benefit check
Quick check: Why infer types from schemas?
Recap
Recap: Validate unknown input with zod/valibot, use safeParse, and infer types to keep compile-time and runtime aligned.
Frequently asked questions
Is the “zod/valibot schemas + inference” lesson free?
Yes — the full text of “zod/valibot schemas + inference” is free to read here on the web, and the TypeScript Academy course includes 2 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “zod/valibot schemas + inference”?
Validate untrusted data with schemas; infer TS types directly from validators; compare zod and valibot; wire into handlers. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start TypeScript Academy?
No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 2, so you can start here or from the beginning and move at your own pace.
How long does the “zod/valibot schemas + inference” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this TypeScript Academy lesson?
Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- zod/valibot schemas + inference
- Defensive parsing and error envelopes