File size: 3,257 Bytes
60cca7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1d7129
60cca7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1d7129
60cca7c
 
 
 
 
 
 
 
a1d7129
60cca7c
 
 
 
 
 
 
a1d7129
60cca7c
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"""
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