SlideShare a Scribd company logo
2
Most read
5
Most read
14
Most read
Samuel Folasayo
MongoDB Schema Validation with Pydantic
Data Governance, Integrity and Consistency with Pydantic & MongoDB
Joe Nyirenda
What is MongoDB Schema Validation?
Ensures data complies with a predefined set of rules(data types etc.)
Maintains data integrity
Helps enforces data governance
Can help optimise schema size
It can be applied when creating or updating collections.
What is Pydantic?
Python library used for data validation and parsing
Enforces data types and constraints
Defining models based on Python type annotations
Provide error handling
Provides conversion between different data formats, like JSON or
dictionaries
Pros and Cons of Pydantic + Schema Validation
Pros
Enforces data validity before storing
Ensures code reliability
Double-layer validation
Greater security
Data integrity
Robust validation
Cons
Potential overhead
Complexity in setup
Inflexible schema
Configuring Schema Validation
Can be performed at collection
creation time or to an already existing
collection
db.createCollection ("users", {
validator: {
$jsonSchema : {
bsonType: "object",
required: ["name", "email"],
properties: {
name: {
bsonType: "string",
description: "must be a string and is required" ,
maxLength: 50
},
email: {
bsonType: "string",
description: "must be a string and is required" ,
pattern: "^S+@S+.S+$" // Enforces valid
email pattern
},
age: {
bsonType: "int",
description: "must be an integer and optional" ,
minimum: 18,
maximum: 120
}
}
}
},
validationAction: "error"
})
Apply Schema to Existing Collection
Use collMod command to update
schema validation rules if collection
exists
db.runCommand({
collMod: "users",
validator: {
$jsonSchema: {
bsonType: "object",
required: ["name", "email"],
properties: {
name: {
bsonType: "string",
description: "must be a string and is required",
maxLength: 50
},
email: {
bsonType: "string",
description: "must be a string and is required",
pattern: "^S+@S+.S+$"
},
age: {
bsonType: "int",
description: "must be an integer and optional",
minimum: 18,
maximum: 120
}
}
}
},
validationAction: "error"
})
After applying the schema, try inserting documents to see how MongoDB enforces
the validation rules.
Testing the Schema
db.users.insertOne({
name: "John Doe",
email: "john@example.com",
age: 30
})
This will work because it complies with the schema.
MongoDB enforces the validation rules
Testing the Schema: invalid document
Invalid Document Insertion:
db.users.insertOne({
name: "John Doe",
email: "john@example.com",
age: 17
})
This will fail because age is less than 18.
Demo: minimalist web form
Tools:
FastAPI: Python web framework
for building APIs
Motor: Asynchronous MongoDB
driver.
Pydantic: Validating and parsing
data.
Jinja2: HTML templating engine
Define the structure and validation rules for incoming data
Example for a user model:
Defining Pydantic Models
class UserModel(BaseModel):
name: str = Field(..., max_length=50)
email: EmailStr
age: Optional[int]= Field(None, ge=18, le=120)
Motor connects asynchronously to MongoDB:
Connecting to MongoDB
from motor.motor_asyncio import AsyncIOMotorClient
from fastapi import FastAPI, HTTPException
app = FastAPI()
# MongoDB connection
client = AsyncIOMotorClient("mongodb://localhost:27017")
db = client.mydatabase
users_collection = db.users
Here, we’ll create two main routes:
GET route to serve the form HTML.
Building Routes
from fastapi import Request, Form
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
# Initialize Jinja2 template rendering
templates = Jinja2Templates(directory="templates")
@app.get("/", response_class=HTMLResponse)
async def get_form(request: Request):
return templates.TemplateResponse("form.html", {"request": request})
post route to handle form submission,
Validate the data using the Pydantic model
Save validated data to MongoDB
Building Routes
@app.post("/submit")
async def submit_form(
request: Request,
name: str = Form(...),
email: str = Form(...),
age: Optional[int] = Form(None)
):
try:
# Validate the data using the Pydantic model
user = UserModel(name=name, email=email, age=age)
# Insert into MongoDB
await users_collection.insert_one(user.dict())
# Success response
return templates.TemplateResponse("success.html", {"request":
request, "user": user.dict()})
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
Handling Validation Errors in Code
from fastapi import FastAPI, HTTPException
from motor.motor_asyncio import AsyncIOMotorClient
from pydantic import BaseModel, EmailStr, Field
app = FastAPI()
client =
AsyncIOMotorClient("mongodb://localhost:27017")
db = client.mydatabase
users_collection = db.users
class UserModel(BaseModel):
name: str = Field(..., max_length=50)
email: EmailStr
age: Optional[int] = Field(None, ge=18, le=120)
@app.post("/submit")
async def submit_user(user: UserModel):
try:
await users_collection.insert_one(user.dict())
return {"message": "User inserted successfully"}
except Exception as e:
raise HTTPException(status_code=400,
detail=str(e))
Form Template (templates/form.html):
Displaying HTML Form
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"
>
<title>User Form</title>
</head>
<body>
<h1>Enter User Information
</h1>
<form action="/submit" method="post">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required><br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required><br><br>
<label for="age">Age:</label>
<input type="number" id="age" name="age"><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Success Template (templates/success.html):
Displaying HTML Form
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form Submitted</title>
</head>
<body>
<h1>Submission Successful</h1>
<p>User Details:</p>
<ul>
<li><strong>Name:</strong> {{ user.name }}</li>
<li><strong>Email:</strong> {{ user.email }}</li>
<li><strong>Age:</strong> {{ user.age }}</li>
</ul>
</body>
</html>
Results Displayed on Browser
Conclusion
Pydantic provides an extra layer of validation
Schema validation enforces data type and domain specific rules
for data model
Schema validation great for mature applications

