
Lesson Description
The "Introduction to Zod" Lesson is part of the full, Fullstack TypeScript, v2 (feat. Zod) course featured in this preview video. Here's what you'd learn in this lesson:
Steve introduces Zod, a schema validation library explicitly built for TypeScript, and briefly walks through the documentation. Some basic validation examples are shared along with the performance benefits.
Transcript from the "Introduction to Zod" Lesson
[00:00:00]
>> Steve Kinney: Let's take a look at Zod for a second. We can see that there's a few things to kind of take note of when we look at the site. One is, we'll see, I'll look at the sidebar in a moment. But two is scrolling past the sponsors. This is what I want to look at the ecosystem, right?
[00:00:17]
One of the things that I want to kind of encourage you is, if there are other things that you use, there might be libraries for kind of integrating with that. So you can kind of get a lot of things for free. I think more importantly, yes, you can use it with React, hook form that the we talked about, we joked earlier, that stuff, users type might be problematic.
[00:00:41]
And having the form validation, so your same schema that you're using on the request body in Express or when you're gonna get it back from the API. You can also use those same schemas for the forms that your users are filling out. Again, if you use Formik, there's other ones, so on and so forth.
[00:00:59]
And there's for spell kit, there's the ability then also to turn them into JSON schema, or take a bunch of schemas and turn them into type script types, right? So for a lot of these things, what I want to call out is, if there are things that you think you need to do very tediously, it is worth at least checking.
[00:01:20]
To see if somebody else did it tediously first and seeing if you have it as well. There are some other interesting things that you can do that you cannot do in TypeScript. And so, like I said before, there is a coercion, which is kinda nice, which is the ability to say like a string.
[00:01:38]
You can also actually, but like an email, right? And obviously Typescript's not going to do that for you. There's some like template literals that you can use, but the type will be a string, but then you can also actually like make sure that it is truly an email, something so on and so forth.
[00:01:51]
Which is also very useful, again, for when you're receiving it in the API level before you put it in the database, right? You can probably take lines and lines and lines of regex. And code and stuff to make sure that the number's positive, so on and so forth, and probably eliminate that as well.
[00:02:06]
But if it's something that can coerce into a string, you can do that first, so on and so forth. And there's also the ability to do a lot of the utility types that we're used to seeing from Typescript, all of those are available as well, right? So you get a little bit more than you get from TypeScript, and then you can always, like, create the TypeScript types out of it as well.
[00:02:29]
Installing Zod is relatively easy, the simplest use case. And we're going to do some examples together in a second. But we'll do the high level, which is like, again, just a string. It will parse a string. Just a number, so on and so forth, right? You can also do things like making sure that it's a date object or a big int.
[00:02:47]
All of the things that you want are in there. You can start to, then build up things as well, and then generate those types, like we said, but there's some nice things you can do, which is, you can also make sure that it's got a minimum length. Things you can't do in TypeScript, which is, again, because you have full access to the data object itself, because a lot of times, like TypeScript couldn't help you, if you if it wanted to.
[00:03:09]
Again, if it's outside those walls, you do literals. So instead of a string, no, no, no, it should be the string, hello, right? Or if you want to use this is not technically like the typescript version of an enum, but this is like effectively a union of strengths to say hey, it should be read the string.
[00:03:28]
Read the string green, or the sring blue, right? If you have something that can only be a few different strings, this one will throw in all of those cases if that's not. And that's a lot easier than if you had to try to figure out from the type.
[00:03:40]
You'd either have to do that by hand or again, put it into a data structure and then iterate over it, this kinda get it all for free.
>> Steve Kinney: Other fun things we can do, a tuple or tuple, your choice. I'm not having that fight right now, is to say like an array of two things, the first one being a string and the second one being a number.
[00:04:00]
The tuple that we probably or tuple, I haven't decided which one I want to say because it's not a word I say out loud that often, that we see a lot of times is something like useState in React. Sure, it's an array, but that first one is always the current state and the second thing out of that array from useState is the ability to set At the state, right?
[00:04:21]
And so to know exactly what types those are. Is incredibly powerful as well. You can also do unions in this case, and so this will be either a string or a number as well. Intersections, again, all of the TypeScript stuff that you know that you can use, you can use as well.
[00:04:40]
But where this gets a little bit more powerful is then you can begin to compose some of these things, right? And so if you have a schema, like we talked about those nested JavaScript objects, right, you can just reference a different schema for that value of that key, right?
[00:04:56]
Or you can extend one in the same way you might extend an interface face and TypeScript, right? And add more things to it, or grab we can pick things out of it, so on and so forth. Do a lot of times, if you find yourself like rewriting the same things over and over by hand again.
[00:05:12]
Keep in mind that you have a programming language and you can, like, begin to compose things as well.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops