Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Validation, Errors and Middleware in Express (Summary)

In this course, you set out to guard an Express API with middleware and Zod schemas, so every request is checked at the boundary, and to turn every failure into a clear, consistent error response with the right status code. Crux Corner’s wall API now does both.

Three Requests, Three Refusals

In the first lesson, the trusting API stored a V14, a problem set next year, and a name made of three spaces, each with 201 Created. Send the same three requests to the finished API, and each gets a 422 in the one error shape. Its details names the field that broke Ines’s rules: the grade, the date, and the name. Here’s the first:

Language: Shell
$ curl -i -H "Content-Type: application/json" \
  -d '{"name":"Sky Ladder","colour":"black","grade":"V14","setOn":"2026-11-26"}' \
  http://localhost:3000/problems
HTTP/1.1 422 Unprocessable Entity
X-Powered-By: Express
X-Request-Id: a5ca691d-fe19-4e44-9afe-10e033bf1a5b
X-Api-Version: 1
Content-Type: application/json; charset=utf-8
Content-Length: 247
ETag: W/"f7-NH2Gmznu+rjZHNu1CyZkKjPwC1M"
Date: Fri, 27 Nov 2026 13:44:55 GMT
Connection: keep-alive
Keep-Alive: timeout=5

{"error":{"code":"invalid_body","message":"Check the fields listed in details.",
"details":{"formErrors":[],"fieldErrors":{"grade":["Invalid option: expected one of
\"V0\"|\"V1\"|\"V2\"|\"V3\"|\"V4\"|\"V5\"|\"V6\"|\"V7\"|\"V8\"|\"V9\"|\"V10\""]}}}}

The command and the body are wrapped here for display. Early Bird’s details holds "setOn":["Use a date that isn't after today"], and the blank name’s holds "name":["Too small: expected string to have >=1 characters"].

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 In this course, you set out to guard an Express API with middleware and Zod schemas, so every request is checked at the boundary. And to turn every failure into a clear, consistent error response with the right status code.

00:14 In the first lesson, the API stored a V14, a problem set next year, and a name made of spaces. Send them to the finished API. Each gets a 422, in the one error shape.

00:29 The details name each broken field: the grade, the date, the name.

00:35 Follow the V14. Your middleware gave it an ID and a timer, and the JSON parser read the body. In the route, validate() checked the body and threw an error.

00:47 The error skipped every ordinary function to your one error handler, which sent the 422.

00:54 A crash takes the same path. Its stack goes to your terminal under the request ID, and the client gets a plain 500 with that ID. Then you did it all again for the climbing log.

01:08 Only the server’s checks decide what reaches the data. Middleware runs in the order you register it. Check every body, path, and query at the boundary. Send 400 for a body that isn’t valid JSON, or a path or query value that fails its schema, and 422 for valid JSON that breaks the rules.

01:30 And answer every error from one handler, in one shape.

01:36 Next on the path, you’ll get an API ready for a browser front end: settings from environment variables, calls from another origin, and a log line for every request, with no secrets in it.

01:48 Thanks for hardening Crux Corner’s API with Ines.

Become a Member to join the conversation.