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