Skip to content

16 min read

Zod validation in Node.js: checking input data

In one sentence: Validating means checking, every time a request arrives, that the data has the shape you expect before you use it. TypeScript's types don't do it, because they disappear when the code runs; a Zod schema does, and if something doesn't match, your server answers 422 and says what's wrong.

What you will learn

  • Explain why TypeScript's types don't protect you from the data that comes from outside, and what a schema does.
  • Write a schema with Zod, with your own messages, and read what safeParse returns.
  • Validate the body, the route parameters and the query string with a middleware that answers 422, and tell it apart from the 400 of a body that can't be read.
  • Tell what a schema checks, the shape of the data, from what the route checks, the shop's rules.

Before you start, read: 06-middleware.md

Imagine someone sends strange orders to the shop’s API with curl. This is what the API from lesson 6, “Middleware” answers:

Ventana de terminal
curl -w '\n' --json '{"productId":1,"quantity":-3}' localhost:3000/orders
curl -w '\n' --json '{"productId":1,"quantity":"lots"}' localhost:3000/orders
curl -w '\n' --json '{"productId":1}' localhost:3000/orders
{"id":1,"productId":1,"quantity":-3,"status":"pending"}
{"id":2,"productId":1,"quantity":"lots","status":"pending"}
{"id":3,"productId":1,"status":"pending"}

Three orders saved: minus three T-shirts, “lots” of T-shirts and one with no quantity. None of them makes sense, and the API accepted them all.

In Phase 0 you saw the golden rule of the backend: never trust the ClientThe program that starts the conversation by asking another program, the server, for something. Your browser is a client.Go to definition. The website’s form can check the quantity, but anyone can skip it and write the request by hand, as you’ve just seen. In this lesson, your server starts following that rule.

The analogy

In lesson 3, you were the one filling in the form. Now you’re on the other side of the counter, where other people’s forms arrive. Before processing one, the clerk goes over it with a checklist: that the required boxes are filled in, that the date is a date and that “number of children” doesn’t say “lots.” If something’s wrong, the clerk hands it back with each badly filled box marked and the reason why.

That checklist is a schema, and that check is validation. And notice that a well-filled form isn’t an approved application: whether you’re entitled to it or not is decided later by the department, with its own records.

Where the analogy breaks down

  • The clerk uses common sense: they understand “two” written out in words. A schema only checks what it says, no more and no less: if you forget a rule, whatever breaks it gets through.
  • The clerk doesn’t correct your form. A schema can fix a few things along the way, if you ask it to: remove extra spaces or turn the text "2" into the number 2.

TypeScript’s types disappear when the code runs

Section titled “TypeScript’s types disappear when the code runs”

In lesson 4, you added as { productId: number; quantity: number } to an order’s body, and in lessons 5 and 6, the annotation data: { … }. Both promise TypeScript that the body has that shape, and TypeScript believes you without checking.

And Node erases the types before running. That’s why npm run check doesn’t complain about the API from lesson 6, which still saves "lots" as a quantity.

TypeScript A schema
When it checks While you write the code Every time a request arrives
What it checks That your code uses the data correctly That the data arriving is what you expect
If something’s wrong The editor marks it in red The server answers with an error

You need both. TypeScript watches your code; the schema watches the data from outside.

A SchemaThe description of what some data must look like: which fields it has, whether each one is a number or text, and which values are accepted. A program compares the incoming data with it and says whether it matches or what’s wrong.Go to definition is a description of what some data has to look like: which fields it has, whether each one is a number or text, and which values are accepted. A program compares what arrives against it and tells you whether it matches or what’s wrong. That’s ValidationChecking, every time it arrives, that the data the client sends is what was expected before using it: that no field is missing, that a number is a number and that it’s within what’s allowed. If something doesn’t match, the server answers with an error and stops there.Go to definition.

One of the most used tools for this in TypeScript is Zod. Install it in the project folder, ~/backend-course/phase-3/shop-api, along with its connector for Hono, the FrameworkReady-made code that solves what almost every program of one kind repeats, such as a server (reading each request, deciding which route handles it and responding), and that calls your functions when the time comes. You write only what’s specific to your application. Hono, Express and Fastify are frameworks for servers.Go to definition from lesson 5:

Install Zod

