Python-powered chat apps with Twilio and SendGrid Transcripts
Chapter: Creating the Flask Python web app
Lecture: A little more Pydantic validation
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Let's look at one more super quick thing we can do to make our pydantic model
0:04
data exchange a little tiny bit better over at pydantic.
0:07
They have this thing called model config,
0:09
where you can set all sorts of information and auto transformations and validation for your models
0:14
so we can set the title.
0:16
So how it refers to itself,
0:18
things like strip off any white space on any strings or what
0:23
the maximum or minimum length of any string we're willing to accept is,
0:27
and so on, the way you do it is real simple.
0:29
There's an example right here is you create an inner class called config,
0:33
and then you set those elements those validations there.
0:37
So let's go over here and go back to our cake and we'll say,
0:41
class config. What we want to say is any string strip the white space.
0:48
So, for example, if the topping somehow got chocolate space or space chocolate,
0:53
really, when you just wanted to say chocolate and of course,
0:56
we could go write code to say if there's a string here and make sure that
1:00
we take away any spaces, tabs,
1:02
new lines and so on in there but we don't have to.
1:05
We can just use this. But we're going to put this on all of our
1:09
various classes like this. If you want to do further validation like the price has
1:16
to be greater than zero, but less than some number or whatever,
1:20
that makes sense. You know,
1:21
you can add all those types of things here.
1:23
We're just going to keep it simple.
1:24
But I wanted to point out that we do have these really advanced automatic conversion and
1:28
validation features of pydantic that we can layer in with this inner config class.