Day 28: Pydantic. validator.
Hi 👋 I am Hud, a postdoc for engineering data science at the AI Manufacturing Center in Laramie, Wyoming. My funding is running out (AaAaaA !), so while I am actively looking for a new job, instead of doing the 205th coding certificate to prove my worthiness — I thought I’d do design challenges and document how I spend my time upskilling so other engineers can do the same.
Nowadays, certificates are everywhere. Documenting small upskill projects that you can later show off is the best way to get recognition as a professional engineer.
This is day 28 of a 30-day design challenge. Follow along and let me know if you get stuck!
TL;DR tasks
Download the provided code for the challenge.
Task 1: Add validation for available_tickets
- Implement a validation check for available_tickets in the EventCreate Pydantic model to ensure it cannot be negative.
Task 2: Add validation for event end date
- Implement a validation check in the EventCreate Pydantic model to ensure the end date of an event is not before the starting date.
Task 3: Add validation for customer email address
- Implement a validation check in the TicketCreate Pydantic model to ensure the customer email address is a valid email address.
Task 4: Test validation checks
- Write test cases to validate that the validation checks for available_tickets, event end date, and customer email address are working as expected.
Task 5: Ensure proper error messages
- Ensure that the validation checks provide clear and informative error messages when invalid data is provided.
Task 6: Test invalid data scenarios
- Test scenarios where invalid data is provided for available_tickets, event end date, and customer email address to verify that the validation properly handles such cases.
Task 7: Validate the entire API
- Run the API and validate that the added validation checks prevent the use of invalid data and provide appropriate feedback.
Task 8: Review and optimize
- Review the code and make any necessary optimizations or improvements to the validation checks for better efficiency and maintainability.
The solution
This is the final challenge for our API series! If you haven’t, follow along all the API challenges from here onwards. We want to beef up our validation — With pydantic
we do have the validation that throws an error if the type is wrong.
But what if we want more complicated checks? For example, available_tickets
should not be -542 — so how do we do that:
With pydantic
, just use the @validator
decorator! The validator is used to validate and transform data before it is assigned to a field. The @validator
decorator is used to define a validation function for a specific field.
For example, in the EventCreate
model, the available_tickets
field is validated using the validate_available_tickets
function. This function checks if the value of available_tickets
is less than 0 and raises a ValueError
if it is. If the value is valid, the function returns the value.
Similarly, the end_date
field is validated using the validate_end_date
function. This function checks if the value of end_date
is later than the value of start_date
and raises a ValueError
if it is not. If the value is valid, the function returns the value.
As a bonus, in the TicketCreate
model, the customer_email
field is validated using the validate_customer_email
function. This function checks if the value of customer_email
is a valid email address using a regular expression. If the value is not a valid email address, the function raises a ValueError
. If the value is valid, the function returns the value. It’s a pretty simple example for production, but the idea is that you can build as complicated a check as you need.
And there we have it! Did you try something else? Let me know in the comments.
Conclusion
Congratulations! You finished Day 28 from the 30-day design challenge.
If you have reached this far, you know how to:
- Use pydantic’s
@validator
decorator for custom validations
That’s the final challenge for APIs! Check out the day 29 challenge for bridge patterns!
Also, you can access the full 30-day GitHub repository here.
💡 My goal here is to help engineering data scientists upskill in design. I’d like to hear from you! Was this helpful? Anything I can improve? Connect with me on LinkedIn | Medium