Skip to content

model_kind_validation

Per-category field validation framework using KindPolicy and FieldPolicy rules.

Severity module-attribute

Severity = Literal['error', 'warning']

Cardinality module-attribute

Cardinality = Literal['single', 'list']

kind_policy_registry module-attribute

kind_policy_registry = KindPolicyRegistry()

NormalizedModelStyle module-attribute

NormalizedModelStyle = Annotated[
    str, AfterValidator(_strip_value)
]

NormalizedTag module-attribute

NormalizedTag = Annotated[str, AfterValidator(_strip_value)]

FieldPolicy dataclass

Per-field validation policy for a category.

Source code in src/horde_model_reference/model_kind_validation.py
@dataclass(frozen=True)
class FieldPolicy:
    """Per-field validation policy for a category."""

    cardinality: Cardinality = "single"
    severity: Severity = "warning"

cardinality class-attribute instance-attribute

cardinality: Cardinality = 'single'

severity class-attribute instance-attribute

severity: Severity = 'warning'

__init__

__init__(
    cardinality: Cardinality = "single",
    severity: Severity = "warning",
) -> None

KindPolicy dataclass

Collection of field policies for a category.

Source code in src/horde_model_reference/model_kind_validation.py
@dataclass
class KindPolicy:
    """Collection of field policies for a category."""

    field_policies: dict[str, FieldPolicy] = field(default_factory=dict)

field_policies class-attribute instance-attribute

field_policies: dict[str, FieldPolicy] = field(
    default_factory=dict
)

__init__

__init__(
    field_policies: dict[str, FieldPolicy] = dict(),
) -> None

KindPolicyRegistry

Registry for category-specific validation policies.

Source code in src/horde_model_reference/model_kind_validation.py
class KindPolicyRegistry:
    """Registry for category-specific validation policies."""

    def __init__(self) -> None:
        """Initialize an empty KindPolicyRegistry."""
        self._policies: dict[str, KindPolicy] = {}

    def register(self, category: str, policy: KindPolicy) -> None:
        """Register a KindPolicy for a specific category.

        Args:
            category: The model reference category to associate with the policy.
            policy: The KindPolicy instance containing field validation rules.

        Raises:
            ValueError: If a policy is already registered for the given category.

        """
        if category in self._policies:
            raise ValueError(f"Policy already registered for {category!r}")
        self._policies[category] = policy

    def get(self, category: str) -> KindPolicy | None:
        """Retrieve the KindPolicy for a given category, or None if no policy is registered.

        Args:
            category: The model reference category to look up.

        Returns:
            The KindPolicy associated with the category, or None if not found.

        """
        return self._policies.get(category)

_policies instance-attribute

_policies: dict[str, KindPolicy] = {}

__init__

__init__() -> None

Initialize an empty KindPolicyRegistry.

Source code in src/horde_model_reference/model_kind_validation.py
def __init__(self) -> None:
    """Initialize an empty KindPolicyRegistry."""
    self._policies: dict[str, KindPolicy] = {}

register

register(category: str, policy: KindPolicy) -> None

Register a KindPolicy for a specific category.

Parameters:

  • category (str) –

    The model reference category to associate with the policy.

  • policy (KindPolicy) –

    The KindPolicy instance containing field validation rules.

Raises:

  • ValueError

    If a policy is already registered for the given category.

Source code in src/horde_model_reference/model_kind_validation.py
def register(self, category: str, policy: KindPolicy) -> None:
    """Register a KindPolicy for a specific category.

    Args:
        category: The model reference category to associate with the policy.
        policy: The KindPolicy instance containing field validation rules.

    Raises:
        ValueError: If a policy is already registered for the given category.

    """
    if category in self._policies:
        raise ValueError(f"Policy already registered for {category!r}")
    self._policies[category] = policy

get

get(category: str) -> KindPolicy | None

Retrieve the KindPolicy for a given category, or None if no policy is registered.

Parameters:

  • category (str) –

    The model reference category to look up.

Returns:

  • KindPolicy | None

    The KindPolicy associated with the category, or None if not found.

Source code in src/horde_model_reference/model_kind_validation.py
def get(self, category: str) -> KindPolicy | None:
    """Retrieve the KindPolicy for a given category, or None if no policy is registered.

    Args:
        category: The model reference category to look up.

    Returns:
        The KindPolicy associated with the category, or None if not found.

    """
    return self._policies.get(category)

category_key

category_key(category: str | Enum) -> str

Normalize category identifiers to a string key for the registry.

Source code in src/horde_model_reference/model_kind_validation.py
def category_key(category: str | Enum) -> str:
    """Normalize category identifiers to a string key for the registry."""
    return str(category)

_strip_value

_strip_value(value: str) -> str
Source code in src/horde_model_reference/model_kind_validation.py
def _strip_value(value: str) -> str:
    return value.strip()