Convert YAML schemas to Pydantic models
Project description
๐งฌ yaml2pydantic
A powerful, extensible schema compiler that turns YAML/JSON definitions into dynamic Pydantic models โ with full support for:
- โ Custom types
- โ Field and model-level validators
- โ Custom serializers (field- and model-level)
- โ Default values
- โ Nested models
- โ Reusable shared components
- โ Auto-importing of components
- โ Built-in type system
๐ View the full documentation
Built for teams that want to define models declaratively in YAML but leverage all the power of Pydantic v2.
โจ Key Features
| Feature | Description |
|---|---|
| ๐ YAML/JSON to Pydantic | Define your models in YAML or JSON, and compile them into Pydantic models. |
| ๐งฑ Custom Types | Extend your schema with types like Money, MonthYear, etc. |
| ๐งช Validators | Use reusable or model-specific validators (check_positive, etc.) |
| ๐จ Serializers | Serialize fields or models however you want (Money โ "R$ 10,00") |
| ๐ Field Defaults | Fully supports defaults for primitive and complex types |
| โ๏ธ Dynamic ModelFactory | All logic for building Pydantic models is centralized and pluggable |
| ๐ Registry-based architecture | Types, validators, serializers all managed through shared registries |
| ๐ Auto-importing | Components are automatically imported from components directory |
| ๐๏ธ Built-in Types | Support for common types like Money, MonthYear, and all Pydantic primitives |
๐ ๏ธ Installation
# Clone the repository
git clone https://github.com/banduk/yaml2pydantic.git
cd yaml2pydantic
# Set up the development environment
make setup
# Activate the virtual environment
source .venv/bin/activate
๐งญ Project Structure
yaml2pydantic/
โโโ main.py # Entry point to load + test models
โโโ models/ # YAML/JSON model definitions
โ โโโ user.yaml
โโโ components/ # Shared reusable logic
โ โโโ serializers/ # Custom serialization functions
โ โ โโโ money.py # Money-specific serializers
โ โโโ types/ # Custom types (Money, MonthYear)
โ โ โโโ money.py # Money type implementation
โ โ โโโ monthyear.py # MonthYear type implementation
โ โโโ validators/ # Custom validation logic
โ โโโ email.py # Email-related validators
โ โโโ numeric.py # Numeric validators
โโโ core/ # Core schema engine
โโโ factory.py # ModelFactory that builds Pydantic models
โโโ loader.py # Loads YAML, JSON, or dict input
โโโ registry.py # Shared registries for types, validators, serializers
โโโ types.py # TypeRegistry
โโโ validators.py # ValidatorRegistry
โโโ serializers.py # SerializerRegistry
๐ ๏ธ How It Works
1. ๐ Define Your Model in YAML
User:
fields:
name:
type: str
max_length: 10 # Built-in Pydantic field constraints
age:
type: int
ge: 0 # Built-in Pydantic field constraints
validators:
- check_positive # Custom validator
email:
type: Optional[str]
default: null
birthday:
type: datetime
address:
type: Address
default:
street: "Unknown"
city: "Unknown"
zip: "00000"
balance:
type: Money
default: 0
serializers:
- money_as_string # Custom serializer
start_date:
type: MonthYear
default: "03/2025"
Address:
fields:
street:
type: str
city:
type: str
zip:
type: str
pattern: "^[0-9]{5}(-[0-9]{4})?$"
2. ๐๏ธ Compile to Pydantic
from yaml2pydantic import ModelFactory
# Load and compile the model
factory = ModelFactory()
User = factory.create_model("User", "models/user.yaml")
# Use it like any Pydantic model
user = User(
name="John Doe",
age=30,
email="john@example.com",
birthday="1990-01-01",
address={
"street": "123 Main St",
"city": "Anytown",
"zip": "12345"
},
balance=1000,
start_date="03/2025"
)
๐งช Testing
# Run all tests
make test
# Run tests with coverage
python -m pytest --cov=yaml2pydantic
๐ Documentation
# Build the documentation
make docs
# View the documentation
make docs-serve
๐งน Code Quality
# Run the linter
make lint
# Format the code
make format
# Run type checking
python -m mypy .
๐ค Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Run the tests and checks:
make all - Submit a pull request
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐งช Development Setup
This project uses modern Python development tools for a fast and efficient development experience.
Project Structure
yaml2pydantic/
โโโ main.py # Entry point to load + test models
โโโ models/ # YAML/JSON model definitions
โ โโโ user.yaml
โโโ components/ # Shared reusable logic
โ โโโ serializers/ # Custom serialization functions
โ โ โโโ money.py # Money-specific serializers
โ โโโ types/ # Custom types (Money, MonthYear)
โ โ โโโ money.py # Money type implementation
โ โ โโโ monthyear.py # MonthYear type implementation
โ โโโ validators/ # Custom validation logic
โ โโโ email.py # Email-related validators
โ โโโ numeric.py # Numeric validators
โโโ score/ # Core schema engine
โโโ factory.py # ModelFactory that builds Pydantic models
โโโ loader.py # Loads YAML, JSON, or dict input
โโโ registry.py # Shared registries for types, validators, serializers
โโโ types.py # TypeRegistry
โโโ validators.py # ValidatorRegistry
โโโ serializers.py # SerializerRegistry
Common Commands
# Set up the environment
make setup
# Run tests
make test
# Run linters
make lint
# Format code
make format
# Type check
python -m mypy .
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 yaml2pydantic-0.0.2.tar.gz.
File metadata
- Download URL: yaml2pydantic-0.0.2.tar.gz
- Upload date:
- Size: 10.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d8e298ec5ed75e7e5253f97b68e5de0ef7a4511c003551de670e6ded8d8d1ccd
|
|
| MD5 |
f8cf045d4f2be6effe368c7f6b2f3869
|
|
| BLAKE2b-256 |
78650096bf605753d33662e8114d5d715c75e1c333e67c3bc8c207e2b63fcf9c
|
File details
Details for the file yaml2pydantic-0.0.2-py3-none-any.whl.
File metadata
- Download URL: yaml2pydantic-0.0.2-py3-none-any.whl
- Upload date:
- Size: 9.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a2876474180c8b1e8c14b0492ee534998412a998134001b23a980379b7edc614
|
|
| MD5 |
3d2746bc44983d48c14e1a1d35763b51
|
|
| BLAKE2b-256 |
08a735111b53222db9380bfe73b5bfd7b971bdf173436b83ce4fe6d00d5f5057
|