Skip to content

Configuration

dbt_contracts.models.config

Configuration model for dbt-contracts.

Config

Bases: BaseModel

Root configuration for dbt-contracts.

Loaded from contracts/config.yaml in a dbt project.

Source code in src/dbt_contracts/models/config.py
class Config(pyd.BaseModel):
    """Root configuration for dbt-contracts.

    Loaded from ``contracts/config.yaml`` in a dbt project.
    """

    model_config = pyd.ConfigDict(extra="forbid")

    project_name: str | None = None
    dbt_project_dir: str | None = ".."
    default_server_type: str | None = "snowflake"
    generation: GenerationConfig | None = None
    validation: ValidationConfig | None = None

    @classmethod
    def from_file(cls, file_path: str) -> "Config":
        """Load configuration from a YAML file."""
        with open(file_path, encoding="utf-8") as f:
            content = f.read()
        return cls.from_string(content)

    @classmethod
    def from_string(cls, config_str: str) -> "Config":
        """Load configuration from a YAML string."""
        data = yaml.safe_load(config_str) or {}
        return cls(**data)

    def to_yaml(self) -> str:
        """Serialize configuration to a YAML string."""
        return yaml.dump(
            self.model_dump(exclude_defaults=True, exclude_none=True, by_alias=True),
            sort_keys=False,
            allow_unicode=True,
        )

from_file(file_path) classmethod

Load configuration from a YAML file.

Source code in src/dbt_contracts/models/config.py
@classmethod
def from_file(cls, file_path: str) -> "Config":
    """Load configuration from a YAML file."""
    with open(file_path, encoding="utf-8") as f:
        content = f.read()
    return cls.from_string(content)

from_string(config_str) classmethod

Load configuration from a YAML string.

Source code in src/dbt_contracts/models/config.py
@classmethod
def from_string(cls, config_str: str) -> "Config":
    """Load configuration from a YAML string."""
    data = yaml.safe_load(config_str) or {}
    return cls(**data)

to_yaml()

Serialize configuration to a YAML string.

Source code in src/dbt_contracts/models/config.py
def to_yaml(self) -> str:
    """Serialize configuration to a YAML string."""
    return yaml.dump(
        self.model_dump(exclude_defaults=True, exclude_none=True, by_alias=True),
        sort_keys=False,
        allow_unicode=True,
    )

GenerationConfig

Bases: BaseModel

Settings for dbt artifact generation.

Source code in src/dbt_contracts/models/config.py
class GenerationConfig(pyd.BaseModel):
    """Settings for dbt artifact generation."""

    models_dir: str | None = "models/generated"
    sources_dir: str | None = "models/staging"
    generate_sources: bool | None = True
    generate_tests: bool | None = True
    header: str | None = "-- Generated by dbt-contracts. Do not edit manually."

ValidationConfig

Bases: BaseModel

Settings for contract validation.

Source code in src/dbt_contracts/models/config.py
class ValidationConfig(pyd.BaseModel):
    """Settings for contract validation."""

    cross_reference: bool | None = True
    min_status: str | None = "draft"