Zod Section 10 exercises
solution

Update the toString Function

Create a numberParser

Parsers are one of the most basic features of Zod.

To start our solution, we'll create a numberParser by invoking z.number().

This creates something called z.ZodNumber that provides several helpful methods.


const numberParser = z.number();

Pass

Loading solution

Transcript

The solution here is to use the very, very basic stuff that Zod gives you, which is, first of all, we're going to create a numberParser. This numberParser is going to be invoked from ,[object Object],. This creates something called ,[object Object],.

0:16 We can use numberParser in a bunch of ways. We can go .parse. We can pass in some random data. It will error if it's not a number. Anything that we return from this, as you can see,,[object Object, this is now typed as a number. If we wanted to accept a Boolean instead, then we would go z.boolean. Now this would be typed as a Boolean.

0:38 Importantly, it's now erroring correctly. It's expecting a number but received a string, when this is called. Here, if we instead change it to be a Boolean, then this will error, but it's going to error in the wrong way, "Expected boolean, received number." Rather, that's on the one down there.

0:57 These really basic elements of Zod here, like z.string, z.boolean, z.number, these are really, really important primitives because they allow you to build up from a solid base.

1:10 They represent the basic elements of TypeScript too. You've got Boolean. You've got string. You've got number. You've got bigint and null and undefined. You can even have ,[object Object,, ,[object Object],. What you'll start to see about Zod is that it mimics a lot of what you're used to in TypeScript with its runtime type system.