Generate MongoDB validation schemas from Python type annotations and Pydantic models.
Project description
mongo-validations-generator
mongo-validations-generator is a lightweight Python library for generating MongoDB JSON Schema validation rules using standard Python type annotations and Pydantic models.
It allows you to define your MongoDB collection schemas declaratively with Python classes, while supporting advanced typing features like Annotated, Literal, Union, custom validation markers, and nested models.
Features
- ✅ Auto-generates
$jsonSchemavalidation for MongoDB - ✅ Support for Python type hints:
str,int,float,bool,list,None - ✅ Nested objects using Pydantic-style inheritance
- ✅ Annotated constraints using
Annotated[..., Len(...)] - ✅ Support for
Union,Optional, andLiteraltypes - ✅ Custom BSON type markers (e.g.,
Long) - ✅ Ability to ignore fields with
SchemaIgnored
🚀 Getting Started
1. Define a model
from annotated_types import Len
from mongo_validations_generator import MongoValidator, Long, SchemaIgnored
from typing import Annotated, Literal
class Product(MongoValidator):
name: str
description: str
categories: list[str]
price: Annotated[int, Long]
tags: Annotated[list[str], Len(1)]
internal_code: Annotated[str, SchemaIgnored]
status: Literal["active", "archived"]
2. Generate the schema
import json
print(json.dumps(Product.generate_validation_rules("Product"), indent=2))
output:
{
"$jsonSchema": {
"title": "Product Validation",
"bsonType": "object",
"required": [
"name",
"description",
"categories",
"price",
"tags",
"status"
],
"properties": {
"name": {
"bsonType": "string",
"description": "'name' must match schema"
},
"description": {
"bsonType": "string",
"description": "'description' must match schema"
},
"categories": {
"bsonType": "array",
"items": {
"bsonType": "string"
},
"description": "'categories' must match schema"
},
"price": {
"bsonType": "long",
"description": "'price' must match schema"
},
"tags": {
"bsonType": "array",
"items": {
"bsonType": "string"
},
"minItems": 1,
"description": "'tags' must match schema"
},
"status": {
"enum": ["active", "archived"],
"bsonType": "string",
"description": "'status' must match schema"
}
}
}
}
📦 Supported BSON Types
The following BSON types are currently supported by the schema generator:
| Python Type | BSON Type | Notes |
|---|---|---|
str |
"string" |
|
int |
"int" |
|
float |
"double" |
|
bool |
"bool" |
|
list[...] |
"array" |
Supports nested items, constraints, and unions |
None / Optional[...] |
"null" |
Supports Union[Type, None] and Optional[...] |
Annotated[int, Long] |
"long" |
Use Annotated[int, Long] to convert to "long" |
Literal[...] |
"enum" |
Will emit enum values and infer BSON type if homogeneous |
MongoValidator subclass |
"object" |
Nested objects are fully supported |
Annotated[list[T], Len(...)] |
"array" |
Adds minItems and maxItems constraints to list validation |
❗ Not supported:
dict,set,tuple,frozenset, or other complex built-in containers.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mongo_validations_generator-1.0.0.tar.gz.
File metadata
- Download URL: mongo_validations_generator-1.0.0.tar.gz
- Upload date:
- Size: 23.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
072d3bfb0a0ddee5aa06d94b171bed5aafb19687d41b8346448b554b5787f84c
|
|
| MD5 |
d0af272607750a4204a53c916321383d
|
|
| BLAKE2b-256 |
43d27ae8114bb2beb9c08715abc553cf7e92b43cbf2b21de65a36087e13cbe83
|
File details
Details for the file mongo_validations_generator-1.0.0-py3-none-any.whl.
File metadata
- Download URL: mongo_validations_generator-1.0.0-py3-none-any.whl
- Upload date:
- Size: 9.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.5.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d6e05abe0046c71e30abe3a352a8efcba44eb4a5535d9ba38621c0fe4ad04485
|
|
| MD5 |
e4ffbea8e5a28526a8b4995df4a3b473
|
|
| BLAKE2b-256 |
ac4f90c7bc1eb19db9cfac62dbae571a2095862c176002e7f6ebc4b8fdbbca1a
|