Spaces:
Paused
Paused
Create validation_engine.py
Browse files- validation_engine.py +57 -0
validation_engine.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
from log import log
|
3 |
+
|
4 |
+
|
5 |
+
class ValidationEngine:
|
6 |
+
def __init__(self):
|
7 |
+
pass
|
8 |
+
|
9 |
+
def validate_parameters(self, intent_def, variables):
|
10 |
+
"""
|
11 |
+
Checks all parameters defined in the intent against provided variables.
|
12 |
+
Returns: (is_valid, error_messages)
|
13 |
+
"""
|
14 |
+
is_valid = True
|
15 |
+
error_messages = []
|
16 |
+
|
17 |
+
for param_def in intent_def.get("parameters", []):
|
18 |
+
name = param_def.get("name")
|
19 |
+
expected_type = param_def.get("type", "string")
|
20 |
+
regex = param_def.get("regex")
|
21 |
+
validation_message = param_def.get("validation_message", f"Invalid value for {name}.")
|
22 |
+
|
23 |
+
value = variables.get(name)
|
24 |
+
|
25 |
+
# Check existence
|
26 |
+
if value is None:
|
27 |
+
continue # Missing parameters are handled elsewhere
|
28 |
+
|
29 |
+
# Type check
|
30 |
+
if expected_type == "string":
|
31 |
+
if not isinstance(value, str):
|
32 |
+
is_valid = False
|
33 |
+
error_messages.append(f"{validation_message} Expected a string.")
|
34 |
+
continue
|
35 |
+
elif expected_type == "int":
|
36 |
+
if not isinstance(value, int):
|
37 |
+
is_valid = False
|
38 |
+
error_messages.append(f"{validation_message} Expected an integer.")
|
39 |
+
continue
|
40 |
+
elif expected_type == "float":
|
41 |
+
if not isinstance(value, float):
|
42 |
+
is_valid = False
|
43 |
+
error_messages.append(f"{validation_message} Expected a float.")
|
44 |
+
continue
|
45 |
+
|
46 |
+
# Regex check
|
47 |
+
if regex and isinstance(value, str):
|
48 |
+
if not re.match(regex, value):
|
49 |
+
is_valid = False
|
50 |
+
error_messages.append(validation_message)
|
51 |
+
|
52 |
+
if is_valid:
|
53 |
+
log("✅ Parameter validation passed.")
|
54 |
+
else:
|
55 |
+
log(f"❌ Parameter validation failed: {error_messages}")
|
56 |
+
|
57 |
+
return is_valid, error_messages
|