sec_interp.core.validation.validators module

Reusable validators for dataclass fields.

This module provides composable validators that can be used to validate and coerce field values in dataclasses without external dependencies.

class sec_interp.core.validation.validators.FieldValidator(*validators: Callable[[Any], Any])

Bases: object

Container for applying multiple validators to a field.

This class allows composition of multiple validators that are applied in sequence to a field value.

Example

>>> validator = FieldValidator(
...     coerce_type(float, "price"),
...     validate_positive("price"),
...     validate_range(0.0, 1000.0, "price"),
... )
>>> validated_value = validator("42.5")
__call__(value: Any) Any

Apply validators in sequence.

Parameters:

value – The value to validate.

Returns:

The validated (and possibly transformed) value.

Raises:

ValidationError – If any validator fails.

__init__(*validators: Callable[[Any], Any]) None

Initialize with a sequence of validators.

Parameters:

*validators – Variable number of validator functions to apply.

sec_interp.core.validation.validators.coerce_type(target_type: type, field_name: str = '') Callable[[Any], Any]

Create a type coercion validator.

Attempts to convert value to the target type. If conversion fails, raises a ValidationError with details.

Parameters:
  • target_type – The type to convert to (e.g., int, float, str).

  • field_name – Optional field name for error messages.

Returns:

Validator function that coerces value to target type.

Raises:

ValidationError – If type conversion fails.

sec_interp.core.validation.validators.validate_and_clamp(min_val: float, max_val: float) Callable[[float], float]

Create a validator that clamps values to a range.

Unlike validate_range, this validator does not raise an error. Instead, it clamps the value to the nearest boundary.

Parameters:
  • min_val – Minimum allowed value.

  • max_val – Maximum allowed value.

Returns:

Validator function that clamps value to [min_val, max_val].

sec_interp.core.validation.validators.validate_non_empty(field_name: str = '') Callable[[str], str]

Create a validator for non-empty strings.

Parameters:

field_name – Optional field name for error messages.

Returns:

Validator function that checks if string is non-empty.

Raises:

ValidationError – If string is empty or whitespace-only.

sec_interp.core.validation.validators.validate_non_negative(field_name: str = '') Callable[[float], float]

Create a validator for non-negative values.

Parameters:

field_name – Optional field name for error messages.

Returns:

Validator function that checks if value is >= 0.

Raises:

ValidationError – If value is negative.

sec_interp.core.validation.validators.validate_percentage(field_name: str = '') FieldValidator

Create a validator for percentage values (0-100).

Parameters:

field_name – Optional field name for error messages.

Returns:

Composite validator for percentage values.

sec_interp.core.validation.validators.validate_positive(field_name: str = '') Callable[[float], float]

Create a validator for positive values.

Parameters:

field_name – Optional field name for error messages.

Returns:

Validator function that checks if value is positive.

Raises:

ValidationError – If value is not positive.

sec_interp.core.validation.validators.validate_positive_int(field_name: str = '') FieldValidator

Create a validator for positive integers.

Parameters:

field_name – Optional field name for error messages.

Returns:

Composite validator for positive integers.

sec_interp.core.validation.validators.validate_probability(field_name: str = '') FieldValidator

Create a validator for probability values (0-1).

Parameters:

field_name – Optional field name for error messages.

Returns:

Composite validator for probability values.

sec_interp.core.validation.validators.validate_range(min_val: float, max_val: float, field_name: str = '') Callable[[float], float]

Create a range validator.

Parameters:
  • min_val – Minimum allowed value (inclusive).

  • max_val – Maximum allowed value (inclusive).

  • field_name – Optional field name for error messages.

Returns:

Validator function that checks if value is within range.

Raises:

ValidationError – If value is outside the specified range.