Skip to content

Commit 366bcfb

Browse files
Tech dent: validation with better messages
1 parent bec7963 commit 366bcfb

File tree

3 files changed

+32
-27
lines changed

3 files changed

+32
-27
lines changed

index.html

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,15 @@ <h3>Visualiser Output</h3>
216216
const jsonInput = document.getElementById("jsonfile");
217217
const runBtn = document.getElementById("run");
218218

219-
function log(text) {
220-
let cleanText = text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
221-
// Handle ANSI Colors for Pydantic output
222-
cleanText = cleanText
223-
.replace(/\x1b\[92m/g, '<span class="ansi-green">')
224-
.replace(/\x1b\[93m/g, '<span class="ansi-yellow">')
225-
.replace(/\x1b\[91m/g, '<span class="ansi-red">')
226-
.replace(/\x1b\[0m/g, '</span>');
219+
function log(text) {
220+
let cleanText = text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
221+
// ANSI color replacements
222+
cleanText = cleanText
223+
.replace(/\x1b\[92m/g, '<span class="ansi-grey">') // validator/method green
224+
.replace(/\x1b\[93m/g, '<span class="ansi-yellow">') // general yellow
225+
.replace(/\x1b\[34m/g, '<span style="color:#005eb8;font-weight:bold">') // blue
226+
.replace(/\x1b\[33m/g, '<span class="ansi-yellow">') // colon yellow
227+
.replace(/\x1b\[0m/g, '</span>'); // reset
227228

228229
if (cleanText.includes("Valid Config")) {
229230
cleanText = cleanText.replace(/Valid Config/g, '<span style="font-size:2em;font-weight:bold;color:#007f3b">Valid Config</span>');

src/rules_validation_api/app.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from collections import defaultdict
66
from pathlib import Path
77

8+
from pydantic import ValidationError
9+
810
from rules_validation_api.decorators.tracker import VALIDATORS_CALLED
911
from rules_validation_api.validators.rules_validator import RulesValidation
1012

@@ -23,15 +25,21 @@
2325
LEFT_COLOR = "\033[34m" # Blue for class name
2426
COLON_COLOR = "\033[33m" # Yellow for colon
2527
RIGHT_COLOR = "\033[92m" # Milk green for validator
26-
CLASS_COLORS = [
27-
"\033[34m", # blue
28-
"\033[35m", # magenta
29-
"\033[36m", # cyan
30-
"\033[94m", # light blue
31-
"\033[95m", # light magenta
32-
"\033[96m", # light cyan
33-
"\033[37m", # white/light grey
34-
]
28+
29+
30+
def refine_error(e: ValidationError) -> str:
31+
"""Return a very short, single-line error message."""
32+
lines = [f"❌Validation Error: {len(e.errors())} validation error(s)"]
33+
34+
for err in e.errors():
35+
loc = ".".join(str(x) for x in err["loc"])
36+
msg = err["msg"]
37+
type_ = err["type"]
38+
39+
lines.append(f"{loc} : {msg} [type={type_}]")
40+
41+
return "\n".join(lines)
42+
3543

3644
def main() -> None:
3745
parser = argparse.ArgumentParser(description="Validate campaign configuration.")
@@ -50,25 +58,21 @@ def main() -> None:
5058
cls, method = v.split(":", 1)
5159
grouped[cls].append(method.strip())
5260

53-
# Assign colors to classes
54-
cls_color_map = {}
55-
for i, cls_name in enumerate(sorted(grouped.keys(), reverse=True)):
56-
cls_color_map[cls_name] = CLASS_COLORS[i % len(CLASS_COLORS)]
57-
5861
# Print grouped
5962
for cls_name in sorted(grouped.keys(), reverse=True):
6063
methods = sorted(grouped[cls_name])
6164
# First method prints class name
6265
first = methods[0]
63-
colored = f"{cls_color_map[cls_name]}{cls_name}{RESET}{COLON_COLOR}:{RESET}{RIGHT_COLOR}{first}{RESET}"
64-
print(colored)
66+
colored = f"{LEFT_COLOR}{cls_name}{RESET}{COLON_COLOR}:{RESET}{RIGHT_COLOR}{first}{RESET}"
67+
sys.stdout.write(colored)
6568
# Rest methods indented
6669
for method_name in methods[1:]:
6770
colored = f"{' ' * len(cls_name)}{COLON_COLOR}:{RESET}{RIGHT_COLOR}{method_name}{RESET}"
68-
print(colored)
71+
sys.stdout.write(colored)
6972

70-
except ValueError as e:
71-
sys.stderr.write(f"{YELLOW}Validation Error:{RESET} {RED}{e}{RESET}\n")
73+
except ValidationError as e:
74+
clean = refine_error(e)
75+
sys.stderr.write(f"{YELLOW}{clean}{RESET}\n")
7276

7377

7478
if __name__ == "__main__":

src/rules_validation_api/decorators/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)