Pydantic

A data validation library for python

  • Makes it super easy to define schemas & export it to JSON

Syntax

class User:
	def __init__(self, name:str, age: int, email: str):
		self.name = name
		self.age = age
		self.email = email
  • in pydantic, we just list the attributes and types
rom pydantic import BaseModel, Field
 
class pUser(BaseModel):
    name: str
    age: int
    email: str
foo = User(name="Joe", age=32, email="joe@email.com")
print(foo.name) # 'Joe'
  • this will fail due to type validation (age is invalid)
foo_p = pUser(name="Jane", age="bar", email="jane@gmail.com")
ValidationError: 1 validation error for pUser
age
  value is not a valid integer (type=type_error.integer)
  • we can make a class
    • we can nest these data structures!
class Class(BaseModel):
	students: List[pUser]
 
obj = Class(
    students=[pUser(name="Jane", age=32, email="jane@gmail.com")]
) 

Field

  • used for customizing model fields
    • setting default values
    • adding validation and constraints
      • max_length, gt,lt etc
    • providing metadata
      • title, description, examples
    • serialization/deserialization
      • u can use arguments like alias to map an incoming data field name to a different name in the Pydantic model, or repr to control whether the field is included in the model’s string
from uuid import uuid4
from pydantic import BaseModel, Field
 
class User(BaseModel):
    # Field with a default value and description
    name: str = Field(default="John Doe", description="The name of the user")
 
    # Field with a dynamic default using default_factory
    # Note: default and default_factory are mutually exclusive
    id: str = Field(default_factory=lambda: uuid4().hex)
 
    # Field with validation constraints (must be between 0 and 120)
    age: int = Field(gt=0, lt=120)
 
user = User(age=42)
print(user)