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

Coerce Path and Query Values

In this lesson, you gave the problem list’s query string a Zod schema, so a limit that isn’t a number gets an error instead of an empty list.

Key Points

  • Everything in a URL arrives as text. Express hands you a single query value or a route parameter as a string. A repeated key, as in ?limit=5&limit=6, gives an array: ["5", "6"]. In /problems/2?limit=5, the id is "2" and the limit is "5", and "2" === 2 is false.
  • An unchecked limit fails quietly. Before this lesson, GET /problems?limit=abc answered 200 OK with []. The handler turned the limit into a number with Number(), which gave NaN, and .slice() treats NaN as 0.
  • z.number() refuses a string, even "5". z.coerce.number() runs Number() on the value first and then checks the result, so "5" comes out as the number 5.
  • Coerce only what means a number. The grade and colour filters stay strings. They take their fields from Problem through its .shape property, which holds each field’s schema, and add .optional(). The wall’s ids are strings too, so the id’s schema is a plain z.string(). The limit is coerced and must be a whole number of at least 1 (.int().min(1)). When the client leaves it out, it defaults to 20, so a list without a limit now stops at 20 problems.
  • Each limit takes its own path through the schema. "5" becomes 5 and passes. "abc" becomes NaN and fails the number check. No limit at all skips coercion, and the default makes it 20. An empty limit, ?limit=, fails .min(1).
  • Watch out for the empty string. Number("") is 0, not NaN, so an empty limit passes the number check and only .min(1) stops it. With .min(0), it would pass as 0, and .slice() would hand the script an empty list again, with no error.
  • A refused query or path value gets 400. So far, the wall’s status rule covered bodies, and a body that breaks the rules still gets 422. A query or path value the API refuses gets 400 instead, because the address itself is bad. So ?grade=V14 gets 400, where the same grade in a body got 422. The answer uses the API’s error shape, with z.flattenError() listing every problem by field in details.
  • The handler uses the parsed data. After a successful safeParse(), it filters and slices with result.data, where the limit is already a number.

Everything in a URL arrives as text. Coerce the ones that mean numbers, keep the rest as strings, and don’t let an empty value slip through as 0.

The Final Code

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 the last lesson, the wall’s API started checking the body of every new problem. The values in the URL still go unchecked.

00:09 Ask for the problem list with a limit of abc, as a member’s script with a bug might. The API answers 200 OK, with an empty list. The handler turns the limit into a number with Number(), and that gives NaN, short for not a number.

00:26 .slice() treats NaN as 0, so the script gets an empty wall instead of an error. The limit is a string because everything in a URL arrives as text.

00:39 Express hands you a query value or a route parameter as a string. A repeated query key gives an array instead. The id in the path for problem 2 is the string 2.

00:51 In the Node REPL, the string 2 isn’t the number 2. The wall’s ids are strings, so the id can stay one. The limit has to become a number.

01:02 Load Zod in the REPL, and check the string 5 with a number schema. It fails, because a string isn’t a number. Now try the same string with z.coerce.number() instead.

01:14 Coerce runs Number() on the value first, and then checks the result. This time, you get the number 5.

01:22 Now give the problem list’s query its own schema, at the bottom of problems.ts. The grade and colour filters take their fields from Problem through its shape property, which holds each field’s schema.

01:35 Both are optional, and as strings, they need no coercion. The limit is coerced, and then it has to be a whole number of at least 1. When the client leaves it out, the default fills in 20.

01:46 So a list without a limit now stops at 20, more than the wall’s nine.

01:53 The id in the path gets a schema too, a plain string, since that’s what it is. For now, it’s a placeholder that the next lesson puts to work.

02:03 Follow four limits through the schema. The string 5 becomes the number 5. It’s a whole number of at least 1, so it passes. The string abc becomes NaN, and the number check refuses it.

02:18 With no limit at all, there’s nothing to coerce. The default steps in, and the limit is 20. Last, an empty limit, with nothing after the equals sign. It’s there, so the default stays out of it.

02:32 Number() turns an empty string into 0, not NaN. So the empty limit passes the number check, and only the minimum of 1 stops it. With a minimum of 0, it would pass as 0, and the script would get an empty wall again, with no error.

02:49 In the routes file, import ProblemQuery, and in the handler, check the query with safeParse before anything else. When it fails, answer with every problem listed by field, in the API’s error shape.

03:03 So far, the wall’s status rule covered bodies. Now it gets a URL half. A query or path value the API refuses gets 400, because the address itself is bad. When it passes, filter and slice with the parsed data, where the limit is already a number.

03:21 Send the limit of abc again. This time, the API answers 400 Bad Request, and details names the limit. A limit of 5 gets the first five problems.

03:35 And a grade of V14, off the wall’s scale, gets 400 here, where in a body it got 422.

03:44 So everything in a URL arrives as text. Coerce the ones that mean numbers, keep the rest as strings, and don’t let an empty value slip through as 0.

03:55 The query check now sits in its handler, beside the body check, each with its own error answer. In the next lesson, Move the Checks Into validate(), you’ll move them into one middleware that each route can list.

Become a Member to join the conversation.