Stop Using Repetitive Validation Logic – Embrace Zod
Nurul Islam Rimon

Nurul Islam Rimon @nurulislamrimon

About: Thanks for being here! I'm Nurul Islam Rimon, a MERN & PERN stack developer. I'm a student of competitive programming. I love to explore new technology.

Location:
Chaprashirhat, Kabirhat, Noakhali
Joined:
Jan 20, 2024

Stop Using Repetitive Validation Logic – Embrace Zod

Publish Date: May 11
0 0

If you're a backend or fullstack developer, you've probably written this kind of code more times than you'd like to admit:

if (!req.body.email || !req.body.password) {
  return res.status(400).json({ message: 'Missing fields' });
}
Enter fullscreen mode Exit fullscreen mode

It works... but it gets messy really fast.
As your project grows, repeating this kind of manual validation on every route becomes painful and error-prone.

Let me introduce you to something better.

✅ Meet Zod – The Cleaner Way to Validate

Zod is a TypeScript-first schema validation library. It helps you define what your data should look like and automatically validates it.

No more writing if (!email) and if (typeof password !== 'string') every time.

🔧 Example: Login Request Validation
Let’s take a simple login API example.

❌ Without Zod:

app.post('/login', (req, res) => {
  const { email, password } = req.body;

  if (!email || !password) {
    return res.status(400).json({ message: 'Missing email or password' });
  }

  if (typeof email !== 'string' || typeof password !== 'string') {
    return res.status(400).json({ message: 'Invalid input types' });
  }

  // Continue with login logic...
});
Enter fullscreen mode Exit fullscreen mode

This is already ugly — and we’ve just started.

✅ With Zod:

import { z } from 'zod';

const loginSchema = z.object({
  email: z.string().email(),
  password: z.string().min(6),
});

app.post('/login', (req, res) => {
  const result = loginSchema.safeParse(req.body);

  if (!result.success) {
    return res.status(400).json({ message: 'Invalid input', errors: result.error.format() });
  }

  const { email, password } = result.data;

  // Continue with login logic...
});
Enter fullscreen mode Exit fullscreen mode

Much cleaner. More reliable. Fully typed.

🔁 Reusability Is a Bonus
Once you define a schema, you can reuse it anywhere: routes, services, unit tests, even the frontend.

// validators/user.ts
export const registerSchema = z.object({
  name: z.string().min(1),
  email: z.string().email(),
  password: z.string().min(6),
});
Enter fullscreen mode Exit fullscreen mode

Then use it wherever you need:

import { registerSchema } from './validators/user';

const result = registerSchema.safeParse(req.body);
Enter fullscreen mode Exit fullscreen mode

No more duplicate validation logic across your app.

💥 Bonus: It Works with TypeScript Seamlessly
Zod works great with TypeScript out of the box.

type LoginInput = z.infer<typeof loginSchema>;
Enter fullscreen mode Exit fullscreen mode

You get type-safety and runtime validation. No extra setup needed.

So,
If you’re tired of repeating the same if (!value) checks and want your code to look cleaner, safer, and more modern — try Zod.
It’s simple to use, works beautifully with TypeScript, and helps you write better backend logic faster.

Let your validation work for you, not against you.

Thanks

Comments 0 total

    Add comment