Skip to content

validators

validate_time_period(period=None)

Validate a time period string.

Pass a time period (i.e. "days", "weeks", etc). If the period matches a valid time period, string is returned, otherwise a ValueError is raised.

Parameters:

Name Type Description Default
period str

A period of time, like 'days' or 'weeks'

None
Source code in src/red_utils/ext/time_utils/pendulum_utils/validators.py
def validate_time_period(period: str = None) -> str:
    """Validate a time period string.

    Pass a time period (i.e. "days", "weeks", etc). If the period
    matches a valid time period, string is returned, otherwise a
    ValueError is raised.

    Params:
        period (str): A period of time, like 'days' or 'weeks'
    """
    if period is None:
        raise ValueError("Missing a time period to evaluate")
    if not isinstance(period, str):
        raise TypeError(
            f"Invalid type for time period: ({type(period)}). Must be one of {VALID_TIME_PERIODS}"
        )
    if period not in VALID_TIME_PERIODS:
        raise ValueError(
            f"Invalid time period: '{period}'. Must be one of {VALID_TIME_PERIODS}"
        )

    return period