Getting Started with Pydantic- A Python data Validation Library
Pydantic is a very powerful data validation library, and in this article, I would give you a walkthrough of how it works and how you can use it.
Table of contents
No headings in the article.
As a web developer, you would deal with APIs when working on full-stack applications because of how ubiquitous they are. Each time you go on a rideshare app to check your location, what is happening is that the rideshare service(the client) requests your current location from third-party apps such as the Google Maps API. Hence, our location is real-time because google maps have location data that some rideshare app might not have because of how resource-intensive it would be, so they outsource in the form of an API.
You also interact with API when you want to sign in with different apps on your social media(Twitter, Facebook, Google, Github). This option is safer and more often convenient than signing in with your credentials manually. APIs are also prominent in e-commerce applications when dealing with payments of service they often use payment solutions like stripe, Paypal, Paystack etc
What is an API? APIs act as a middleman between two machines(most often a client and a server) to allow them to talk to each other. As you can see APIs are used everywhere and as a developer, it is a really important skill to have under your tool belt.
In this article, we would go over how to handle data requested from clients by using the Python data validation library, Pydantic.
When creating an API you need to have control over the information being sent, to avoid wrong data input( for example, the client can send an empty field for a name field or a string format for a number field)by forcing it into a schema. A schema designs what the data should look like so the client can give it the information it needs unless it throws an error. Python has a data validation library that addresses this issue called Pydantic.
“Pydantic is a data validation and settings management using python type annotations”- Pydantic Official Documentation
Pydantic ensures the data sent or received is what is expected unless it returns a very friendly error message( which we all appreciate 😂❤️).
Pydantic vs Python DataClasses
Python data classes are a way of writing python classes as data containers. They hold data more efficiently and elegantly without having to write so much boilerplate code and initialise every argument in our python class. Let us take a look at how a regular python class is structured against a python data class by creating a Product class for an e-commerce application:
A regular Python class :
A python DataClass :
From the example, you can see there is less boilerplate and a stricter application of the DRY(Do not Repeat Yourself) concept. Python data classes however do not support type or data validation, unlike Pydantic. In other to get started with Pydantic you would need to install it into your virtual machine.
pip install Pydantic
To use Pydantic you have to import BaseModel and then pass it as an argument in your class.
Pydantic checks whether the data given matches the schema described if it does. It returns the data otherwise an error is raised. This would be the error that would be raised if the price field is commented out from the dictionary.
raise validation_error
pydantic.error_wrappers.ValidationError: 1 validation error for Product
price
field required (type=value_error.missing)
Pydantic is a very powerful data parsing and validation library. And this was just a walkthrough of how to get started with Pydantic, for a deeper dive into how Pydnatic works you can check their official documentation. Thanks for reading
References
Pydantic Official documentation: pydantic-docs.helpmanual.io
9 Reasons Why You Should Start Using Python Dataclasses: towardsdatascience.com/9-reasons-why-you-sh..