Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Udayraj123
GitHub Repository: Udayraj123/OMRChecker
Path: blob/master/src/utils/validations.py
228 views
1
"""
2
3
OMRChecker
4
5
Author: Udayraj Deshmukh
6
Github: https://github.com/Udayraj123
7
8
"""
9
import re
10
11
import jsonschema
12
from jsonschema import validate
13
from rich.table import Table
14
15
from src.logger import console, logger
16
from src.schemas import SCHEMA_JSONS, SCHEMA_VALIDATORS
17
18
19
def validate_evaluation_json(json_data, evaluation_path):
20
logger.info(f"Loading evaluation.json: {evaluation_path}")
21
try:
22
validate(instance=json_data, schema=SCHEMA_JSONS["evaluation"])
23
except jsonschema.exceptions.ValidationError as _err: # NOQA
24
table = Table(show_lines=True)
25
table.add_column("Key", style="cyan", no_wrap=True)
26
table.add_column("Error", style="magenta")
27
28
errors = sorted(
29
SCHEMA_VALIDATORS["evaluation"].iter_errors(json_data),
30
key=lambda e: e.path,
31
)
32
for error in errors:
33
key, validator, msg = parse_validation_error(error)
34
if validator == "required":
35
requiredProperty = re.findall(r"'(.*?)'", msg)[0]
36
table.add_row(
37
f"{key}.{requiredProperty}",
38
msg + ". Make sure the spelling of the key is correct",
39
)
40
else:
41
table.add_row(key, msg)
42
console.print(table, justify="center")
43
raise Exception(
44
f"Provided Evaluation JSON is Invalid: '{evaluation_path}'"
45
) from None
46
47
48
def validate_template_json(json_data, template_path):
49
logger.info(f"Loading template.json: {template_path}")
50
try:
51
validate(instance=json_data, schema=SCHEMA_JSONS["template"])
52
except jsonschema.exceptions.ValidationError as _err: # NOQA
53
table = Table(show_lines=True)
54
table.add_column("Key", style="cyan", no_wrap=True)
55
table.add_column("Error", style="magenta")
56
57
errors = sorted(
58
SCHEMA_VALIDATORS["template"].iter_errors(json_data),
59
key=lambda e: e.path,
60
)
61
for error in errors:
62
key, validator, msg = parse_validation_error(error)
63
64
# Print preProcessor name in case of options error
65
if key == "preProcessors":
66
preProcessorName = json_data["preProcessors"][error.path[1]]["name"]
67
preProcessorKey = error.path[2]
68
table.add_row(f"{key}.{preProcessorName}.{preProcessorKey}", msg)
69
elif validator == "required":
70
requiredProperty = re.findall(r"'(.*?)'", msg)[0]
71
table.add_row(
72
f"{key}.{requiredProperty}",
73
f"{msg}. Check for spelling errors and make sure it is in camelCase",
74
)
75
else:
76
table.add_row(key, msg)
77
console.print(table, justify="center")
78
raise Exception(
79
f"Provided Template JSON is Invalid: '{template_path}'"
80
) from None
81
82
83
def validate_config_json(json_data, config_path):
84
logger.info(f"Loading config.json: {config_path}")
85
try:
86
validate(instance=json_data, schema=SCHEMA_JSONS["config"])
87
except jsonschema.exceptions.ValidationError as _err: # NOQA
88
table = Table(show_lines=True)
89
table.add_column("Key", style="cyan", no_wrap=True)
90
table.add_column("Error", style="magenta")
91
errors = sorted(
92
SCHEMA_VALIDATORS["config"].iter_errors(json_data),
93
key=lambda e: e.path,
94
)
95
for error in errors:
96
key, validator, msg = parse_validation_error(error)
97
98
if validator == "required":
99
requiredProperty = re.findall(r"'(.*?)'", msg)[0]
100
table.add_row(
101
f"{key}.{requiredProperty}",
102
f"{msg}. Check for spelling errors and make sure it is in camelCase",
103
)
104
else:
105
table.add_row(key, msg)
106
console.print(table, justify="center")
107
raise Exception(f"Provided config JSON is Invalid: '{config_path}'") from None
108
109
110
def parse_validation_error(error):
111
return (
112
(error.path[0] if len(error.path) > 0 else "$root"),
113
error.validator,
114
error.message,
115
)
116
117