Spaces:
Building
Building
Update validation_engine.py
Browse files- validation_engine.py +22 -53
validation_engine.py
CHANGED
@@ -1,56 +1,25 @@
|
|
1 |
-
|
2 |
-
|
|
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
pass
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
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
|
|
|
1 |
+
"""
|
2 |
+
Flare – Parameter Validation
|
3 |
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
4 |
+
Basit tip + regex kontrolü; Spark veya harici lib ile zenginleştirilebilir.
|
5 |
+
"""
|
6 |
|
7 |
+
import re
|
8 |
+
from typing import Any
|
9 |
+
from config_provider import ParameterConfig
|
10 |
+
|
11 |
+
def validate(value: str, param: ParameterConfig) -> bool:
|
12 |
+
t = param.canonical_type()
|
13 |
+
if t == "int":
|
14 |
+
if not value.isdigit(): return False
|
15 |
+
elif t == "float":
|
16 |
+
try: float(value)
|
17 |
+
except ValueError: return False
|
18 |
+
elif t in ("str", "string"):
|
19 |
pass
|
20 |
+
elif t == "bool":
|
21 |
+
if value.lower() not in ("true","false","1","0","evet","hayır"): return False
|
22 |
|
23 |
+
if param.validation_regex and not re.fullmatch(param.validation_regex, value, re.I):
|
24 |
+
return False
|
25 |
+
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|