PATTERN08 Jul 2026·1 min read
Zod as the single source of the contract: parse, not validate
For a long time I kept three versions of the same contract: the TypeScript interface, the request validator and the DTO consumed by the front-end. Every field change was a treasure hunt — and the drift between them was the #1 cause of silly bugs in production.
schemas/user.ts
undefinedThe key shift is treating the schema as the single source: the type is inferred from it, never written by hand. And in the handler, parse — not validate. Parse returns a new value, typed and clean; validating and keeping the original object lets extra fields and silent coercion slip through.
routes/users.ts
undefinedBonus: publish the schemas in a shared package and the front validates forms with exactly the same contract — React Hook Form + zodResolver close the loop.
← back to blogBernardo Filipe · 08 Jul 2026