106. Data Validation with pydantic
Pydantic is a powerful library for data validation in Python. It allows you to define Python data models using type annotations, and automatically validates the data according to the specified types. This makes it easier to enforce data consistency and ensure that the data being processed is in the correct format.
Here are some Python code snippets demonstrating how to perform data validation using Pydantic.
1. Basic Data Validation
from pydantic import BaseModel
class Person(BaseModel):
name: str
age: int
# Valid Data
person = Person(name="Alice", age=30)
print(person)
# Invalid Data (will raise validation error)
# person = Person(name="Alice", age="thirty") # Raises errorExplanation: In this example, the Person model validates that name must be a string and age must be an integer. If you try to pass invalid data (like age="thirty"), it will raise a ValidationError.
2. Field Constraints with constr and conint
from pydantic import BaseModel, constr, conint
class Product(BaseModel):
name: constr(min_length=3)
price: conint(gt=0)
# Valid Data
product = Product(name="Laptop", price=1000)
print(product)
# Invalid Data (will raise validation error)
# product = Product(name="TV", price=-100) # Raises errorExplanation: Here, constr(min_length=3) ensures that the product name has at least 3 characters, and conint(gt=0) ensures that the price is greater than zero.
3. Default Values and Optional Fields
Explanation: The Optional type allows fields to be omitted, and a default value can be provided (e.g., published_year defaults to None).
4. Nested Models
Explanation: You can nest models within each other to represent more complex structures, like an address within a user model. Pydantic ensures the nested model data is also validated.
5. Data Validation with Regex
Explanation: You can use regular expressions to enforce specific patterns for string fields, such as usernames that must be alphanumeric and between 6 to 12 characters long.
6. Custom Validation with @root_validator
Explanation: The @root_validator decorator allows you to validate fields across the entire model. In this case, it checks that the discount cannot be greater than or equal to the price.
7. Enforcing Enum Types
Explanation: Enums can be used to restrict a field to a predefined set of values. In this case, the role field is restricted to one of the Role enum values.
8. Datetime Validation
Explanation: Pydantic automatically validates datetime fields to ensure the data conforms to the correct datetime format.
9. Validator with Custom Logic
Explanation: The @validator decorator is used to apply custom validation logic to specific fields. In this case, it ensures the price is greater than 0.
10. Parsing Data with JSON Input
Explanation: Pydantic allows you to parse data directly from JSON using parse_raw(). If the data does not match the expected structure, it raises a validation error.
Key Concepts:
BaseModel: All Pydantic models must inherit fromBaseModel.validator: Custom field validation using the@validatordecorator.@root_validator: Validation that operates on multiple fields at once.constr,conint, etc.: Field constraints for limiting values like minimum/maximum lengths, ranges, etc.Enums: Enforce a set of constant values for a field.
Datetime Validation: Pydantic can automatically validate and parse datetime fields.
JSON Parsing: Pydantic supports easy parsing from JSON data into models.
Pydantic makes it simple to define clear data models and apply validation rules, ensuring that data conforms to your expectations and improving data integrity in your application.
Last updated