Skip to content

Validation

blackline.models.validation

In an effort to meet the requirements for the Creative Commons Attribution 4.0 International License, please be aware that the following code is based on the following work: - https://github.com/ethyca/fideslang/blob/main/src/fideslang/validation.py

The code has only been mildly modified to fit the needs of the Blackline project.

Contains all of the additional validation for the resource models.

Key

Bases: ConstrainedStr

A Key type that creates a custom constrained string.

Parameters:

Name Type Description Default
regex Pattern[str] required
Source code in BAR /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/blackline/models/validation.py
class Key(ConstrainedStr):
    """
    A Key type that creates a custom constrained string.
    """

    regex: Pattern[str] = re.compile(r"^[a-zA-Z0-9_.-]+$")

    # This overrides the default method to throw the custom ValidationError
    @classmethod
    def validate(cls, value: str) -> str:
        if not cls.regex.match(value):
            raise ValueError(
                "Key must only contain alphanumeric characters, '.', '_' or '-'."
            )

        return value

ValidationBase

Bases: BaseModel

Base class for validation models.

Parameters:

Name Type Description Default
name str required
Source code in BAR /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/blackline/models/validation.py
class ValidationBase(BaseModel):
    """
    Base class for validation models.
    """

    name: str = Field(..., exclude=True)

    class Config:
        """
        Config class for validation models.
        """

        arbitrary_types_allowed = True
        extra = "forbid"

Config

Config class for validation models.

Source code in BAR /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/blackline/models/validation.py
class Config:
    """
    Config class for validation models.
    """

    arbitrary_types_allowed = True
    extra = "forbid"

check_valid_country_code(country_code_list)

Validate all listed countries (if present) are valid country codes.

Source code in /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/blackline/models/validation.py
def check_valid_country_code(country_code_list: list) -> list:
    """
    Validate all listed countries (if present) are valid country codes.
    """
    if country_code_list is not None:
        for country_code in country_code_list:
            if country_code not in VALID_COUNTRY_CODES:
                raise ValueError(
                    f"The country identified as {country_code} is not a valid Alpha-3 code per ISO 3166."  # noqa: E501
                )

    return country_code_list

no_self_reference(value, values)

Check to make sure that the _key doesn't match other _key references within an object.

i.e. DataCategory.parent_key != DataCategory.key

Source code in /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/blackline/models/validation.py
def no_self_reference(value: Key, values: dict) -> Key:
    """
    Check to make sure that the _key doesn't match other _key
    references within an object.

    i.e. DataCategory.parent_key != DataCategory.key
    """

    key = Key.validate(values.get("key", ""))
    if value == key:
        raise ValueError("Key can not self-reference!")
    return value

sort_list_objects_by_name(values)

Sort objects in a list by their name. This makes resource comparisons deterministic.

Source code in /opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/blackline/models/validation.py
def sort_list_objects_by_name(values: list) -> list:
    """
    Sort objects in a list by their name.
    This makes resource comparisons deterministic.
    """
    values.sort(key=lambda value: value.name)
    return values