Ventana de terminal
npm install zod@4.6.7 @hono/zod-validator@0.9.1

What you will see:

added 2 packages, and audited 9 packages in 1s
1 package is looking for funding
run `npm fund` for details
found 0 vulnerabilities

Two Package (of code)Ready-to-use code that someone publishes with a name and a version number, so others can install it in their projects instead of writing it. In Node.js, packages are installed with npm and kept in the node_modules folder.Go to definition with their exact version, as in lesson 1, “Node.js”. This lesson’s outputs are from Node 26; your times and dates will be different.

Before touching the server, try Zod in the REPLREPL stands for Read-Eval-Print Loop.A way of working with a language in which you type a line of code, it runs and you see the result right away, without creating any file. In Node.js, you open it by typing node, with nothing else, in the terminal.Go to definition. Open the terminal in the project folder and type node. In the REPL, import … from doesn’t work: you use await import('zod'), which does the same.

In the REPL: a schema and two orders

Ventana de terminal
const { z } = await import('zod')
const orderSchema = z.object({ productId: z.number(), quantity: z.number().int().min(1) })
orderSchema.safeParse({ productId: 1, quantity: 2 })
orderSchema.safeParse({ productId: 'one', quantity: -3 }).error.issues

What you will see:

undefined
undefined
{ success: true, data: { productId: 1, quantity: 2 } }
[
{
expected: 'number',
code: 'invalid_type',
path: [ 'productId' ],
message: 'Invalid input: expected number, received string'
},
{
origin: 'number',
code: 'too_small',
minimum: 1,
inclusive: true,
path: [ 'quantity' ],
message: 'Too small: expected number to be >=1'
}
]

Word by word:

  • const { z } = … takes the piece called z out of Zod and keeps it under that name. z is all of Zod: each piece of a schema is asked for with z.something.
  • z.object({ … }) describes an ObjectA value that groups several pieces of data, each with its own name, such as { name: "Mug", price: 8.5 }. Each piece is read with a period and its name, such as product.price.Go to definition and its fields. z.number() says “a number,” and each dot adds a rule: .int(), a whole number, and .min(1), at least 1.
  • safeParse compares some data with the schema. If it matches, it returns success: true and the data in data.
  • If it doesn’t match, success is false and error.issues is the list of problems: one per field that fails.

Each problem says what happened (code), a message (message) and, above all, where: path, the field that fails.

Zod also has parse, which returns the data or, if it doesn’t match, throws an error, an ExceptionAn error a program throws when it can’t go on with what it was doing, like reading a JSON that arrives cut off. It travels up through the code until some part catches it; if none does, the program ends.Go to definition: you’ll see it in lesson 8. In a server, safeParse lets you decide what to answer.

Zod’s messages are in English, but they’re written for programmers: “expected number to be >=1”. Look at the ones for an order with no product and half a T-shirt:

In the REPL: Zod's own messages

Ventana de terminal
orderSchema.safeParse({ quantity: 2.5 }).error.issues.map((p) => p.message)

What you will see:

[
'Invalid input: expected number, received undefined',
'Invalid input: expected int, received number'
]

The messages are taken out with map (lesson 2).

Someone using the shop doesn’t know what “undefined”, “int” or “>=1” mean. Zod also comes with translations into other languages (z.locales), but they translate these same phrases.

That’s why the project writes its own messages, for the people who will read them. This is an order’s schema, in a new file, src/schemas.ts:

src/schemas.ts
export const orderSchema = z.object({
productId: z.number({ error: 'Must be a number' }).int('Must be a whole number'),
quantity: z
.number({ error: 'Must be a number' })
.int('Must be a whole number')
.min(1, 'At least 1')
.max(10, 'At most 10 per order'),
});
// The type comes from the schema: there's no need to write it twice.
export type OrderInput = z.infer<typeof orderSchema>;

Word by word:

  • export lets other files use the schema, as in lesson 3.
  • z.number({ error: '…' }) is “a number,” with the message for when it isn’t one. A missing field isn’t a number either, so it gets the same message.
  • .int('…'), .min(1, '…') and .max(10, '…') are the rules, each with its message. The maximum of 10 is the shop’s decision: nobody orders a hundred hoodies at once.
  • z.infer<typeof orderSchema> takes the TypeScript type out of the schema: { productId: number; quantity: number }. Between < and > goes where the type comes from, and typeof means here “the type of this value.”

