Booking backend project · Chapter 10 of 12

Validation and errors

By the end of this chapter you can check every part of a request against zod schemas and report every problem at once, send every error the API makes in one shape (with the right status for bad JSON, a broken database rule or a bug), keep a 500's real message in the server log instead of the client's screen, and write a strict schema that refuses a time without its offset or a price in paise. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Ready for production
  • 3 tasks
  • TypeScript
  • In the all labs plan

The first 2 chapters of Booking backend project are free. This one comes with the all labs plan.

Why you need this

Rukhsana is wiring the React site's sign-up form to POST /auth/signup. Ira's design shows a red line under every field that is wrong. Today the API tells her one problem at a time, so Neha fixes her email, presses Sign up, and only then learns her password is too short. Rukhsana also needs one error shape: her code reads error.code and shows error.message, and any answer that is not in that shape breaks her page.

Asha found worse while scheduling Diwali week. Here is what chapter 9's code does with what she, or a script with a bug, might send:

$ signup, three bad fields
{"error":{"code":"bad_request","message":"Give a valid email address"}} [400]
$ login, bad JSON
{"error":{"code":"internal","message":"Something went wrong on our side"}} [500]
$ showtime 2026-11-08 10:00
{"showtime":{"id":24,"showId":7,"screenId":1,"startsAt":"2026-11-08T10:00:00.000Z","endsAt":"2026-11-08T12:04:00.000Z","prices":{"Front":120,"Middle":180,"Balcony":240},"status":"scheduled"}} [201]
$ showtime tomorrow
{"error":{"code":"internal","message":"Something went wrong on our side"}} [500]
$ showtime price 120.5
{"showtime":{"id":25,"showId":7,"screenId":1,"startsAt":"2026-11-08T12:30:00.000Z","endsAt":"2026-11-08T14:34:00.000Z","prices":{"Front":120.5,"Middle":180,"Balcony":240},"status":"scheduled"}} [201]

Three problems, one reported. A typo in JSON is "our fault", a 500. Asha meant 10:00 am in Pune and got 10:00 UTC, which is 3:30 pm in Pune: the morning show is now in the afternoon. "tomorrow" crashed the insert. And Front seats now cost ₹120.50, in an API where money is whole rupees. None of these is a bug in a service: the input should never have reached it.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    validate.ts: report every problem at once

    Rewrite validate({ body, query, params }) in src/middleware/validate.ts. Today it stops at the first problem with a 400. Make it check every part that has a schema and collect every zod issue of every part, in the order body, query, params, each as { in, path, message }: in is the part (body, query or params), path is the issue's path joined with dots (prices.Front), message is zod's message. If there is any issue, call next with new AppError(422, 'validation_failed', 'Some fields are not right', details), where details is that list; never answer the request yourself. When every part passes, keep today's behaviour: the parsed body replaces req.body, the parsed query and params go to res.locals.query and res.locals.params, then next() with nothing.

    The Check runs your middleware on a request with two problems in the body, one in the query and one in the params (four details, in that order), on a nested price (path prices.Front), and on a good request (zod's parsed values, numbers from the query and params). Then POST /auth/signup with a bad email, an empty name and a short password must answer 422 with three details, and GET /shows?kind=opera a 422 with a detail in the query. The details reach the client only once Task 2's error handler sends them.

  2. Task 2 · interview question

    Interview: error-handler.ts, one error shape

    Write toErrorResponse(err) in src/middleware/error-handler.ts. It returns { status, body } with body { error: { code, message, details? } }: an AppError gives its status, code and message, and details only when the error has details; an error whose type is entity.parse.failed (express.json() could not read the body) gives 400, code bad_json, message The request body is not valid JSON; type entity.too.large gives 413, code too_large, message The request body is too large; Postgres code 23505 (read with pgErrorCode, which also looks at Drizzle's cause) gives 409, code conflict, message That already exists; 23503 gives 409, code conflict, message Something it refers to does not exist, or still uses it; anything else gives 500, code internal, message Something went wrong on our side, never the error's own message.

    Then make errorHandler answer with the status and body toErrorResponse gives, and console.error the error when the status is 500 or more (and only then). notFoundHandler stays as it is.

    The Check calls toErrorResponse with each kind of error (a Drizzle error with the code on its cause, an Error whose message holds a password), calls errorHandler for a 500 and a 422, and sends POST /auth/login with a body that is not valid JSON (400 bad_json) and GET /nowhere (404 in the one shape).

    Nikhil will ask: why must a 500 never send the error's own message to the client, and where does that message go?

  3. Task 3

    showtimes.schemas.ts: a strict showtime

    Rewrite createShowtimeSchema in src/modules/showtimes/showtimes.schemas.ts, keeping its name and the type CreateShowtimeInput. showId and screenId are whole numbers from 1 up; startsAt is a date and time with its offset, such as 2026-11-06T21:30:00+05:30 (Z counts as an offset), so 2026-11-08 10:00, 2026-11-08T10:00:00 and tomorrow are refused; prices is an object of class names (not empty) to whole rupees from 1 to 10000, so 120.5, 0, -5 and 10001 are refused; and any key the schema does not name (venue) is refused.

    The Check parses a good showtime (and the same with Z), then each of those bad values, and wants each refused at its field (startsAt, prices.Front, showId, screenId). Then POST /admin/showtimes as Asha with startsAt 2026-11-08 10:00 must answer 422 with a detail at startsAt, and a good showtime (The Last Show at Moti Mahal, show 7, on screen 1 at 2026-11-08T10:00:00+05:30) 201, starting at 2026-11-08T04:30:00.000Z.

What you will learn

  • zod issues: every problem with a value, with its path and message.
  • Validation middleware that collects them all into one 422.
  • Express error middleware: how errors reach it, and why it answers every error.
  • One error shape for our errors, the body parser's, Postgres's and our bugs.
  • Never leaking a 500: the message goes to the log, a fixed sentence to the client.
  • Strict schemas: integers, ranges, ISO date-times with offsets, no unknown keys.

More in Ready for production

Build "Validation and errors" yourself

Parda sells tickets for Pune's own cinemas and stages. You build its booking API, one module a chapter, and take the whole codebase home. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Booking backend project are free. This one comes with the all labs plan.