Exercise 2: Zod Rejecting a Malformed Request — Possible Solution ==================================================================== FULL SETUP ------------------------------ // server.js app.use(express.json()); // routes/admin.js const { z } = require('zod'); const titleSchema = z.object({ title: z.string().min(1).max(255), }); app.post('/api/pages/:id/title', async (req, res) => { const result = titleSchema.safeParse(req.body); if (!result.success) { return res.status(400).json({ error: result.error.flatten() }); } await pool.query('UPDATE pages SET title = ? WHERE id = ?', [result.data.title, req.params.id]); res.json({ status: 'ok' }); }); SENDING { title: 12345 } ------------------------------ With express.json() now registered, req.body correctly becomes { title: 12345 } - a real parsed object. titleSchema.safeParse() then rejects it outright, since z.string() doesn't accept a number, returning { success: false, ... }. The endpoint responds with a 400 status and a real validation error, and the database update is never reached. WHY THIS WORKS AS AN ANSWER ------------------------------ It correctly shows express.json() populating req.body correctly, and correctly confirms the Zod schema rejects the same malformed request from Exercise 1's own scenario before it can reach the database.