
Lesson Description
The "Request Body Validation" 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 creates a validateBody middleware that is abstracted to accept any Zod schema and validate the incoming request's body. This can be reused through the Express application and reduce boilerplate validation logic for each request.
Transcript from the "Request Body Validation" Lesson
[00:00:00]
>> Steve Kinney: And so you can create a few of these and we'll actually kinda make this a little bit more generalizable in a second, a few of these and get lots and lots of stuff for free. In the same way we were taking the existing schemas and building on top of them, we can create a few of these middleware and make it a little bit better.
[00:00:17]
I can even make this one better, right? Which is right now this assumes just this create task schema. What if I wanted to make it generalizable? What if I wanted to say, hey, I want a middleware where I'll give you a schema and you make sure that it works, and I just use that thing all over the place, right?
[00:00:40]
So we can do that as well, right? Who wants to get stressed out for a moment, you do? Okay, we got multiple who want to get stressed out. Remember when you were first learning JavaScript and you saw currying functions and some functional programming nerd said, this is the best thing ever, and you're like, I don't understand what any of that is, when am I going to need it?
[00:01:09]
Right now. [LAUGH] All right, short version. Occurring function is just a function that returns another function, right? And as a way, I'm gonna give you some of the arguments now and the rest of them later, right? Which is a great way for, again, making factories. I got one Rubeus in the room, so I knew that word would work.
[00:01:36]
You're nodding, I said factory. It's a way to create a function that will make more functions with some of the arguments already applied. So instead of having one where we validate the request body with a hard coded in-schema, what I would like to do is first, I will give you the schema, and then you fill in this spot for me, right?
[00:02:00]
And for being very confusing, it is luckily, somewhat easy to implement with our functions. So I'm gonna get rid of the type stuff for one second, just cuz it's in the way. I cut it though and we'll see. Which is to say, give me some.
>> Steve Kinney: Kinda schema.
[00:02:31]
>> Steve Kinney: And then I will return a function to you that will then be the rest of this, right? So then we will be able to say something like, we'll call this validateBody instead, right? Where now we can say validateBody with a CreateTaskSchema, right? And that will do one where this part is filled in with the create task schema, right?
[00:02:54]
Then later, I wanna make a validate body with Create User schema, same code, you just pass in a different schema, right? And I will show you the natural conclusion of where you can take this, I will not make you watch me type it, cuz I did it, and I was gonna do it live, and I realize that nobody wants to watch that.
[00:03:15]
It's not compelling entertainment, so I'll show you, be like one of the natural conclusions in which you can take in a second. But let's get the conceptual part down. So now we can have the ability to validate bodies whenever we want. What we need to do is to say we do need a generic, I'm sorry, and that's going to be.
[00:03:45]
>> Steve Kinney: I think that schema is what I needed, some Zod schema that is evaluating something being of a given type, right? So if we look at, I moved all my things out. Task schema is really a the type of it is a Zod schema that matches a task, right?
[00:04:06]
So we're saying don't worry about it. It'll become clear. So we're gonna pass that in and then what we are going to do is then have a function that should give us that request handler that we had before, cool. So that is going to give me paste that code back in and of the same type.
[00:04:41]
Okay, that stresses you out, I'm sorry. But watch what you can do cuz you write that code once, you get it working, we'll deal with that squiggly in a second. But now I can do.
>> Steve Kinney: Everything works, but now I never have to write that middleware again, cuz it will validate the body on any schema that I want, right?
[00:05:11]
And so theoretically, again, you build up these reusable tools. Yeah, it's gonna take your morning, right? But then theoretically, now you can make large, sweeping changes to the types very easily. In your code base, you change the schema, everything that uses this one will then validate the body, so anything that uses this middleware.
[00:05:30]
And like in Express, you can say all of these sub paths should already do that, right? And have it cascade down, and get a lot of this for free. If that stresses you out, you're like, my app's not that big, I just wanna go manually do the parsing, you will still get the same benefit, right?
[00:05:47]
This is now we're getting to the point of, how do we do this on larger, and larger, and larger code bases, right? But sometimes you can do this to just the place in your code base that has the most pain, and that will get you there, right? And so you could theoretically make one of these for validate body, validate query params, and validate the path params.
[00:06:11]
And you can make this, they don't expose the types and it's really big pain in the ass. I have a bunch of tricks if you're interested that I will show you. But for now, I'm just gonna do this, which is be an object that can't be null, is all I'm saying here.
[00:06:27]
I don't care what you are, I don't know anything about it. Just be an object that's not null.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops