In web development, there is one golden rule: Never trust user input. Whether it is a login form, a search bar, or an environment variable, unvalidated data is a leading cause of bugs and security vulnerabilities.
For a long time, developers relied on manual if/else checks or complex Regex to validate data. But then came Zod .
The Problem: Manual Validation Hell
Imagine you have an endpoint that receives a user profile. Without a schema validator, your code becomes cluttered and hard to maintain:
// Manual validation is messy and error-prone app . post ( " /profile " , ( request , reply ) => { const { username , age , email } = request . body ;
// Manual type and existence checks if ( ! username || typeof username !== ' string ' ) { return reply . status ( 400 ). send ( " Invalid username " ); }
if ( age && typeof age !== ' number ' ) { return reply . status ( 400 ). send ( " Age must be a number " ); }
if ( ! email || ! email . includes ( ' @ ' )) { return reply . status ( 400 ).…