Kubernetes monitoring tools
Share this post

Introduction

Data validation is very important when you work with complex data structures. Imagine you want to read some YAML file that contains important data that is used to configure your application. You want to make sure that all fields have the proper type and values that make sense. I’m sure nobody wants to have a database record where user name is 25 and the age is Natalia :).

Pydantic is an open-source Python library that allows developers to validate data easily and quickly. In this article, I want to focus on showing the potential of Pythantic – You will learn how to validate your data, promptly load a YAML file, how to pass secret values like passwords, and many many more. Enjoy!

Installation

Before we start we have to install Pydantic. Use this pip command in your console:

A simple example of Pydantic usage

In the beginning, let’s write some simple code snipped where the Pydantic library is used.

To use Pydantic validation you have to import BaseModel and create a new class that inherits from it – like in our example, the User class inherits from the BaseModel. From now we will say that the User class is a Pydantic model. For that moment, whenever you create a new object Pydantic will check if provided data has the correct type.

Example of User class object creation when provided arguments have correct types:

Example of User class object creation when provided arguments have wrong types:

Pydantic recognized that we want to use incorrect arguments and raised a proper exception that informs us what is wrong.

Pydantic validation

Pydantic gained popularity due to its ability to validate data with ease and robustness. Let’s dive into some code and figure it out!

In this example, we added a validator that checks if provided age is positive. If it’s negative proper exception raises.

Example of loading YAML file

Classes that inherit from BaseModel have a bunch of new methods. They are described in the official Pytest documentation – https://docs.pydantic.dev/usage/models/. One of them is the parse_obj method which allows us to load any object into a Pydantic model. There are many uses of this functionality – for example, loading a YAML file into the model.

Let’s collect some information about users in the users.yaml file.

Then we can read the file and use the parse_obj method to load data into the User model.

 Secret string in Pydantic

Some of the variables can be sensitive and we don’t want somebody to know them. It can be a password, login, token, and many many others. Fortunately, Pydantic has a special type for it – the SecretStr. Let me show you how it works.

 Model configuration with Config class

To configure the Pydantic model we can add the Config class inside the model. It allows us to customize our model to fit our needs the most. In this article, I want to show some of Config class capabilities but believe me – there are many many more (check the official documentation here – https://docs.pydantic.dev/usage/model_config/).

1. Set the maximum string length

2. Remove whitespaces around a string

3. Allow extra fields

Summary

Thank you so much for reading this article. I hope you enjoyed it and the examples presented here will help you understand the Pydantic library and implement new solutions into your code.

Author

Share this post

Natalia Kraszewska

Natalia Kraszewska is a Data Engineer and Python Developer at DS STREAM. Her main interests are algorithms and sharing knowledge.

Close

Send Feedback