Skip to content

_bundles_validate

rhiza_hooks._bundles_validate

Structural validation of a template-bundles document.

These functions operate on an already-loaded mapping (see :mod:rhiza_hooks._bundles_fetch for how the document is obtained) and report problems as lists of human-readable error strings. They never perform I/O, except :func:validate_template_bundles, which is a thin convenience wrapper that loads a local file and then validates it.

validate_selected_bundles(templates, bundles, missing_message)

Validate that each requested template exists in bundles and is well-formed.

Shared by the local-file and remote-fetch paths; they differ only in the "not found" wording, supplied via missing_message.

Parameters:

Name Type Description Default
templates set[str]

Template names requested in the rhiza config.

required
bundles dict[Any, Any]

The bundles mapping from a template-bundles document.

required
missing_message Callable[[str], str]

Builds the error string for a template absent from bundles.

required

Returns:

Type Description
list[str]

List of error messages (empty if every requested template is valid).

Source code in rhiza_hooks/_bundles_validate.py
def validate_selected_bundles(
    templates: set[str],
    bundles: dict[Any, Any],
    missing_message: Callable[[str], str],
) -> list[str]:
    """Validate that each requested template exists in ``bundles`` and is well-formed.

    Shared by the local-file and remote-fetch paths; they differ only in the
    "not found" wording, supplied via ``missing_message``.

    Args:
        templates: Template names requested in the rhiza config.
        bundles: The ``bundles`` mapping from a template-bundles document.
        missing_message: Builds the error string for a template absent from ``bundles``.

    Returns:
        List of error messages (empty if every requested template is valid).
    """
    errors: list[str] = []
    bundle_names: set[str] = set(bundles.keys())

    for template in templates:
        if template not in bundle_names:
            errors.append(missing_message(template))

    for template in templates:
        if template in bundles:
            errors.extend(_validate_bundle_structure(template, bundles[template], bundle_names))

    return errors

validate_template_bundles(bundles_path, templates_to_check=None)

Validate template bundles configuration.

Parameters:

Name Type Description Default
bundles_path Path

Path to template-bundles.yml

required
templates_to_check set[str] | None

Optional set of template names to validate. If None, validate all.

None

Returns:

Type Description
tuple[bool, list[str]]

Tuple of (success, error_messages)

Source code in rhiza_hooks/_bundles_validate.py
def validate_template_bundles(bundles_path: Path, templates_to_check: set[str] | None = None) -> tuple[bool, list[str]]:
    """Validate template bundles configuration.

    Args:
        bundles_path: Path to template-bundles.yml
        templates_to_check: Optional set of template names to validate. If None, validate all.

    Returns:
        Tuple of (success, error_messages)
    """
    # Load YAML file
    loaded = load_local_bundles(bundles_path)
    if loaded.data is None:
        return False, loaded.errors
    # data is narrowed to dict[Any, Any] by the `is None` guard above.
    data = loaded.data

    # Validate top-level fields
    errors = validate_top_level_fields(data)
    if errors:
        return False, errors

    # Validate bundles section
    bundles = data.get("bundles", {})
    if not isinstance(bundles, dict):
        return False, ["'bundles' must be a dictionary"]

    if templates_to_check is not None:
        # Validate only the requested subset (existence + structure).
        errors.extend(
            validate_selected_bundles(
                templates_to_check,
                bundles,
                lambda t: f"Template '{t}' specified in .rhiza/template.yml not found in bundles",
            )
        )
    else:
        errors.extend(_validate_all_bundles(data, bundles))

    return len(errors) == 0, errors

validate_top_level_fields(data)

Validate required top-level fields.

Source code in rhiza_hooks/_bundles_validate.py
def validate_top_level_fields(data: dict[Any, Any]) -> list[str]:
    """Validate required top-level fields."""
    errors = []
    required_fields = {"version", "bundles"}
    for field in required_fields:
        if field not in data:
            errors.append(f"Missing required field: {field}")
    return errors