Steve Kinney

Full-Stack TypeScript

Validating Request Parameters

Search parameters and path parameters are technically strings. But, I know they should be numbers. It’s fine. Let’s just make sure that they coerce into numbers.

const TaskSchema = z.object({
	id: z.coerce.number().int(),
	title: z.string(),
	description: z.string().optional(),
	completed: z.coerce.boolean(),
});

const TaskParamsSchema = TaskSchema.pick({ id: true });

We can use it like this.

const { id } = TaskParamsSchema.parse(req.params);

Search Parameters

The same basic idea works for search parameters.

const TaskQuerySchema = z.object({
	completed: z.coerce.boolean().optional(),
});

Or, I might do something like this:

const TaskQuerySchema = TaskSchema.pick({ completed: true }).partial();

And now we can make sure our queries make sense.

Next: Using Middleware to Validate Schema

Last modified on .