Skip to content

input4mips_validation.validation.allowed_characters#

input4mips_validation.validation.allowed_characters #

Support for defining rules around the characters allowed in metadata

AllowedCharacters #

Definition of a set of allowed characters

Source code in src/input4mips_validation/validation/allowed_characters.py
@define
class AllowedCharacters:
    """
    Definition of a set of allowed characters
    """

    values: set[str]
    """The allowed characters"""

    desc: str
    """A human-readable description of this set of allowed characters"""

desc: str instance-attribute #

A human-readable description of this set of allowed characters

values: set[str] instance-attribute #

The allowed characters

check_only_allowed_characters(value, allowed_characters) #

Check that a value only contains allowed characters

Parameters:

Name Type Description Default
value str

Value to check

required
allowed_characters AllowedCharacters

Allowed characters

required

Raises:

Type Description
ValueError

inv contains characters which are not allowed.

Source code in src/input4mips_validation/validation/allowed_characters.py
def check_only_allowed_characters(
    value: str, allowed_characters: AllowedCharacters
) -> None:
    """
    Check that a value only contains allowed characters

    Parameters
    ----------
    value
        Value to check

    allowed_characters
        Allowed characters

    Raises
    ------
    ValueError
        inv contains characters which are not allowed.
    """
    invalid_chars = {c for c in set(value) if c not in allowed_characters.values}
    if invalid_chars:
        msg = f"Only {allowed_characters.desc} are allowed. {value=}. {invalid_chars=}."
        raise ValueError(msg)