More Related Content

Similar to Implementing Schema Validation in MongoDB with Pydantic (20)

PDF
Declarative Data Modeling in Python
Joshua Forman
 
PDF
REST Web API with MongoDB
MongoDB
 
PPTX
Python Code Camp for Professionals 4/4
DEVCON
 
PDF
Pyconie 2012
Yaqi Zhao
 
PPTX
moma-django overview --> Django + MongoDB: building a custom ORM layer
Gadi Oren
 
PDF
MongoDB Introduction talk at Dr Dobbs Conference, MongoDB Evenings at Bangalo...
Prasoon Kumar
 
PPT
Rapid and Scalable Development with MongoDB, PyMongo, and Ming
Rick Copeland
 
PDF
Avoiding Bad Database Surprises: Simulation and Scalability - Steven Lott
PyData
 
PDF
MongoDB World 2016: From the Polls to the Trolls: Seeing What the World Think...
MongoDB
 
KEY
MongoDB at ZPUGDC
Mike Dirolf
 
ODP
Mongokit presentation mongofr-2010
namlook
 
PPTX
Build restful ap is with python and flask
Jeetendra singh
 
PDF
Python RESTful webservices with Python: Flask and Django solutions
Solution4Future
 
KEY
MongoDB hearts Django? (Django NYC)
Mike Dirolf
 
PDF
Сергей Матвеенко: MongoEngine: NoORM for NoSQL
it-people
 
PDF
Strongly Typed Languages and Flexible Schemas
Norberto Leite
 
PDF
Building your first app with MongoDB
Norberto Leite
 
PPTX
Webinar: Strongly Typed Languages and Flexible Schemas
MongoDB
 
PDF
Create a python program that creates a database in MongoDB using API.pdf
murtuzadahadwala3
 
PPTX
Webinar: Building Your First App
MongoDB
 
Declarative Data Modeling in Python
Joshua Forman
 
REST Web API with MongoDB
MongoDB
 
Python Code Camp for Professionals 4/4
DEVCON
 
Pyconie 2012
Yaqi Zhao
 
moma-django overview --> Django + MongoDB: building a custom ORM layer
Gadi Oren
 
MongoDB Introduction talk at Dr Dobbs Conference, MongoDB Evenings at Bangalo...
Prasoon Kumar
 
Rapid and Scalable Development with MongoDB, PyMongo, and Ming
Rick Copeland
 
Avoiding Bad Database Surprises: Simulation and Scalability - Steven Lott
PyData
 
MongoDB World 2016: From the Polls to the Trolls: Seeing What the World Think...
MongoDB
 
MongoDB at ZPUGDC
Mike Dirolf
 
Mongokit presentation mongofr-2010
namlook
 
Build restful ap is with python and flask
Jeetendra singh
 
Python RESTful webservices with Python: Flask and Django solutions
Solution4Future
 
MongoDB hearts Django? (Django NYC)
Mike Dirolf
 
Сергей Матвеенко: MongoEngine: NoORM for NoSQL
it-people
 
Strongly Typed Languages and Flexible Schemas
Norberto Leite
 
Building your first app with MongoDB
Norberto Leite
 
Webinar: Strongly Typed Languages and Flexible Schemas
MongoDB
 
