pyfcstm.utils.validate

Validation framework utilities for model validation workflows.

This module provides a lightweight validation framework for Python models. It defines base exceptions for validation failures and a mixin-style interface (IValidatable) that allows classes to register validation rules and perform aggregated validation.

The module contains the following main components:

Example:

>>> from pyfcstm.utils.validate import IValidatable, ValidationError
>>>
>>> class MyModel(IValidatable):
...     def __init__(self, value: int):
...         self.value = value
...
...     def _validate_positive(self) -> None:
...         if self.value <= 0:
...             raise ValidationError("Value must be positive.")
...
...     __validators__ = [_validate_positive]
...
>>> model = MyModel(1)
>>> model.validate()
>>> bad_model = MyModel(0)
>>> try:
...     bad_model.validate()
... except Exception as err:
...     print(type(err).__name__)
ModelValidationError

ValidationError

class pyfcstm.utils.validate.ValidationError[source]

Base exception class for validation errors.

This exception should be raised when a single validation rule fails. It is designed to be caught and collected by IValidatable.

Example:

>>> raise ValidationError("Invalid value.")
Traceback (most recent call last):
    ...
ValidationError: Invalid value.

ModelValidationError

class pyfcstm.utils.validate.ModelValidationError(errors: List[ValidationError])[source]

Exception class for aggregating multiple validation errors.

This exception contains a list of ValidationError instances and formats them into a readable error message.

Parameters:

errors (List[ValidationError]) – List of validation errors that occurred

Variables:

errors (List[ValidationError]) – Stored validation errors

Example:

>>> err = ModelValidationError([ValidationError("A"), ValidationError("B")])
>>> "2 errors" in str(err)
True
__init__(errors: List[ValidationError]) None[source]

IValidatable

class pyfcstm.utils.validate.IValidatable[source]

Interface class for implementing validatable objects.

Classes inheriting from IValidatable should define their validation rules in the __validators__ class variable as a list of validator methods. Each validator should accept the instance as the sole parameter and raise ValidationError if the rule fails.

Variables:

__validators__ – List of validator functions to be applied

Example:

>>> class MyModel(IValidatable):
...     def _validate_non_empty(self) -> None:
...         if not getattr(self, "value", None):
...             raise ValidationError("Value is empty.")
...
...     __validators__ = [_validate_non_empty]
...
>>> model = MyModel()
>>> try:
...     model.validate()
... except ModelValidationError as err:
...     isinstance(err.errors[0], ValidationError)
True
validate() None[source]

Validate the object using all registered validators.

Raises:

ModelValidationError – If any validation errors occur

Example:

>>> class MyModel(IValidatable):
...     def _validate(self) -> None:
...         raise ValidationError("Always invalid.")
...
...     __validators__ = [_validate]
...
>>> try:
...     MyModel().validate()
... except ModelValidationError as err:
...     len(err.errors)
1