Skip to main content
Editor

Python Pydantic: Validate Data Like a Pro

Advanced25 min6 exercises120 XP
0/6 exercises

Every application deals with messy data. User forms with missing fields, API responses with wrong types, config files with invalid values. Data validation is the practice of checking that data matches your expectations before you use it.

The Pydantic library is the most popular tool for this in Python. To understand what it does, you'll first build validation systems by hand using dataclasses and __post_init__ - the same concepts that power Pydantic under the hood - and then rebuild them with Pydantic itself.

By the end, you'll know how to validate types, enforce constraints, build custom validators, create nested models, and serialize data - all patterns you'll recognize when you use Pydantic in a real project.

How Do You Validate Data in a Dataclass?

Python's dataclasses give you structured data containers. By adding a __post_init__ method, you can run validation logic right after the object is created. This is exactly how Pydantic's validators work conceptually.

Basic validation with __post_init__
from dataclasses import dataclass
@dataclass
class User:
name: str
age: int
email: str
def __post_init__(self):
if not isinstance(self.name, str) or len(self.name) == 0:
raise ValueError('name must be a non-empty string')
if not isinstance(self.age, int) or self.age < 0:
raise ValueError('age must be a non-negative integer')
if '@' not in self.email:
raise ValueError('email must contain @')
# Valid user
user = User(name='Alice', age=30, email='alice@example.com')
print(f'{user.name}, age {user.age}')
# Invalid user - uncomment to see the error:
# bad_user = User(name='', age=30, email='alice@example.com')
Output
Alice, age 30

__post_init__ runs automatically after __init__. If the data is invalid, we raise a ValueError with a clear message. This is the foundation of all data validation.

What Is Type Coercion and Why Does It Matter?

Real-world data is messy. An API might send age as "25" (a string) instead of 25 (an integer). Type coercion means automatically converting data to the expected type when possible.

Type coercion in __post_init__
from dataclasses import dataclass
@dataclass
class Product:
name: str
price: float
quantity: int
def __post_init__(self):
# Coerce types
self.name = str(self.name)
try:
self.price = float(self.price)
except (TypeError, ValueError):
raise ValueError(f'price must be a number, got {self.price!r}')
try:
self.quantity = int(self.quantity)
except (TypeError, ValueError):
raise ValueError(f'quantity must be an integer, got {self.quantity!r}')
# Validate constraints
if self.price < 0:
raise ValueError('price must be non-negative')
if self.quantity < 0:
raise ValueError('quantity must be non-negative')
# These all work thanks to coercion
p1 = Product(name='Widget', price='9.99', quantity='3')
print(f'{p1.name}: ${p1.price} x {p1.quantity}')
print(f'Types: {type(p1.price).__name__}, {type(p1.quantity).__name__}')
Output
Widget: $9.99 x 3
Types: float, int

The string "9.99" was automatically converted to the float 9.99, and "3" became the integer 3. This is exactly what Pydantic does by default.

How Do You Build Reusable Validators?

When you have validation rules that apply to many fields or many classes, you can extract them into reusable validator functions. This mirrors Pydantic's @field_validator decorator pattern.

Reusable validator functions
from dataclasses import dataclass
# Reusable validator functions
def validate_non_empty(value: str, field_name: str) -> str:
value = str(value).strip()
if len(value) == 0:
raise ValueError(f'{field_name} must not be empty')
return value
def validate_range(value, field_name: str, min_val=None, max_val=None):
if min_val is not None and value < min_val:
raise ValueError(f'{field_name} must be >= {min_val}, got {value}')
if max_val is not None and value > max_val:
raise ValueError(f'{field_name} must be <= {max_val}, got {value}')
return value
def validate_email(value: str) -> str:
value = str(value).strip()
if '@' not in value or '.' not in value.split('@')[-1]:
raise ValueError(f'Invalid email: {value!r}')
return value.lower()
@dataclass
class Employee:
name: str
email: str
salary: float
def __post_init__(self):
self.name = validate_non_empty(self.name, 'name')
self.email = validate_email(self.email)
self.salary = float(self.salary)
self.salary = validate_range(self.salary, 'salary', min_val=0)
emp = Employee(name='Bob', email='BOB@Company.Com', salary='75000')
print(f'{emp.name}: {emp.email}, ${emp.salary}')
Output
Bob: bob@company.com, $75000.0

Notice how each validator both validates and transforms the data - validate_email lowercases the email, validate_non_empty strips whitespace. This pattern of validate-then-transform is central to Pydantic.

How Do You Handle Nested Data?