Create a python program that creates a database in MongoDB using API.pdf
murtuzadahadwala3
 
Webinar: Building Your First App
MongoDB
 

More from techprane (17)

PDF
REDIS + FastAPI: Implementing a Rate Limiter
techprane
 
PDF
Performance Optimization MongoDB: Compound Indexes
techprane
 
PPTX
SSO with Social Login Integration & FastAPI Simplified
techprane
 
PDF
A Beginner's Guide to Tortoise ORM and PostgreSQL
techprane
 
PDF
Boost Your API with Asynchronous Programming in FastAPI
techprane
 
PDF
Top 10 Network Troubleshooting Commands.pdf
techprane
 
PPTX
Using jq to Process and Query MongoDB Logs
techprane
 
PPTX
How to Integrate PostgreSQL with Prometheus
techprane
 
PPTX
10 Basic Git Commands to Get You Started
techprane
 
PPTX
Top Linux 10 Commands for Windows Admins
techprane
 
PPTX
Implementing full text search with Apache Solr
techprane
 
PPTX
How to Overcome Doubts as a New Developer(Imposter Syndrome)
techprane
 
PPTX
How to Use JSONB in PostgreSQL for Product Attributes Storage
techprane
 
PDF
A Beginners Guide to Building MicroServices with FastAPI
techprane
 
PPTX
Storing Large Image Files in MongoDB Using GRIDFS
techprane
 
PPTX
Open Source Mapping with Python, and MongoDB
techprane
 
PPTX
Learning MongoDB Aggregations in 10 Minutes
techprane
 
REDIS + FastAPI: Implementing a Rate Limiter
techprane
 
Performance Optimization MongoDB: Compound Indexes
techprane
 
SSO with Social Login Integration & FastAPI Simplified
techprane
 
A Beginner's Guide to Tortoise ORM and PostgreSQL
techprane
 
Boost Your API with Asynchronous Programming in FastAPI
techprane
 
Top 10 Network Troubleshooting Commands.pdf
techprane
 
Using jq to Process and Query MongoDB Logs
techprane
 
How to Integrate PostgreSQL with Prometheus
techprane
 
10 Basic Git Commands to Get You Started
techprane
 
Top Linux 10 Commands for Windows Admins
techprane
 
Implementing full text search with Apache Solr
techprane
 
How to Overcome Doubts as a New Developer(Imposter Syndrome)
techprane
 
How to Use JSONB in PostgreSQL for Product Attributes Storage
techprane
 
A Beginners Guide to Building MicroServices with FastAPI
techprane
 
Storing Large Image Files in MongoDB Using GRIDFS
techprane
 
Open Source Mapping with Python, and MongoDB
techprane
 
Learning MongoDB Aggregations in 10 Minutes
techprane
 
Ad

Recently uploaded (20)

PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
PDF
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PDF
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
PDF
🚀 Let’s Build Our First Slack Workflow! 🔧.pdf
SanjeetMishra29
 
PDF
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PDF
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
🚀 Let’s Build Our First Slack Workflow! 🔧.pdf
SanjeetMishra29
 
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
Ad

