Python

How to Structure and Validate Data with Pydantic Schema

When it comes to setting up and checking our data in Python programs, Pydantic Schema is a valuable tool. This guide will show us how to use Pydantic to create organized data models and make sure that our data is accurate. Using Pydantic Schema, we can straightforwardly define our data and also ensure that it’s correct and reliable. Pydantic Schema provides our accurate data through careful validation processes.

Method 1: Efficient Data Structuring and Validation with Pydantic Schema in Python

In the era of Python programming, maintaining the data integrity is important. Pydantic Schema emerges as an effective solution to structure and validate the data, ensuring its accuracy and reliability throughout the application. This example will guide us through the process of using Pydantic Schema to establish a solid data foundation, displaying its ease of use and effectiveness in data validation.

To understand the Pydantic Schema, Pydantic is a widely used library that simplifies the data validation and structuring in Python applications. It offers an elegant way to define the shape of our data using Python classes while also providing built-in validation capabilities. Whether we are building APIs, working with configuration files, or processing the user input, Pydantic Schema helps in maintaining a consistent and error-free data.

To get started, the installation and setup of Pydantic is required. So, ensure that we have Pydantic installed. We can install it using pip:

!pip install pydantic

!pip install pydantic[email]

from pydantic import BaseModel,EmailStr

With Pydantic installed, let’s create a scenario where we build a simple application to manage the user information.

Begin by defining a data model using Pydantic Schema. Consider a scenario where we need to handle the user information including their name, age, and email. Here’s how we can structure the data model using Pydantic:

class UserInfo(BaseModel):

  name: str

  age: int

  email: EmailStr

In this example, the “UserInfo” is a Pydantic model class that specifies the expected structure of the user data. It has three fields: name (string), age (integer), and email (validated email string using “EmailStr”).

Once the data model is defined, then comes the data validation and usage where we can use it to validate the incoming data and ensure its accuracy. Consider a situation where we receive the user data from an external source such as a form submission or an API request. We can create an instance of the “UserInfo” model and pass the received data for validation:

data = {

  "name": "Bobby",

  "age": 28,

  "email": "[email protected]"

}

user = UserInfo(**data)

In this code, the “**received_data” syntax unveils the dictionary and passes its values as keyword arguments to the “UserInfo” class constructor. Pydantic automatically validates the data based on the defined model structure. If any data doesn’t match the expected types or formats, Pydantic raises a validation error.

Once the data is validated, we can access the validated values through the instance attributes, just like we would do with any Python object:

print("User's Name:", user.name)

print("User's Age:", user.age)

print("User's Email:", user.email)

By accessing these attributes, we are guaranteed to have accurate and validated data that will stick/adhere to the specified structure.

When the data fails to meet the validation criteria, Pydantic throws a ValidationError containing a full information about the problems. This can be quite helpful in recognizing and fixing the data quality issues. Here’s an example on how we can handle the validation errors:

from pydantic import ValidationError

invalid_data = {

  "name": "Bob",

  "age": "thirty",

  "email": "invalid-email"

}

try:

  user = UserInfo(**invalid_data)

except ValidationError as e:

  print("Validation Error:", e.errors())

In this example, we will attempt to create a “UserInfo” instance with invalid age and email values. This raises a ValidationError which provides a list of validation errors and their corresponding details.

The code for the example with the observed output is as follows:

!pip install pydantic

!pip install pydantic[email]

from pydantic import BaseModel,EmailStr

class UserInfo(BaseModel):

  name: str

  age: int
 
  email: EmailStr

data = {

  "name": "Bobby",

  "age": 28,

  "email": "[email protected]"

}

user = UserInfo(**data)

print("User's Name:", user.name)

print("User's Age:", user.age)

print("User's Email:", user.email)

from pydantic import ValidationError

invalid_data = {

  "name": "Bob",

  "age": "thirty",

  "email": "invalid-email"

}

try:

  user = UserInfo(**invalid_data)

except ValidationError as e:

  print("Validation Error:", e.errors())

Pydantic Schema aids the Python developers to structure and validate the data effortlessly and enhances the data accuracy and reliability within the applications. By defining the data models using Pydantic classes and their built-in validation, we can make sure that our application works with high-quality consistency. This example highlights the simplicity and effectiveness of Pydantic Schema, illustrating how it contributes to effective data management and application performance.

Method 2: Making the Data Reliable with Pydantic Schema in Python

In the world of programming with Python, keeping the data accurate is very important. Pydantic Schema is like a helpful guide that arranges and checks the data to make sure it’s correct. In this special example, we’ll go through how Pydantic Schema works to create neat data structures and ensure accuracy through careful checking.

The initial step is installing and starting the Python environment. Before we begin, we make sure that Pydantic is ready to use. We just need to do a simple installation using the following command:

!pip install pydantic

from pydantic import BaseModel, EmailStr

For this example, we create a program that deals with user information. Let’s start creating the plan and design the data model. Imagine that we need to organize the information about the students in school or the employees in the offices to keep track of the monthly leave they have taken. Their data could be name, number of leaves, and email. Here’s how we do it with Pydantic:

class StudentInformation(BaseModel):

  name: str

  no_of_leaves: int

  email: EmailStr

To design the data model, the “StudentInformation” piece is a Pydantic model. It tells us how the data should look. It has three parts: name (a name in words), number of leaves (a number for leaves), and email (an email address that’s checked using “EmailStr”).

To validate and check using the data, Pydantic Schema checks if our data has the information in the right place. Imagine that we receive some information from a form or an application. To make sure it’s right, we can use the Pydantic Schema like this:

received_info = {

  "name": "Vikram",

  "no_of_leaves": 45,

  "email": "[email protected]"

}

user_info = StudentInformation(**received_info)

Pydantic Schema looks at the data and checks if it fits the way it should. If something’s not right, Pydantic lets us know. Get the right information to use the validated data. Once the data passes the checks and is right, we can use it or access it with its attributes.

print("User's Name:", user_info.name)

print("User's leaves:", user_info.no_of_leaves)

print("User's Email:", user_info.email)

To handle the ValidationErrors, imagine if we had invalid user input, then Pydantic Schema notices and tells us what’s wrong. For example, we may follow the following step where we can make the data with the invalid inputs and then check if Pydantic will raise an error.

from pydantic import ValidationError

wrong_info = {

  "name": "David",

  "no_of_leaves": "twenty",

  "email": "invalid-address"

}

try:

  user_info =StudentInformation(**wrong_info)

except ValidationError as e:

  print("Wrong Infor:", e.errors())

The code for the previously-mentioned example with its resulted input is as follows:

!pip install pydantic

from pydantic import BaseModel, EmailStr

class StudentInformation(BaseModel):

  name: str

  no_of_leaves: int

  email: EmailStr

received_info = {

  "name": "Vikram",

  "no_of_leaves": 45,

  "email": "[email protected]"

}

user_info = StudentInformation(**received_info)

print("User's Name:", user_info.name)

print("User's leaves:", user_info.no_of_leaves)

print("User's Email:", user_info.email)

from pydantic import ValidationError

wrong_info = {

  "name": "David",

  "no_of_leaves": "twenty",

  "email": "invalid-address"

}

try:

  user_info =StudentInformation(**wrong_info)

except ValidationError as e:

  print("Wrong Infor:", e.errors())

Conclusion

Pydantic Schema makes sure that our data is accurate and tidy. Our program works with good data using Pydantic classes to create the data rules and letting Pydantic Schema check the data. The examples that are discussed in this article show how Pydantic Schema keeps our data accurate and our Python program running smoothly.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.