Skip to content

Manifest Checks: Semantic Models#

Note

The below checks require manifest.json to be present.

Functions:

Name Description
check_semantic_model_based_on_non_public_models

Semantic models should be based on public models only.

check_semantic_model_based_on_non_public_models #

Semantic models should be based on public models only.

Receives at execution time:

Name Type Description
models list[ModelNode]

List of ModelNode objects parsed from manifest.json.

semantic_model SemanticModelNode

The SemanticModelNode object to check.

Other Parameters (passed via config file):

Name Type Description
description str | None

Description of what the check does and why it is implemented.

exclude str | None

Regex pattern to match the semantic model path (i.e the .yml file where the semantic model is configured). Semantic model paths that match the pattern will not be checked.

include str | None

Regex pattern to match the semantic model path (i.e the .yml file where the semantic model is configured). Only semantic model paths that match the pattern will be checked.

severity Literal[error, warn] | None

Severity level of the check. Default: error.

Example(s):

manifest_checks:
    - name: check_semantic_model_based_on_non_public_models

Source code in src/dbt_bouncer/checks/manifest/check_semantic_models.py
@check
def check_semantic_model_based_on_non_public_models(semantic_model, ctx):
    """Semantic models should be based on public models only.

    Receives:
        models (list[ModelNode]): List of ModelNode objects parsed from `manifest.json`.
        semantic_model (SemanticModelNode): The SemanticModelNode object to check.

    Other Parameters:
        description (str | None): Description of what the check does and why it is implemented.
        exclude (str | None): Regex pattern to match the semantic model path (i.e the .yml file where the semantic model is configured). Semantic model paths that match the pattern will not be checked.
        include (str | None): Regex pattern to match the semantic model path (i.e the .yml file where the semantic model is configured). Only semantic model paths that match the pattern will be checked.
        severity (Literal["error", "warn"] | None): Severity level of the check. Default: `error`.

    Example(s):
        ```yaml
        manifest_checks:
            - name: check_semantic_model_based_on_non_public_models
        ```

    """
    models_by_id = (
        ctx.models_by_unique_id
        if ctx.models_by_unique_id
        else {m.unique_id: m for m in ctx.models}
    )
    non_public_upstream_dependencies = []
    for model in getattr(semantic_model.depends_on, "nodes", []) or []:
        model_obj = models_by_id.get(model)
        if not model_obj:
            continue
        if (
            model_obj.resource_type == "model"
            and model_obj.package_name == semantic_model.package_name
            and model_obj.access
            and model_obj.access.value != "public"
        ):
            non_public_upstream_dependencies.append(model_obj.name)

    if non_public_upstream_dependencies:
        fail(
            f"Semantic model `{semantic_model.name}` is based on a model(s) that is not public: {non_public_upstream_dependencies}."
        )