Backend Development
Step 8 / 2090 min

Validation & Error Handling

APIs

Explanation

Reject bad input with Zod (or similar) and return consistent error JSON instead of crashing the process.

Code example

typescript
import { z } from "zod";

const createSkillSchema = z.object({
  name: z.string().trim().min(2).max(80),
});

app.post("/api/skills", (req, res) => {
  const parsed = createSkillSchema.safeParse(req.body);
  if (!parsed.success) {
    return res.status(400).json({ error: parsed.error.issues[0]?.message });
  }
  res.status(201).json(parsed.data);
});

Helpful resources

Exercise

Add Zod validation and a global error middleware that maps known errors to 400/401/404/500.