𧬠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 |
π§ͺ Validators |
Use reusable or model-specific validators ( |
π¨ Serializers |
Serialize fields or models however you want ( |
π 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 |
π Quick Start (For Users)ΒΆ
InstallationΒΆ
pip install yaml2pydantic
Basic UsageΒΆ
Define your model in YAML:
# models/user.yaml
User:
fields:
name:
type: str
max_length: 10
age:
type: int
ge: 0
email:
type: Optional[str]
default: null
Use it in your Python code:
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"
)
Advanced Usage ExampleΒΆ
Hereβs a more comprehensive example showing the full power of yaml2pydantic:
# models/user.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})?$"
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"
)
Advanced FeaturesΒΆ
π οΈ Development Guide (For Contributors)ΒΆ
Project SetupΒΆ
# 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
Development WorkflowΒΆ
Create a new feature branch:
git checkout -b feature/your-feature-name
Make your changes and run tests:
make test
Run code quality checks:
make lint make format python -m mypy .
Update documentation:
make docs
Submit a pull request
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
# Type check
make type-check
# Security check
make security-check
Or just run
make all-checks
π LicenseΒΆ
This project is licensed under the MIT License - see the LICENSE file for details.
π€ ContributingΒΆ
We welcome contributions! Please read our Contributing Guide for details on our code of conduct and the process for submitting pull requests.
π DocumentationΒΆ
For detailed documentation, visit our documentation site.