"""
DataValidationFramework
Generated by Eden via recursive self-improvement
2025-10-28 07:58:05.311560
"""

class DataValidator:
    """A class to automate data validation with customizable rules."""
    
    def __init__(self, data):
        """
        Initializes the DataValidator with input data.
        
        Args:
            data (dict): The data to be validated.
        """
        self.data = data
        self.results = {
            'successes': [],
            'failures': [],
            'exceptions': []
        }
    
    def validate_required_fields(self, required_fields):
        """
        Validates that all required fields are present in the data.
        
        Args:
            required_fields (list): List of field names that must be present.
        """
        for field in required_fields:
            if field not in self.data:
                self.results['failures'].append(f"Required field '{field}' is missing.")
    
    def validate_field_type(self, field, expected_type):
        """
        Validates that a specific field has the correct type.
        
        Args:
            field (str): The name of the field to check.
            expected_type (type): The expected type of the field value.
        """
        if not isinstance(self.data.get(field), expected_type):
            self.results['failures'].append(
                f"Field '{field}' is not of type {expected_type.__name__}."
            )
    
    def validate_range(self, field, min_value=None, max_value=None):
        """
        Validates that a numeric field falls within specified range.
        
        Args:
            field (str): The name of the numeric field to check.
            min_value (int/float): Minimum allowed value. Defaults to None.
            max_value (int/float): Maximum allowed value. Defaults to None.
        """
        value = self.data.get(field)
        if not isinstance(value, (int, float)):
            self.results['failures'].append(f"Field '{field}' is not numeric.")
            return
        
        if min_value is not None and value < min_value:
            self.results['failures'].append(
                f"Field '{field}' must be greater than or equal to {min_value}."
            )
        
        if max_value is not None and value > max_value:
            self.results['failures'].append(
                f"Field '{field}' must be less than or equal to {max_value}."
            )
    
    def validate_regex(self, field, pattern):
        """
        Validates that a string field matches a specified regex pattern.
        
        Args:
            field (str): The name of the string field to check.
            pattern (str): The regex pattern to match against.
        """
        import re
        value = str(self.data.get(field))
        if not re.match(pattern, value):
            self.results['failures'].append(
                f"Field '{field}' does not match pattern '{pattern}'."
            )
    
    def validate_required_if(self, field, condition_field, condition_value):
        """
        Validates that a field is required only if another field has a specific value.
        
        Args:
            field (str): The name of the field to check requirement for.
            condition_field (str): The name of the field containing the condition.
            condition_value: The value that condition_field must have for field to be required.
        """
        if self.data.get(condition_field) == condition_value and field not in self.data:
            self.results['failures'].append(
                f"Field '{field}' is required when '{condition_field}' equals '{condition_value}'."
            )
    
    def run_validations(self):
        """Executes all defined validations and returns the results."""
        return self.results

# Example usage
if __name__ == "__main__":
    sample_data = {
        "name": "John Doe",
        "age": 30,
        "email": "john@example.com",
        "is_active": True,
        "created_at": None
    }
    
    # Create validator instance
    validator = DataValidator(sample_data)
    
    # Define and run validations
    validator.validate_required_fields(["name", "age", "email"])
    validator.validate_field_type("age", int)
    validator.validate_range("age", 0, 120)
    validator.validate_regex("email", r"^\w+@\w+\.\w+$")
    
    # Check if 'created_at' is required only if 'is_active' is True
    validator.validate_required_if("created_at", "is_active", True)
    
    # Get results
    results = validator.run_validations()
    
    print("\nValidation Results:")
    for result_type, messages in results.items():
        print(f"\n{result_type.title()}:")
        if messages:
            for msg in messages:
                print(msg)
        else:
            print("No issues found.")