Zod is a TypeScript-first schema validation library. Schemas are both validation rules and type inferrers: z.infer<typeof schema> extracts the TypeScript type. parse() throws ZodError on failure; safeParse() returns {success, data, error}. Transform and refine add custom logic. Coerce auto-converts types (string → number from form inputs). Zod integrates with react-hook-form, trpc, and drizzle for end-to-end type safety.
End-to-end type safety with tRPC and Zod.
Don't write a TypeScript interface AND a Zod schema separately. Define the Zod schema once, derive the type with z.infer<typeof schema>. Changes to the schema automatically update the type.
parse() throws on invalid input — in async Express/Fastify handlers, uncaught throws crash the request. Use safeParse() and return 400 Bad Request with error messages. Reserve parse() for internal trusted data.
URL params and query strings are always strings in Express/Next.js. z.coerce.number() converts '42' → 42 before validation. Without coerce, z.number().parse('42') fails.
Sign in to share your feedback and join the discussion.