Runtime validation for request bodies (Zod)

2395
0

TypeScript only protects you at compile time; your API still receives untrusted JSON from the internet. I lean on Zod as the source of truth for parsing + validation so runtime and types stay aligned. The big win is that I don’t try to validate ‘everything’—I validate what I actually use, and I coerce where it’s pragmatic (like turning query strings into numbers). When validation fails, I return a consistent 400 shape (error + issues) so the frontend can render field errors without special casing. This has saved me from subtle bugs during refactors, and it makes versioned endpoints safer because unexpected payloads fail loudly instead of turning into undefined behavior.