Real data is often nested - an Order contains a Customer, which contains an Address. Pydantic handles this elegantly with nested BaseModels. We can do the same with nested dataclasses.

Nested dataclasses with validation
from dataclasses import dataclass
@dataclass
class Address:
street: str
city: str
zip_code: str
def __post_init__(self):
if not self.zip_code.isdigit() or len(self.zip_code) != 5:
raise ValueError(f'zip_code must be 5 digits, got {self.zip_code!r}')
@dataclass
class Customer:
name: str
address: Address
def __post_init__(self):
# If address is passed as a dict, convert it
if isinstance(self.address, dict):
self.address = Address(**self.address)
# Direct construction
customer1 = Customer(
name='Alice',
address=Address(street='123 Main St', city='Springfield', zip_code='12345')
)
print(f'{customer1.name} lives in {customer1.address.city}')
# From a dict (like JSON data)
customer2 = Customer(
name='Bob',
address={'street': '456 Oak Ave', 'city': 'Portland', 'zip_code': '97201'}
)
print(f'{customer2.name} lives in {customer2.address.city}')
Output
Alice lives in Springfield
Bob lives in Portland

The key trick is in Customer.__post_init__: if address is a raw dictionary, we convert it to an Address object. This mirrors Pydantic's automatic dict-to-model conversion.

How Do You Convert Models to Dictionaries and JSON?

Pydantic models have .model_dump() and .model_dump_json() methods. We can add similar capabilities to our dataclasses using dataclasses.asdict() and the json module.

Serialization and deserialization
from dataclasses import dataclass, asdict
import json
@dataclass
class Config:
host: str
port: int
debug: bool
def __post_init__(self):
self.port = int(self.port)
self.debug = bool(self.debug)
if not 1 <= self.port <= 65535:
raise ValueError(f'port must be 1-65535, got {self.port}')
def to_dict(self) -> dict:
return asdict(self)
def to_json(self) -> str:
return json.dumps(self.to_dict(), indent=2)
@classmethod
def from_dict(cls, data: dict):
return cls(**data)
config = Config(host='localhost', port='8080', debug=True)
print(config.to_dict())
print()
print(config.to_json())
print()
# Round-trip: dict -> Config -> dict
data = {'host': '0.0.0.0', 'port': 443, 'debug': False}
config2 = Config.from_dict(data)
print(f'{config2.host}:{config2.port} (debug={config2.debug})')
Output
{'host': 'localhost', 'port': 8080, 'debug': True}

{
  "host": "localhost",
  "port": 8080,
  "debug": true
}

0.0.0.0:443 (debug=False)

The to_dict, to_json, and from_dict methods mirror Pydantic's model_dump(), model_dump_json(), and model_validate(). This pattern makes your data models easy to save, send over networks, and reconstruct.

The Same Models in Real Pydantic

Here is a user with a nested address, written with Pydantic. Each hand-written piece from earlier has a built-in equivalent: range checks become Field(ge=0, le=120), coercion happens on its own, a nested dict becomes a model, and to_dict/to_json are model_dump()/model_dump_json().

Validation, nesting and serialization with Pydantic
from pydantic import BaseModel, Field, ValidationError
class Address(BaseModel):
city: str
pincode: str = Field(pattern=r'^\d{6}$')
class User(BaseModel):
name: str
age: int = Field(ge=0, le=120)
address: Address
# Type coercion: "21" becomes 21, and the nested dict becomes an Address
user = User(name='Asha', age='21', address={'city': 'Pune', 'pincode': '411001'})
print(user)
print(user.model_dump())
print(user.model_dump_json())
# Every problem is reported at once
try:
User(name='Ben', age=-5, address={'city': 'Delhi', 'pincode': 'abc'})
except ValidationError as e:
print(e.error_count(), 'errors')
for err in e.errors():
print(err['loc'], err['msg'])
Output
name='Asha' age=21 address=Address(city='Pune', pincode='411001')
{'name': 'Asha', 'age': 21, 'address': {'city': 'Pune', 'pincode': '411001'}}
{"name":"Asha","age":21,"address":{"city":"Pune","pincode":"411001"}}
2 errors
('age',) Input should be greater than or equal to 0
('address', 'pincode') String should match pattern '^\d{6}$'

Unlike the hand-written __post_init__ checks, which stop at the first problem, a ValidationError lists every field that failed, with its location (('address', 'pincode')) and a readable message.

Dataclass vs Pydantic: At a Glance

Plain dataclass (no validation)
@dataclass
class User:
    name: str
    age: int

