Skip to content

Validation

ValidationEngine

ValidationEngine

ValidationEngine(strict: bool = False)

Validates UDM events against schema and business rules.

Performs validation checks including: - Required fields present - Timestamps are valid - Enum values are valid - Numeric ranges are valid - SI units consistency - Cross-field consistency

Initialize the validation engine.

PARAMETER DESCRIPTION
strict

If True, warnings are treated as errors

TYPE: bool DEFAULT: False

Source code in phytrace/core/validation.py
def __init__(self, strict: bool = False):
    """
    Initialize the validation engine.

    Args:
        strict: If True, warnings are treated as errors
    """
    self.strict = strict

Functions

validate

validate(event: UDMEvent) -> ValidationResult

Validate an event against UDM schema.

PARAMETER DESCRIPTION
event

The UDMEvent to validate

TYPE: UDMEvent

RETURNS DESCRIPTION
ValidationResult

ValidationResult with errors and warnings

Source code in phytrace/core/validation.py
def validate(self, event: UDMEvent) -> ValidationResult:
    """
    Validate an event against UDM schema.

    Args:
        event: The UDMEvent to validate

    Returns:
        ValidationResult with errors and warnings
    """
    result = ValidationResult(valid=True)

    # Required field validation
    self._validate_required_fields(event, result)

    # Timestamp validation
    self._validate_timestamps(event, result)

    # Domain-specific validation
    self._validate_location(event, result)
    self._validate_motion(event, result)
    self._validate_power(event, result)
    self._validate_safety(event, result)

    # Cross-field consistency
    self._validate_consistency(event, result)

    # Convert warnings to errors if strict mode
    if self.strict and result.warnings:
        for warning in result.warnings:
            result.add_error(
                warning.field,
                warning.message,
                f"STRICT_{warning.warning_code}",
                warning.value,
            )

    return result

ValidationResult

ValidationResult dataclass

ValidationResult(valid: bool, errors: list[ValidationError] = list(), warnings: list[ValidationWarning] = list())

Result of event validation.

Functions

add_error

add_error(field: str, message: str, error_code: str, value: Any = None) -> None

Add an error to the result.

Source code in phytrace/core/validation.py
def add_error(
    self, field: str, message: str, error_code: str, value: Any = None
) -> None:
    """Add an error to the result."""
    self.errors.append(ValidationError(field, message, error_code, value))
    self.valid = False

add_warning

add_warning(field: str, message: str, warning_code: str, value: Any = None) -> None

Add a warning to the result.

Source code in phytrace/core/validation.py
def add_warning(
    self, field: str, message: str, warning_code: str, value: Any = None
) -> None:
    """Add a warning to the result."""
    self.warnings.append(ValidationWarning(field, message, warning_code, value))

ValidationError

ValidationError dataclass

ValidationError(field: str, message: str, error_code: str, value: Any = None)

Represents a blocking validation error.