SlideShare a Scribd company logo
2
Most read
3
Most read
8
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

PPTX
Document validation in MongoDB 3.2
PDF
PyConUK2013 - Validated documents on MongoDB with Ming
PDF
MongoDB.pdf
PPTX
Mongoose and MongoDB 101
PPTX
Document Validation in MongoDB 3.2
PDF
MongoDB user group israel May
PDF
Nodejs mongoose
PPTX
Dev Jumpstart: Build Your First App with MongoDB
Document validation in MongoDB 3.2
PyConUK2013 - Validated documents on MongoDB with Ming
MongoDB.pdf
Mongoose and MongoDB 101
Document Validation in MongoDB 3.2
MongoDB user group israel May
Nodejs mongoose
Dev Jumpstart: Build Your First App with MongoDB

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

PDF
Declarative Data Modeling in Python
PDF
REST Web API with MongoDB
PPTX
Python Code Camp for Professionals 4/4
PDF
Pyconie 2012
PPTX
moma-django overview --> Django + MongoDB: building a custom ORM layer
PDF
MongoDB Introduction talk at Dr Dobbs Conference, MongoDB Evenings at Bangalo...
PPT
Rapid and Scalable Development with MongoDB, PyMongo, and Ming
PDF
Avoiding Bad Database Surprises: Simulation and Scalability - Steven Lott
PDF
MongoDB World 2016: From the Polls to the Trolls: Seeing What the World Think...
KEY
MongoDB at ZPUGDC
ODP
Mongokit presentation mongofr-2010
PPTX
Build restful ap is with python and flask
PDF
Python RESTful webservices with Python: Flask and Django solutions
KEY
MongoDB hearts Django? (Django NYC)
PPTX
MongoDB with Mongoose_ Schemas, CRUD & Error Handling.pptx
PDF
Сергей Матвеенко: MongoEngine: NoORM for NoSQL
PDF
Strongly Typed Languages and Flexible Schemas
PDF
Building your first app with MongoDB
PPTX
Webinar: Strongly Typed Languages and Flexible Schemas
PDF
Create a python program that creates a database in MongoDB using API.pdf
Declarative Data Modeling in Python
REST Web API with MongoDB
Python Code Camp for Professionals 4/4
Pyconie 2012
moma-django overview --> Django + MongoDB: building a custom ORM layer
MongoDB Introduction talk at Dr Dobbs Conference, MongoDB Evenings at Bangalo...
Rapid and Scalable Development with MongoDB, PyMongo, and Ming
Avoiding Bad Database Surprises: Simulation and Scalability - Steven Lott
MongoDB World 2016: From the Polls to the Trolls: Seeing What the World Think...
MongoDB at ZPUGDC
Mongokit presentation mongofr-2010
Build restful ap is with python and flask
Python RESTful webservices with Python: Flask and Django solutions
MongoDB hearts Django? (Django NYC)
MongoDB with Mongoose_ Schemas, CRUD & Error Handling.pptx
Сергей Матвеенко: MongoEngine: NoORM for NoSQL
Strongly Typed Languages and Flexible Schemas
Building your first app with MongoDB
Webinar: Strongly Typed Languages and Flexible Schemas
Create a python program that creates a database in MongoDB using API.pdf
Ad

More from techprane (17)

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

Recently uploaded (20)

PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPT
Teaching material agriculture food technology
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Electronic commerce courselecture one. Pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
DOCX
The AUB Centre for AI in Media Proposal.docx
NewMind AI Weekly Chronicles - August'25 Week I
20250228 LYD VKU AI Blended-Learning.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
NewMind AI Monthly Chronicles - July 2025
Encapsulation_ Review paper, used for researhc scholars
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Diabetes mellitus diagnosis method based random forest with bat algorithm
Chapter 3 Spatial Domain Image Processing.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Teaching material agriculture food technology
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Electronic commerce courselecture one. Pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
MYSQL Presentation for SQL database connectivity
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Unlocking AI with Model Context Protocol (MCP)
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
The AUB Centre for AI in Media Proposal.docx

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: "john@example.com", 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: "john@example.com", 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