InterviewHubPRO
Section Overview
Q45

Question 45 of 60

Part 4: Express, Fastify, APIs & Middleware (36 - 50)

75%
Node.js & Libuv•Part 4: Express, Fastify, APIs & Middleware (36 - 50)

Input Sanitization with Zod.

In Simple Words: Validate request body data with Zod schemas to guarantee type safety and block malformed payloads before they reach business logic.

typescript
import { z } from 'zod';

const UserSchema = z.object({
  email: z.string().email(),
  age: z.number().min(18)
});

const validateUser = (req, res, next) => {
  const result = UserSchema.safeParse(req.body);
  if (!result.success) return res.status(400).json(result.error);
  req.body = result.data;
  next();
};