Spaces:
Paused
Paused
| import re | |
| from log import log | |
| class ValidationEngine: | |
| def __init__(self): | |
| pass | |
| def validate_parameters(self, intent_def, variables): | |
| """ | |
| Checks all parameters defined in the intent against provided variables. | |
| Returns: (is_valid, error_messages) | |
| """ | |
| is_valid = True | |
| error_messages = [] | |
| for param_def in intent_def.get("parameters", []): | |
| name = param_def.get("name") | |
| expected_type = param_def.get("type", "string") | |
| regex = param_def.get("regex") | |
| validation_message = param_def.get("validation_message", f"Invalid value for {name}.") | |
| value = variables.get(name) | |
| # Check existence | |
| if value is None: | |
| continue # Missing parameters are handled elsewhere | |
| # Type check | |
| if expected_type == "string": | |
| if not isinstance(value, str): | |
| is_valid = False | |
| error_messages.append(f"{validation_message} Expected a string.") | |
| continue | |
| elif expected_type == "int": | |
| if not isinstance(value, int): | |
| is_valid = False | |
| error_messages.append(f"{validation_message} Expected an integer.") | |
| continue | |
| elif expected_type == "float": | |
| if not isinstance(value, float): | |
| is_valid = False | |
| error_messages.append(f"{validation_message} Expected a float.") | |
| continue | |
| # Regex check | |
| if regex and isinstance(value, str): | |
| if not re.match(regex, value): | |
| is_valid = False | |
| error_messages.append(validation_message) | |
| if is_valid: | |
| log("β Parameter validation passed.") | |
| else: | |
| log(f"β Parameter validation failed: {error_messages}") | |
| return is_valid, error_messages | |