🧬 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
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 |
🚀 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.
Metadata
Release files for yaml2pydantic 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| yaml2pydantic-0.1.0.tar.gz | 12.7 kB | Without provenance | Details for yaml2pydantic-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| yaml2pydantic-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for yaml2pydantic-0.1.0-py3-none-any.whl |
Total release size: 23.6 kB
Release files / yaml2pydantic-0.1.0.tar.gz
| Download URL | yaml2pydantic-0.1.0.tar.gz |
|---|---|
| Size | 12.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
cec80f4a4abd7fb4485e682e5ac1dd7b597f3fa64401fbac725e30dbc74fe31f
|
|
BLAKE2b-256 checksum How to use checksums |
60af9a0e8b3572ab41b16ce79620b8f3b94efe8926c4a9770c510570bb3edfae
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.12
|
Release files / yaml2pydantic-0.1.0-py3-none-any.whl
| Download URL | yaml2pydantic-0.1.0-py3-none-any.whl |
|---|---|
| Size | 11.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
aebc1312ad6b0b97230b9a47bf04f10e691244eb9e58ffd4c8b1f5be17e642f3
|
|
BLAKE2b-256 checksum How to use checksums |
559510c53a072fd7f76841cb4951456e2b6abe0995d7fa6b6c2d4d56d02781c0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.12
|
Security for yaml2pydantic 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.