# Accepts anything - no checks!
user = User(name=123, age="not a number")
# No error, but data is wrong
Validated dataclass (Pydantic-style)
@dataclass
class User:
    name: str
    age: int

    def __post_init__(self):
        self.name = str(self.name).strip()
        self.age = int(self.age)
        if self.age < 0:
            raise ValueError("age < 0")

Practice Exercises

Basic Validated Model
Write Code

Create a Temperature dataclass with a single field celsius (float). In __post_init__, coerce celsius to float and validate that it's above absolute zero (-273.15). If invalid, raise ValueError.

Create two instances: Temperature(celsius='100') and Temperature(celsius=-10). Print each one's celsius value.

from dataclasses import dataclass
# Create the Temperature dataclass with validation
# Create instances and print their celsius values
Multiple Field Validation
Write Code

Create a PasswordPolicy dataclass with fields min_length (int), require_uppercase (bool), and require_digits (bool).

Add a method validate_password(self, password: str) -> bool that returns True if the password meets all enabled requirements, False otherwise.

Create a policy with min_length=8, require_uppercase=True, require_digits=True.

Test it with these passwords and print the results:

  • "hello" (should be False)
  • "HelloWorld1" (should be True)
  • "ALLCAPS123" (should be True)
  • from dataclasses import dataclass
    # Create PasswordPolicy dataclass with validate_password method
    # Create policy and test passwords
    Predict the Output: Coercion Behavior
    Predict Output

    Read the code carefully and predict exactly what it prints.

    from dataclasses import dataclass
    
    @dataclass
    class Score:
        value: float
        label: str
    
        def __post_init__(self):
            self.value = float(self.value)
            self.label = str(self.label).upper()
            if self.value > 100:
                self.value = 100.0
    
    s1 = Score(value="85", label="math")
    s2 = Score(value=150, label="science")
    print(f"{s1.label}: {s1.value}")
    print(f"{s2.label}: {s2.value}")
    from dataclasses import dataclass
    @dataclass
    class Score:
    value: float
    label: str
    def __post_init__(self):
    self.value = float(self.value)
    self.label = str(self.label).upper()
    if self.value > 100:
    self.value = 100.0
    s1 = Score(value="85", label="math")
    s2 = Score(value=150, label="science")
    print(f"{s1.label}: {s1.value}")
    print(f"{s2.label}: {s2.value}")
    Nested Validated Models
    Write Code

    Create two dataclasses:

    1. Item with name (str) and price (float). Validate that price >= 0.

    2. Order with customer (str) and items (list). In __post_init__, convert any dicts in the items list to Item objects. Add a method total() that returns the sum of all item prices.

    Create an order:

    order = Order(
        customer='Alice',
        items=[{'name': 'Book', 'price': 12.99}, {'name': 'Pen', 'price': 1.50}]
    )

    Print the customer name and then the total.

    from dataclasses import dataclass
    # Create Item dataclass with price validation
    # Create Order dataclass with dict-to-Item conversion
    # and a total() method
    # Create order and print results
    Fix the Bug: Validation Order
    Fix the Bug

    This UserProfile class has a bug in its validation logic. The type coercion happens *after* the range check, so string inputs cause a TypeError instead of being validated properly. Fix the order of operations in __post_init__.

    The code should print the profile when given age="25".

    from dataclasses import dataclass
    @dataclass
    class UserProfile:
    username: str
    age: int
    score: float
    def __post_init__(self):
    # Bug: validation before coercion!
    if self.age < 0 or self.age > 150:
    raise ValueError(f'Invalid age: {self.age}')
    if self.score < 0 or self.score > 100:
    raise ValueError(f'Invalid score: {self.score}')
    self.age = int(self.age)
    self.score = float(self.score)
    self.username = str(self.username).strip().lower()
    profile = UserProfile(username=' Alice ', age='25', score='99.5')
    print(f'{profile.username}: age={profile.age}, score={profile.score}')
    Build a Mini Validator Framework
    Write Code

    Build a ValidatedModel base class with a to_dict() method that returns a dictionary of all fields, and a from_dict(cls, data) classmethod that creates an instance from a dictionary.

    Then create a Book class that inherits from ValidatedModel with fields title (str), pages (int), and rating (float). Validate in __post_init__ that pages > 0 and 0 <= rating <= 5.

    Create a book from a dict, print its to_dict() output, and then reconstruct it with from_dict and print the title.

    from dataclasses import dataclass, asdict, fields
    # Create ValidatedModel base class
    # with to_dict() and from_dict() methods
    # Create Book subclass with validation
    # Create a book, print to_dict, reconstruct, print title

    Harder problems

    More problems on this topic: Classes

    Related Tutorials

    Was this tutorial helpful?