Implementing Schema Validation in MongoDB with Pydantic

  • 1. Samuel Folasayo MongoDB Schema Validation with Pydantic Data Governance, Integrity and Consistency with Pydantic & MongoDB Joe Nyirenda
  • 2. What is MongoDB Schema Validation? Ensures data complies with a predefined set of rules(data types etc.) Maintains data integrity Helps enforces data governance Can help optimise schema size It can be applied when creating or updating collections.
  • 3. What is Pydantic? Python library used for data validation and parsing Enforces data types and constraints Defining models based on Python type annotations Provide error handling Provides conversion between different data formats, like JSON or dictionaries
  • 4. Pros and Cons of Pydantic + Schema Validation Pros Enforces data validity before storing Ensures code reliability Double-layer validation Greater security Data integrity Robust validation Cons Potential overhead Complexity in setup Inflexible schema
  • 5. Configuring Schema Validation Can be performed at collection creation time or to an already existing collection db.createCollection ("users", { validator: { $jsonSchema : { bsonType: "object", required: ["name", "email"], properties: { name: { bsonType: "string", description: "must be a string and is required" , maxLength: 50 }, email: { bsonType: "string", description: "must be a string and is required" , pattern: "^S+@S+.S+$" // Enforces valid email pattern }, age: { bsonType: "int", description: "must be an integer and optional" , minimum: 18, maximum: 120 } } } }, validationAction: "error" })
  • 6. Apply Schema to Existing Collection Use collMod command to update schema validation rules if collection exists db.runCommand({ collMod: "users", validator: { $jsonSchema: { bsonType: "object", required: ["name", "email"], properties: { name: { bsonType: "string", description: "must be a string and is required", maxLength: 50 }, email: { bsonType: "string", description: "must be a string and is required", pattern: "^S+@S+.S+$" }, age: { bsonType: "int", description: "must be an integer and optional", minimum: 18, maximum: 120 } } } }, validationAction: "error" })
  • 7. After applying the schema, try inserting documents to see how MongoDB enforces the validation rules. Testing the Schema db.users.insertOne({ name: "John Doe", email: "[email protected]", age: 30 }) This will work because it complies with the schema.
  • 8. MongoDB enforces the validation rules Testing the Schema: invalid document Invalid Document Insertion: db.users.insertOne({ name: "John Doe", email: "[email protected]", age: 17 }) This will fail because age is less than 18.
  • 9. Demo: minimalist web form Tools: FastAPI: Python web framework for building APIs Motor: Asynchronous MongoDB driver. Pydantic: Validating and parsing data. Jinja2: HTML templating engine
  • 10. Define the structure and validation rules for incoming data Example for a user model: Defining Pydantic Models class UserModel(BaseModel): name: str = Field(..., max_length=50) email: EmailStr age: Optional[int]= Field(None, ge=18, le=120)
  • 11. Motor connects asynchronously to MongoDB: Connecting to MongoDB from motor.motor_asyncio import AsyncIOMotorClient from fastapi import FastAPI, HTTPException app = FastAPI() # MongoDB connection client = AsyncIOMotorClient("mongodb://localhost:27017") db = client.mydatabase users_collection = db.users
  • 12. Here, we’ll create two main routes: GET route to serve the form HTML. Building Routes from fastapi import Request, Form from fastapi.responses import HTMLResponse from fastapi.templating import Jinja2Templates # Initialize Jinja2 template rendering templates = Jinja2Templates(directory="templates") @app.get("/", response_class=HTMLResponse) async def get_form(request: Request): return templates.TemplateResponse("form.html", {"request": request})
  • 13. post route to handle form submission, Validate the data using the Pydantic model Save validated data to MongoDB Building Routes @app.post("/submit") async def submit_form( request: Request, name: str = Form(...), email: str = Form(...), age: Optional[int] = Form(None) ): try: # Validate the data using the Pydantic model user = UserModel(name=name, email=email, age=age) # Insert into MongoDB await users_collection.insert_one(user.dict()) # Success response return templates.TemplateResponse("success.html", {"request": request, "user": user.dict()}) except Exception as e: raise HTTPException(status_code=400, detail=str(e))
  • 14. Handling Validation Errors in Code from fastapi import FastAPI, HTTPException from motor.motor_asyncio import AsyncIOMotorClient from pydantic import BaseModel, EmailStr, Field app = FastAPI() client = AsyncIOMotorClient("mongodb://localhost:27017") db = client.mydatabase users_collection = db.users class UserModel(BaseModel): name: str = Field(..., max_length=50) email: EmailStr age: Optional[int] = Field(None, ge=18, le=120) @app.post("/submit") async def submit_user(user: UserModel): try: await users_collection.insert_one(user.dict()) return {"message": "User inserted successfully"} except Exception as e: raise HTTPException(status_code=400, detail=str(e))
  • 15. Form Template (templates/form.html): Displaying HTML Form <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0" > <title>User Form</title> </head> <body> <h1>Enter User Information </h1> <form action="/submit" method="post"> <label for="name">Name:</label> <input type="text" id="name" name="name" required><br><br> <label for="email">Email:</label> <input type="email" id="email" name="email" required><br><br> <label for="age">Age:</label> <input type="number" id="age" name="age"><br><br> <input type="submit" value="Submit"> </form> </body> </html>
  • 16. Success Template (templates/success.html): Displaying HTML Form <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Form Submitted</title> </head> <body> <h1>Submission Successful</h1> <p>User Details:</p> <ul> <li><strong>Name:</strong> {{ user.name }}</li> <li><strong>Email:</strong> {{ user.email }}</li> <li><strong>Age:</strong> {{ user.age }}</li> </ul> </body> </html>
  • 18. Conclusion Pydantic provides an extra layer of validation Schema validation enforces data type and domain specific rules for data model Schema validation great for mature applications