In Register Middleware in Order, you listed showBody() in the POST /problems route, right before its handler. It logs what Ines’s page sends and then lets everything through, including the V14, the problem from next year, and the blank name you sent in the first lesson.
In this lesson, you’ll add validation to that route. You’ll describe a valid new problem as a Zod schema, check each body against it before anything reaches the store, and respond to an invalid body with a list of problems grouped by field. Then you’ll send the first lesson’s three requests again and watch the API refuse them. Start by writing Ines’s rules down as a schema.
Turn Ines’s Rules Into a Schema
The starting project checks bodies by hand, and its toNewProblem() function only asks whether each field is a string. Ines’s rules are about values: a grade from V0 to V10, one of the wall’s five hold colors, a real date that isn’t in the future, and a name with something in it. Zod, the library you’ve used to check outside data in TypeScript programs, can say all of that in a few lines.
You already know the parts. z.object() describes an object’s fields, .safeParse() checks a value against the schema, and z.infer turns the schema into a TypeScript type. On the server, the outside data is a request body, and the place to check it is the boundary, before the store sees it.
Replace everything in problems.ts with a schema for a stored problem and one for a new problem:
problems.ts
import { z } from "zod";
function today() {
return new Date().toISOString().slice(0, 10);
}
export const Problem = z.object({
id: z.string(),
name: z.string().trim().min(1),
colour: z.enum(["yellow", "blue", "green", "red", "black"]),
grade: z.enum([
"V0", "V1", "V2", "V3", "V4", "V5",
"V6", "V7", "V8", "V9", "V10",
]),
setOn: z.iso.date().refine((date) => date <= today(), {
message: "Use a date that isn't after today",
}),
retired: z.boolean(),
});
export type Problem = z.infer<typeof Problem>;
export const NewProblem = Problem.omit({ id: true, retired: true });
export type NewProblem = z.infer<typeof NewProblem>;
Each schema shares its name with the type inferred from it. That’s allowed because TypeScript keeps values and types apart, so Problem in code is the schema and Problem in a type annotation is the type. The import type { Problem } line in routes/problems.ts keeps working, and toNewProblem() is gone.
Here’s what each field adds:
name: A name of three spaces has a length of three, so.min(1)alone would accept it..trim()removes the spaces around the name before.min(1)checks its length. It also changes the value, so the parsed data holds the trimmed name.colourandgrade:z.enum()accepts only the strings it lists. The grade list stops at V10, so a V14 fails, and a purple problem like Mei’s from the first lesson fails on its color.setOn:z.iso.date()accepts only a real calendar date written asYYYY-MM-DD. Then.refine()runs your function on the date and fails with your message when the function returnsfalse.
Why is today() a function instead of a constant? The server runs for days, and a constant set at startup would keep the day the server started. The refinement calls today() on every check instead.
Comparing the two strings works because YYYY-MM-DD dates sort in date order. .toISOString() gives the date in UTC, so near midnight the API can refuse today’s date or accept tomorrow’s. A real API compares dates in the wall’s own time zone. For Crux Corner, the UTC date is close enough.
The Problem schema describes a stored record, including the id field and retired flag that the server sets. Ines doesn’t send those, so NewProblem omits them from the same schema instead of repeating four fields.