Spaces:
Sleeping
Sleeping
| """ | |
| Validation utilities for HVAC Load Calculator | |
| This module provides validation functions for input data in the HVAC Load Calculator. | |
| """ | |
| class ValidationWarning: | |
| """ | |
| A class to represent validation warnings. | |
| """ | |
| def __init__(self, message, suggestion, is_critical=False): | |
| """ | |
| Initialize a validation warning. | |
| Args: | |
| message (str): Warning message | |
| suggestion (str): Suggestion for fixing the warning | |
| is_critical (bool): Whether the warning is critical (prevents proceeding) | |
| """ | |
| self.message = message | |
| self.suggestion = suggestion | |
| self.is_critical = is_critical | |
| def validate_input(input_value, validation_type, min_value=None, max_value=None, required=False): | |
| """ | |
| Validate an input value. | |
| Args: | |
| input_value: Value to validate | |
| validation_type (str): Type of validation ('number', 'text', etc.) | |
| min_value: Minimum allowed value (for numeric inputs) | |
| max_value: Maximum allowed value (for numeric inputs) | |
| required (bool): Whether the input is required | |
| Returns: | |
| tuple: (is_valid, warnings) | |
| """ | |
| warnings = [] | |
| is_valid = True | |
| # Check if required | |
| if required and (input_value is None or input_value == "" or (isinstance(input_value, (int, float)) and input_value == 0)): | |
| warnings.append(ValidationWarning( | |
| "Required field is empty", | |
| "Please provide a value for this field", | |
| is_critical=True # Keep required fields as critical | |
| )) | |
| is_valid = False | |
| # Skip further validation if value is empty and not required | |
| if input_value is None or input_value == "": | |
| return is_valid, warnings | |
| # Validate based on type | |
| if validation_type == 'number': | |
| try: | |
| # Convert to float if it's a string | |
| if isinstance(input_value, str): | |
| input_value = float(input_value) | |
| # Check min value | |
| if min_value is not None and input_value < min_value: | |
| warnings.append(ValidationWarning( | |
| f"Value is below minimum ({min_value})", | |
| f"Please enter a value greater than or equal to {min_value}", | |
| is_critical=False # Changed to non-critical | |
| )) | |
| is_valid = False | |
| # Check max value | |
| if max_value is not None and input_value > max_value: | |
| warnings.append(ValidationWarning( | |
| f"Value exceeds maximum ({max_value})", | |
| f"Please enter a value less than or equal to {max_value}", | |
| is_critical=False # Changed to non-critical | |
| )) | |
| is_valid = False | |
| except ValueError: | |
| warnings.append(ValidationWarning( | |
| "Invalid number format", | |
| "Please enter a valid number", | |
| is_critical=True # Keep format validation as critical | |
| )) | |
| is_valid = False | |
| elif validation_type == 'text': | |
| # Add text validation if needed | |
| pass | |
| return is_valid, warnings | |