Skip to content

Validators

provide.foundation.config.validators

TODO: Add module docstring.

Functions

validate_choice

validate_choice(
    choices: list[Any],
) -> Callable[[Any, Any, Any], None]

Create a validator that ensures value is one of the given choices.

Parameters:

Name Type Description Default
choices list[Any]

List of valid choices

required

Returns:

Type Description
Callable[[Any, Any, Any], None]

Validator function for use with attrs

Source code in provide/foundation/config/validators.py
def validate_choice(choices: list[Any]) -> Callable[[Any, Any, Any], None]:
    """Create a validator that ensures value is one of the given choices.

    Args:
        choices: List of valid choices

    Returns:
        Validator function for use with attrs

    """

    def validator(instance: Any, attribute: Any, value: Any) -> None:
        if value not in choices:
            # Import ValidationError locally to avoid circular imports
            from provide.foundation.errors.config import ValidationError

            raise ValidationError(
                f"Invalid value '{value}' for {attribute.name}. Must be one of: {choices!r}",
            )

    return validator

validate_log_level

validate_log_level(
    instance: Any, attribute: Any, value: str
) -> None

Validate that a log level is valid.

Source code in provide/foundation/config/validators.py
def validate_log_level(instance: Any, attribute: Any, value: str) -> None:
    """Validate that a log level is valid."""
    # Import ValidationError locally to avoid circular imports
    from provide.foundation.errors.config import ValidationError

    if value not in _VALID_LOG_LEVEL_TUPLE:
        raise ValidationError(
            _format_invalid_value_error(
                attribute.name,
                value,
                valid_options=list(_VALID_LOG_LEVEL_TUPLE),
            ),
        )

validate_non_negative

validate_non_negative(
    instance: Any, attribute: Any, value: float
) -> None

Validate that a value is non-negative.

Source code in provide/foundation/config/validators.py
def validate_non_negative(instance: Any, attribute: Any, value: float) -> None:
    """Validate that a value is non-negative."""
    # Import ValidationError locally to avoid circular imports
    from provide.foundation.errors.config import ValidationError

    # Check if value is numeric
    if not isinstance(value, (int, float)):
        raise ValidationError(
            f"Value must be a number, got {type(value).__name__}",
        )

    if value < 0:
        raise ValidationError(
            _format_validation_error(attribute.name, value, "must be non-negative"),
        )

validate_overflow_policy

validate_overflow_policy(
    instance: Any, attribute: Any, value: str
) -> None

Validate rate limit overflow policy.

Source code in provide/foundation/config/validators.py
def validate_overflow_policy(instance: Any, attribute: Any, value: str) -> None:
    """Validate rate limit overflow policy."""
    # Import ValidationError locally to avoid circular imports
    from provide.foundation.errors.config import ValidationError

    if value not in _VALID_OVERFLOW_POLICY_TUPLE:
        raise ValidationError(
            _format_invalid_value_error(
                attribute.name,
                value,
                valid_options=list(_VALID_OVERFLOW_POLICY_TUPLE),
            ),
        )

validate_port

validate_port(
    instance: Any, attribute: Any, value: int
) -> None

Validate that a port number is valid.

Source code in provide/foundation/config/validators.py
def validate_port(instance: Any, attribute: Any, value: int) -> None:
    """Validate that a port number is valid."""
    # Import ValidationError locally to avoid circular imports
    from provide.foundation.errors.config import ValidationError

    if not 1 <= value <= 65535:
        raise ValidationError(
            _format_validation_error(attribute.name, value, "must be between 1 and 65535"),
        )

validate_positive

validate_positive(
    instance: Any, attribute: Any, value: float
) -> None

Validate that a value is positive.

Source code in provide/foundation/config/validators.py
def validate_positive(instance: Any, attribute: Any, value: float) -> None:
    """Validate that a value is positive."""
    # Import ValidationError locally to avoid circular imports
    from provide.foundation.errors.config import ValidationError

    # Check if value is numeric
    if not isinstance(value, (int, float)):
        raise ValidationError(
            f"Value must be a number, got {type(value).__name__}",
        )

    if value <= 0:
        raise ValidationError(
            _format_validation_error(attribute.name, value, "must be positive"),
        )

validate_range

validate_range(
    min_val: float, max_val: float
) -> Callable[[Any, Any, Any], None]

Create a validator that ensures value is within the given numeric range.

Parameters:

Name Type Description Default
min_val float

Minimum allowed value (inclusive)

required
max_val float

Maximum allowed value (inclusive)

required

Returns:

Type Description
Callable[[Any, Any, Any], None]

Validator function for use with attrs

Source code in provide/foundation/config/validators.py
def validate_range(min_val: float, max_val: float) -> Callable[[Any, Any, Any], None]:
    """Create a validator that ensures value is within the given numeric range.

    Args:
        min_val: Minimum allowed value (inclusive)
        max_val: Maximum allowed value (inclusive)

    Returns:
        Validator function for use with attrs

    """

    def validator(instance: Any, attribute: Any, value: Any) -> None:
        # Import ValidationError locally to avoid circular imports
        from provide.foundation.errors.config import ValidationError

        # Check if value is numeric
        if not isinstance(value, (int, float)):
            raise ValidationError(
                f"Value must be a number, got {type(value).__name__}",
            )

        if not (min_val <= value <= max_val):
            raise ValidationError(
                f"Value must be between {min_val} and {max_val}, got {value}",
            )

    return validator

validate_sample_rate

validate_sample_rate(
    instance: Any, attribute: Any, value: float
) -> None

Validate that a sample rate is between 0.0 and 1.0.

Source code in provide/foundation/config/validators.py
def validate_sample_rate(instance: Any, attribute: Any, value: float) -> None:
    """Validate that a sample rate is between 0.0 and 1.0."""
    # Import ValidationError locally to avoid circular imports
    from provide.foundation.errors.config import ValidationError

    if not 0.0 <= value <= 1.0:
        raise ValidationError(
            _format_validation_error(attribute.name, value, "must be between 0.0 and 1.0"),
        )