The last line saves you from writing the shape twice, once for TypeScript and once for Zod. The schema is the only source: if you add a field to it, the type has it too.

A middleware validates before the route runs

Section titled “A middleware validates before the route runs”

In lesson 6 you saw that a MiddlewareA function a server runs with the requests of the routes it’s registered on (all of them, or only some), before the code that answers them. It can answer the request itself or pass it on to the next one, and it sees the response on its way back. It’s used for what affects many requests, like writing them to the log, granting CORS permission or asking for a credential.Go to definition is a layer the request goes through before it reaches the route, and that it can answer without letting it continue. Validating is exactly that. This is the project’s middleware, in src/middleware/validate.ts:

src/middleware/validate.ts
import { zValidator } from '@hono/zod-validator';
import type { ValidationTargets } from 'hono';
import type { ZodType } from 'zod';
// Checks one part of the request (the JSON body, the path or the query string) against a schema.
// If it doesn't match, it answers 422 with each problem and the route never runs: the server could
// read the request, but the data makes no sense (Phase 2, lesson 3).
export const validate = <Schema extends ZodType, Target extends keyof ValidationTargets>(
target: Target,
schema: Schema,
) =>
zValidator(target, schema, (result, c) => {
if (!result.success) {
const details = result.error.issues.map((issue) => ({
field: issue.path.join('.'),
problem: issue.message,
}));
return c.json({ error: 'The data is not valid', details }, 422);
}
});

Word by word:

  • zValidator is Hono’s connector with Zod. It reads the part of the request you tell it (target), compares it with the schema, like safeParse, and gives you the result.
  • target can be 'json' (the Body (of a request or response)The data an HTTP message carries after the blank line that ends the headers, such as the fields of a form, some JSON or an image. The Content-Type header says what format it’s in, and Content-Length how many bytes it takes up.Go to definition), 'param' (the Route parameterA variable piece of a route, marked with a colon: in /products/:id, the :id. The route accepts any value there (/products/2, /products/7) and hands it to your code, always as text. It says which resource you’re asking for; to filter or search a list, you use the query string.Go to definition, like the :id), 'query' (the Query stringThe part of a URL that comes after “?”: data in name=value pairs separated by “&”, such as q=mug&sort=price. It travels to the server attached to the path, and it’s used to search, filter or choose a page of results.Go to definition), and also the headers or the cookies.
  • What goes between < and >, with the two types from the import type, tells TypeScript what target and schema can be: a part of the request and a Zod schema. Thanks to that, the route will know later what type the data has. You can read it as validate(target, schema).
  • The function at the end gets the result. If it isn’t a success, it turns each problem into { field, problem }, with path.join('.') so that a field inside another one, like [ 'address', 'street' ], reads address.street. And it answers 422: it could read the request, but the data is no good.
  • If everything matches, it returns nothing, and the request goes on to the route.

