Validating With Decorators
00:00
Using Validation Decorators to Validate Functions While BaseModel
is Pydantic’s bread and butter class for validating data schemas, you can also use Pydantic to validate function arguments using the @validate_call
decorator.
00:14 This allows you to create robust functions with informative type errors without having to manually implement validation logic. To see how this works, let’s suppose you’re writing a function that sends invoices to clients after they’ve made a purchase.
00:29 Your function takes in the client’s name, email, items purchased, and total billing amount, and it constructs and sends them an email. You’ll need to validate all of these inputs because getting them wrong could result in the email not being sent, being misformatted, or the client being invoiced incorrectly.
00:47 On screen, you’ll see a function to achieve this.
00:51
First, you import the dependencies needed to write and annotated send_invoice()
.
01:03
You then create send_invoice()
decorated with the @validate_call
decorator. Before executing send_invoice()
, @validate_call
ensures that each input conforms to your annotations.
01:15
In this case, validate_call
checks whether client_name
has at least one character, client_email
is properly formatted, items_purchased
is a list of strings, and amount_owed
is a positive float.
01:27
If any of the inputs don’t conform to the annotation, Pydantic will throw an error similar to one you’ve seen already with BaseModel
. If all the inputs are valid, send_invoice()
creates a string and simulates sending it to your client with time.sleep()
.
01:56
You may have noticed that client_name
is annotated with Python’s Annotated
type.
02:02
In general, you can use Annotated
when you want to provide metadata about a function argument. Pydantic recommends using Annotated
when you need to validate a function argument that has metadata specified by Field
.
02:15
If you use default_factory
to assign a default value to your function argument, you should assign the argument directly to a Field
instance.
02:24
You can see an example of this in Pydantic’s documentation. To see the @validate_call
decorator and send_invoice()
in action, open a new REPL and run the code seen on screen.
02:39
First, you import send_invoice()
, and then you pass in invalid function arguments.
03:00
The @validate_call
decorator recognizes this and throws errors telling you that client_name
needs at least one character, client_email
is invalid, items_purchased
should contain strings, and amount_owed
should be greater than zero.
03:13
But when you pass in valid inputs, send_invoice()
runs as expected.
03:36
While the @validate_call
decorator isn’t as flexible as BaseModel
, you can still use it to apply powerful validation to your function arguments.
03:45
This saves you time and lets you avoid writing boilerplate type checking and validation logic. If you’ve done this kind of thing before, you know how cumbersome writing assert
statements for each of your function arguments can be.
03:57
For many use cases, @validate_call
takes care of this for you. In the final section of the course, you’ll learn how you can use Pydantic for settings management, and configuration.
Become a Member to join the conversation.