Skip to content

Validate and serialize YAML config files with Pydantic

License

Notifications You must be signed in to change notification settings

stefanofusai/pyamldantic

Repository files navigation

pyamldantic

Validate and serialize YAML config files with Pydantic, with support for environment variables.

This package uses uv for project management. To get started, ensure that uv is installed on your machine and updated to the 0.5.6 version. Detailed installation instructions for uv can be found here.

Installation

uv add pyamldantic

Usage

config.yaml

database:
  host: $DATABASE_HOST
  name: $DATABASE_NAME
  password: $DATABASE_PASSWORD
  port: $DATABASE_PORT
  user: $DATABASE_USER
  timeout: $DATABASE_TIMEOUT?
environment: development
is_debug: true

config.py

from pyamldantic import YAMLConfig
from pydantic import BaseModel, SecretStr

class DatabaseSchema(BaseModel):
    host: str
    name: str
    password: SecretStr
    port: int
    user: str
    ssl: bool = False
    timeout: int | None = None

class Schema(BaseModel):
    database: DatabaseSchema
    environment: str
    is_debug: bool

config = YAMLConfig.load("config.yaml", schema=Schema)

main.py

from .config import config

if __name__ == "__main__":
    print(f"Initializing {config.environment} environment...")
    ...

Development

uv sync --frozen --group=development
uv run --frozen pre-commit install --install-hooks
uv run --frozen pre-commit install --hook-type=commit-msg

Testing

uv sync --frozen --group=testing
uv run --frozen pytest

Acknowledgments

This project was inspired by envyaml.

Contributing

Contributions are welcome! To get started, please refer to our contribution guidelines.

Issues

If you encounter any problems while using this package, please open a new issue here.

About

Validate and serialize YAML config files with Pydantic

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages