Skip to content

Validators

psengine.helpers.helpers.Validators

Common validators for pydantic models.

check_uhash_prefix staticmethod

check_uhash_prefix(
    value: Union[str, list],
) -> Union[str, list]

Validate that all fields start with 'uhash:' and add it if missing.

PARAMETER DESCRIPTION
value

String or list of strings to check for uhash prefix.

TYPE: Union[str, list]

RETURNS DESCRIPTION
Union[str, list]

String or list with 'uhash:' prefix ensured.

Source code in psengine/helpers/helpers.py
@staticmethod
def check_uhash_prefix(
    value: Annotated[
        Union[str, list], Doc('String or list of strings to check for uhash prefix.')
    ],
) -> Annotated[Union[str, list], Doc("String or list with 'uhash:' prefix ensured.")]:
    """Validate that all fields start with 'uhash:' and add it if missing."""
    uhash = 'uhash:'
    if isinstance(value, str):
        return f'{uhash}{value}' if not value.startswith(uhash) else value

    if isinstance(value, list):
        new_values = []
        for h in value:
            if h:
                complete_value = f'{uhash}{h}' if not h.startswith(uhash) else h
                new_values.append(complete_value)
        return new_values

    return value

convert_relative_time staticmethod

convert_relative_time(input_time: str) -> str

Convert relative time to datetime string if possible.

PARAMETER DESCRIPTION
input_time

Relative time string, e.g., '7d', '3h'.

TYPE: str

RETURNS DESCRIPTION
str

Datetime string in ISO 8601 format if conversion is possible.

Source code in psengine/helpers/helpers.py
@staticmethod
def convert_relative_time(
    input_time: Annotated[str, Doc("Relative time string, e.g., '7d', '3h'.")],
) -> Annotated[str, Doc('Datetime string in ISO 8601 format if conversion is possible.')]:
    """Convert relative time to datetime string if possible."""
    return (
        TimeHelpers.rel_time_to_date(input_time)
        if TimeHelpers.is_rel_time_valid(input_time)
        else input_time
    )

convert_str_to_list staticmethod

convert_str_to_list(value: Union[str, list]) -> list

Convert value from str to list and remove None values.

PARAMETER DESCRIPTION
value

String or list to convert.

TYPE: Union[str, list]

RETURNS DESCRIPTION
list

Converted list with None values removed.

Source code in psengine/helpers/helpers.py
@staticmethod
def convert_str_to_list(
    value: Annotated[Union[str, list], Doc('String or list to convert.')],
) -> Annotated[list, Doc('Converted list with None values removed.')]:
    """Convert value from str to list and remove None values."""
    value = value if isinstance(value, list) else [value]
    return [v for v in value if v is not None]