Without that function, zValidator would answer 400 with the whole Zod error, {"success":false,"error":{"name":"ZodError",…, with the list of problems packed into a single string of text, hard to read for whoever uses your API.

With validate, the orders route looks like this:

src/app.ts
// When the route runs, the data already matches the schema: c.req.valid('json') has its type.
app.post('/orders', validate('json', orderSchema), (c) => {
const { productId, quantity } = c.req.valid('json');
// It matches the schema, but asks for a product that doesn't exist: 422, like invalid data.
if (!findProduct(productId)) return c.json({ error: 'No such product' }, 422);
const order = createOrder(productId, quantity);
return c.json(order, 201, { Location: `/orders/${order.id}` });
});
  • validate('json', orderSchema) goes between the route and its function, like adminOnly in lesson 6. If the body doesn’t match, the route’s function doesn’t even run.
  • c.req.valid('json') returns the data already checked, and TypeScript knows its type: no more as or annotation needed. This time the promise is true. const { productId, quantity } = … takes out those two values, like const { z } in the REPL.

Notice the check that’s left: findProduct. An order for product 99 has the perfect shape and passes the schema. But the shop has no product 99, and the route answers 422, as it would to a value that’s no good. It isn’t a 404: /orders exists, and what’s wrong is a value in the body.

They’re two different questions:

  • Does the data have the right shape? The schema answers it, without knowing anything about the shop.
  • Does it make sense for the shop right now? Whether the product exists or there’s stock depends on the data at that moment. The route answers it.

What comes in the path and the query string is always text

Section titled “What comes in the path and the query string is always text”

In /products/2, the 2 arrives as the text "2", and in ?max=10, the 10 too. A URL is text. So z.number() would always reject the parameters. That’s what z.coerce is for:

In the REPL: the text “2” and the number 2

Ventana de terminal
z.number().safeParse('2').success
z.coerce.number().safeParse('2')
z.coerce.number().safeParse('abc').success

What you will see:

false
{ success: true, data: 2 }
false

z.coerce.number() turns the text into a number before checking it: '2' passes and arrives as 2. 'abc' isn’t any number, and it fails.

These are the parameters’ schemas, at the end of src/schemas.ts:

src/schemas.ts
// What comes in the path and in the query string is always text: coerce turns it into a number.
export const idSchema = z.object({
id: z.coerce.number({ error: 'Must be a number' }).int('Must be a whole number'),
});
export const filterSchema = z.object({
max: z.coerce.number({ error: 'Must be a number' }).positive('Must be greater than 0').optional(),
});
  • idSchema is for the :id in /products/:id: a whole number. c.req.valid('param').id is that id, already turned into a number.
  • filterSchema is for ?max=: a number greater than 0. .optional() says it may not be there: plain /products still works.

And this is how the routes that read products use them:

src/app.ts
app.get('/products', validate('query', filterSchema), (c) => {
const { max } = c.req.valid('query');
if (max === undefined) return c.json(products);
return c.json(products.filter((product) => product.price <= max));
});
app.get('/products/:id', validate('param', idSchema), (c) => {
const product = findProduct(c.req.valid('param').id);
if (!product) return c.json({ error: 'No such product' }, 404);
return c.json(product);
});

Before, /products/abc answered 404, “No such product”, and ?max=abc, an empty list with no warning. Now both answer 422 and say what’s wrong. /products/99 is still a 404: it’s a well-formed id, of a product that doesn’t exist.

422 if the data is no good; 400 if it can’t even be read

Section titled “422 if the data is no good; 400 if it can’t even be read”

This API follows the rule from Phase 2’s lesson 3: with a 400, the server couldn’t even read the request; with a 422, it read it, and what it asks for makes no sense.

What reaches POST /orders Code Who answers
A body that isn’t JSON 400 Hono, before the schema sees it
{"productId":1,"quantity":-3} 422 validate, with details
{"productId":99,"quantity":1} 422 The route, without details

The 400 for a broken body is written by Hono on its own, and in text, not JSON: Malformed JSON in request body. In lesson 8 you’ll turn it into JSON, like the rest of the API.

Not every API makes the distinction: Stripe, or zValidator itself without your function, answers 400 to any data that’s no good, because 400 is the general client error. What matters is always following the same criterion and having the body say what’s wrong.

The goal: make the shop’s API refuse every impossible order, and check what it answers to each one. You start from your project from lesson 6. If you get stuck, download this lesson’s folder and compare it with yours: from ~/backend-course/phase-3, curl -O https://backenddesdecero.com/practicas/shop-api/07-validation.tar.gz, tar -xzf 07-validation.tar.gz and, inside, npm install.

If you didn’t install Zod in “How it really works,” do it now. Then:

  1. Create src/schemas.ts. Start with the line import { z } from 'zod'; and put below it the order’s schema, the product’s (below) and the parameters’, in that order.
  2. Create src/middleware/validate.ts with the middleware above.

The schema for a new product is missing, the one the admin creates with POST /products:

src/schemas.ts
export const productSchema = z.object({
name: z
.string({ error: 'Must be text' })
.trim()
.min(1, "Can't be empty")
.max(60, 'At most 60 characters'),
price: z
.number({ error: 'Must be a number' })
.positive('Must be greater than 0')
.max(10_000, 'At most €10,000'),
});

One new piece: .trim() removes the spaces at the start and the end before checking the rest. That way, " " doesn’t pass the .min(1), and " Cap " reaches the route as "Cap".

In src/app.ts, add the two new import lines at the top (the highlighted lines):

src/app.ts
import { Hono } from 'hono';
import { cors } from 'hono/cors';
import { createOrder, createProduct, findOrder, findProduct, orders, products } from './data.ts';
import { adminOnly } from './middleware/admin-only.ts';
import { requestLog } from './middleware/request-log.ts';
import { validate } from './middleware/validate.ts';
import { filterSchema, idSchema, orderSchema, productSchema } from './schemas.ts';

Replace the routes that read products and the one that creates an order with the ones you’ve seen above, and the one that creates a product with this one:

src/app.ts
app.post('/products', adminOnly, validate('json', productSchema), (c) => {
const { name, price } = c.req.valid('json');
const product = createProduct(name, price);
return c.json(product, 201, { Location: `/products/${product.id}` });
});

adminOnly goes before validate: without a token, the API answers 401 without looking at the body. The order of the layers, as in lesson 6.

Start the server in one terminal with npm run dev. You’ll see The shop is listening on http://localhost:3000, and with --watch it restarts by itself every time you save. Open another terminal for curl. --json, from Phase 2’s lesson 1, sends the body with POST and Content-Type: application/json.

1. A correct order

Ventana de terminal
curl -i --json '{"productId":1,"quantity":2}' localhost:3000/orders

What you will see:

HTTP/1.1 201 Created
content-type: application/json
location: /orders/1
vary: Origin
content-length: 54
Date: Tue, 13 Oct 2026 11:19:22 GMT
Connection: keep-alive
Keep-Alive: timeout=5
{"id":1,"productId":1,"quantity":2,"status":"pending"}

201 and the order created: validating doesn’t get in the way of whoever sends the data correctly.

If you get Couldn't connect to server, the server isn’t running: look at the other terminal. If you see ReferenceError: filterSchema is not defined and Failed running 'src/server.ts' there, you’re missing an import in src/app.ts.

2. An order with no quantity

Ventana de terminal
curl -i --json '{"productId":1}' localhost:3000/orders

What you will see:

HTTP/1.1 422 Unprocessable Entity
content-type: application/json
vary: Origin
content-length: 95
Date: Tue, 13 Oct 2026 11:19:22 GMT
Connection: keep-alive
Keep-Alive: timeout=5
{"error":"The data is not valid","details":[{"field":"quantity","problem":"Must be a number"}]}

Look at the 422 on the first line and at details: the field and the problem, with the message you wrote. The route didn’t run, so there’s no new order. Unprocessable Entity is the old name of the same code, the one Node still writes; Phase 2 taught you the current one, Unprocessable Content. Programs only look at the number.

3. A wrong type, a negative quantity and 50 T-shirts

Ventana de terminal
curl -w '\n' --json '{"productId":"one","quantity":2}' localhost:3000/orders
curl -w '\n' --json '{"productId":1,"quantity":-3}' localhost:3000/orders
curl -w '\n' --json '{"productId":1,"quantity":50}' localhost:3000/orders

What you will see:

{"error":"The data is not valid","details":[{"field":"productId","problem":"Must be a number"}]}
{"error":"The data is not valid","details":[{"field":"quantity","problem":"At least 1"}]}
{"error":"The data is not valid","details":[{"field":"quantity","problem":"At most 10 per order"}]}

Without -i, only the body; -w '\n' adds a line break at the end of each response, so they don’t come out stuck together. Each order fails because of a different rule, and each message says so.

4. A product that doesn't exist

Ventana de terminal
curl -i --json '{"productId":99,"quantity":1}' localhost:3000/orders

What you will see:

HTTP/1.1 422 Unprocessable Entity
content-type: application/json
vary: Origin
content-length: 27
Date: Tue, 13 Oct 2026 11:19:22 GMT
Connection: keep-alive
Keep-Alive: timeout=5
{"error":"No such product"}

Another 422, but without details: it doesn’t come from the schema, but from the route’s check.

5. The path and the query string

Ventana de terminal
curl -w '\n' localhost:3000/products/abc
curl -w '\n' 'localhost:3000/products?max=-5'
curl -w '\n' 'localhost:3000/products?max=10'

What you will see:

{"error":"The data is not valid","details":[{"field":"id","problem":"Must be a number"}]}
{"error":"The data is not valid","details":[{"field":"max","problem":"Must be greater than 0"}]}
[{"id":2,"name":"Mug","price":8.5}]

abc isn’t an id and -5 isn’t a maximum price: two 422s. The third one works: z.coerce has turned "10" into 10, and only the mug costs €10 or less.

The quotes around the last two URLs are needed: without them, zsh treats the ? as a wildcard and answers zsh: no matches found.

6. A body cut in half

Ventana de terminal
curl -i --json '{"productId":1,"quant' localhost:3000/orders

What you will see:

HTTP/1.1 400 Bad Request
content-type: text/plain;charset=UTF-8
vary: Origin
content-length: 30
Date: Tue, 13 Oct 2026 11:19:22 GMT
Connection: keep-alive
Keep-Alive: timeout=5
Malformed JSON in request body

A 400 at last: Hono couldn’t read the body, and the schema never even saw it. Look at content-type: text/plain: the response is text, not JSON.

In the server’s terminal, the log from lesson 6 has written down each request:

POST /orders 201 4 ms
POST /orders 422 1 ms
POST /orders 422 1 ms
POST /orders 422 1 ms
POST /orders 422 1 ms
POST /orders 422 0 ms
GET /products/abc 422 1 ms
GET /products 422 1 ms
GET /products 200 0 ms
POST /orders 400 1 ms

Seven 422s, one 400 and a single order saved: the correct one.

Challenge: GET /orders/abc still answers 404, “No such order”, as /products/abc used to. Make it answer 422 with its details, without writing any new schema. And what does GET /orders/7 answer?Show answer

idSchema does the job: it describes any :id, of a product or of an order. In src/app.ts, the route ends up like this:

src/app.ts
app.get('/orders/:id', validate('param', idSchema), (c) => {
const order = findOrder(c.req.valid('param').id);
if (!order) return c.json({ error: 'No such order' }, 404);
return c.json(order);
});

When you save, --watch restarts the server:

Ventana de terminal
curl -w '\n' localhost:3000/orders/abc
curl -w '\n' localhost:3000/orders/7
{"error":"The data is not valid","details":[{"field":"id","problem":"Must be a number"}]}
{"error":"No such order"}

abc isn’t an id: 422. 7 is one, but that order doesn’t exist: it’s still a 404. This lesson’s folder already comes with this change.

  • Forms that mark a field in red before you send them (“This field is required”) validate in the browser. It’s convenient for whoever is typing, but the server checks again, because that check can be skipped.
  • The errors that arrive after you send, like “That ZIP code doesn’t exist,” come from the server. If you open DevTools (how to open them), in the Network tab you’ll see a 400 or a 422 with a list of fields, like your details, which the page uses to mark each one.
  • Validating only in the form. Anyone can send the request with curl, without going through the website.
  • z.number() for a route or query string parameter. They always arrive as text and would always fail. Use z.coerce.number().
  • Forgetting Content-Type: application/json. With curl -d and no -H, Hono doesn’t read the body as JSON and every field fails with “Must be a number”. Use --json.
  • Using parse in a route on its own. If the data doesn’t match, it throws an error, and the client gets a 500 instead of a 422. In a server, use safeParse, or a middleware like validate.
  • TypeScript’s types are erased when the code runs: they don’t protect you from what comes over the network. A schema does, because it checks the data every time it arrives.
  • With Zod, z.object describes the shape, each dot adds a rule and safeParse says whether it matches or what’s wrong, field by field. z.infer takes the type from the same schema.
  • The validate middleware checks the body, the path or the query string, and if something doesn’t match it answers 422 with each problem, without reaching the route. A body that isn’t JSON gets a 400 from Hono: it couldn’t even be read.
  • What comes in the URL is always text: z.coerce converts it before checking it.
  • The schema checks the shape; the rules that depend on the shop’s data, like whether the product exists, are checked by the route.
The shop’s form already stops anyone from ordering more than 10 units. Does the API need to check it too?

The API from lesson 6 says in its code that the quantity is a number, and someone sends an order with the quantity “lots”. What happens?

You write idSchema with z.number().int() for the route /products/:id, without coerce. What does GET /products/2 answer?

POST /orders gets a body cut in half, which isn’t valid JSON. What does this lesson’s API answer?

Someone orders 2 units of product 99, which the shop doesn’t have. Who stops it?

You add a note field to the order’s schema, an optional text. What else do you have to change for TypeScript to know about it?

phase-3 · lesson 7/9