Modern APIs with FastAPI and Python Transcripts
Chapter: Modern language foundations
Lecture: Model validation, the Pydantic way

Login or purchase this course to watch this video and the rest of the course contents.
0:00 That was rough, wasn't it?
0:01 Okay, let's take a step back towards this.
0:04 So we're gonna go and use this library called pydantic.
0:07 Now pydantic is really, really neat.
0:09 And what it lets us do is it lets us create simple classes.
0:13 Here's a class. It has an ID
0:15 which is an integer, a name which is a string and has a default value.
0:17 A signup which is an optional datetime defaulting to not set and so
0:22 on, and some external data like this,
0:25 and then we create it exactly the same way.
0:27 But this is all we write,
0:29 right? Well, technically, we already have the external data.
0:31 This is what we write. And it behaves very,
0:33 very similar to what we already saw,
0:36 which is killer. So let's go make that happen.
0:39 In order to do that, we're going to need to use pydantic here,
0:42 Yep. That's still not misspelled.
0:47 Go run pip install again.
0:48 Get pydantic. So what we're gonna do is I'll make a copy of this.
0:52 I'll call this V2, it'll be the pydantic one.
0:55 And let's get rid of this big gnarly beast here,
1:00 and I'll just leave that for a minute.
1:01 Let's go create our class order, and the way we use pydantic is we derive
1:04 from base model, which comes from pydantic,
1:08 And then it's very, very similar to data classes.
1:11 If you've seen this, we'll have item ID and then we just specify the type.
1:14 This is gonna be a required integer field,
1:17 Okay? Say created date, which is,
1:22 say this is an optional,
1:24 datetime dot datetime. We just need to import that.
1:28 What else do we have? We had pages visited and that was going to be
1:32 a list of int. And then we had a price which was a float.
1:36 Look how nice and clean that is.
1:37 Even simpler than are simple version.
1:40 And guess what? Unless I made a quick mistake here,
1:42 it should run and it should do the same thing.
1:44 So let's go and run "order_v2".
1:47 Check it out. It did the same thing.
1:49 Item is an ID. An integer got converted.
1:53 23 created date. The datetime got converted,
1:56 pages visited. Look, even the elements in the list got converted and the price
2:01 just hung in there. And we said that the datetime was optional.
2:04 So what if we omit it.
2:06 Well, it's just set to none.
2:08 That's fine. No big deal.
2:09 We did not say that the pages was optional.
2:12 What happens if we omit that? it hates it!
2:15 A required field is missing. What field? Pages visited. Super,
2:20 Super cool. All the validation,
2:21 all that conversion, everything, even a little bit better than we had.
2:25 It will say you know what's required and what's not.
2:28 So this is what pydantic offers for us.
2:31 This really clean model of writing this that takes this external data very much like you
2:35 might receive from an API from some client sending random junky,
2:40 semi-correct data, like here's a sort of a three,
2:44 but it's not a three, but it could be a three type of data over
2:47 to us and just turn it into exactly what we want or a nice error message.
2:51 Like what happens if I put ABC
2:53 here? Pages visited, the second index,
2:58 so third item, is not an integer. How super cool is that,
3:03 and we don't have to write it.
3:04 That's why it's cool. So this is exactly we want, the sort of filter we
3:08 want to go through with submitting data to our API and, a little bit
3:13 of a sneak peek here,
3:15 If we have some order API call, instead of saying it takes in item
3:20 ID, which is an int, created date,
3:22 blah blah blah, we can just say this:
3:24 It takes an order, which is an order,
3:27 and that's it. All that automatic validation and all that cool stuff I just showed
3:31 you happens before our function even gets called.
3:34 It's done automatically by FastAPI, so that's super cool.
3:38 This really works by default for JSON post, and can be done for others with a
3:45 slight modification. You have to like,
3:47 give it a certain, a certain statement here that says,
3:50 Please go find this somewhere else,
3:52 okay? So by default, this works when some API is being called
3:56 and data is being posted to it,
3:57 if you want something else as like our calculator example, we're gonna need to do something
4:02 slightly different, but nonetheless, it's still super,
4:04 super cool, and I guess we could even just leave this here like that.
4:07 Alright, this is pydantic.
4:10 Look, let's just do a quick compare.
4:12 Here's what we do by ourselves,
4:13 and this is actually less validation than what pydantic is doing because it doesn't say like,
4:18 Oh, the created date is required and stuff like that. It'll crash if it's not
4:21 there, but it doesn't say that it's required explicitly.
4:25 So all of this, place down to that, that's doing even more. Pydantic derived from the
4:31 base model to create those things,
4:34 kind of like you would with data class.
4:36 We could even come over here and say that if you don't specify
4:39 these, we'll just do an empty list as the default. Super cool.
4:43 So that's pydantic and you'll see it appear as we work with our API's at
4:47 the exchange layer, when data is being passed to us or when we return
4:51 objects, we can actually return Pydantic models right out of our API methods
4:55 and they'll get converted to JSON as well.