Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Building Data Science Applications with FastAPI - Second Edition

You're reading from  Building Data Science Applications with FastAPI - Second Edition

Product type Book
Published in Jul 2023
Publisher Packt
ISBN-13 9781837632749
Pages 422 pages
Edition 2nd Edition
Languages
Author (1):
François Voron François Voron
Profile icon François Voron

Table of Contents (21) Chapters

Preface 1. Part 1: Introduction to Python and FastAPI
2. Chapter 1: Python Development Environment Setup 3. Chapter 2: Python Programming Specificities 4. Chapter 3: Developing a RESTful API with FastAPI 5. Chapter 4: Managing Pydantic Data Models in FastAPI 6. Chapter 5: Dependency Injection in FastAPI 7. Part 2: Building and Deploying a Complete Web Backend with FastAPI
8. Chapter 6: Databases and Asynchronous ORMs 9. Chapter 7: Managing Authentication and Security in FastAPI 10. Chapter 8: Defining WebSockets for Two-Way Interactive Communication in FastAPI 11. Chapter 9: Testing an API Asynchronously with pytest and HTTPX 12. Chapter 10: Deploying a FastAPI Project 13. Part 3: Building Resilient and Distributed Data Science Systems with FastAPI
14. Chapter 11: Introduction to Data Science in Python 15. Chapter 12: Creating an Efficient Prediction API Endpoint with FastAPI 16. Chapter 13: Implementing a Real-Time Object Detection System Using WebSockets with FastAPI 17. Chapter 14: Creating a Distributed Text-to-Image AI System Using the Stable Diffusion Model 18. Chapter 15: Monitoring the Health and Performance of a Data Science System 19. Index 20. Other Books You May Enjoy

Adding custom data validation with Pydantic

Up to now, we’ve seen how to apply basic validation to our models through Field arguments or the custom types provided by Pydantic. In a real-world project, though, you’ll probably need to add your own custom validation logic for your specific case. Pydantic allows this by defining validators, which are methods on the model that can be applied at the field level or the object level.

Applying validation at the field level

This is the most common case: having a validation rule for a single field. To define a validation rule in Pydantic, we just have to write a static method on our model and decorate it with the validator decorator. As a reminder, decorators are syntactic sugar, allowing the wrapping of a function or a class with common logic without compromising readability.

The following example checks a birth date by verifying that the person is not more than 120 years old:

chapter04_custom_validation_01.py

from...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}