-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathvalidate.py
More file actions
92 lines (64 loc) · 2.38 KB
/
validate.py
File metadata and controls
92 lines (64 loc) · 2.38 KB
1
2
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import argparse
from pathlib import Path
import yaml
from yaml import CLoader as Loader
from jsonschema import Draft202012Validator
def main():
parser = argparse.ArgumentParser(
description="Validate database schema and records")
parser.add_argument('action', choices=['schema', 'records'])
args = parser.parse_args()
if args.action == 'schema':
validate_schema()
elif args.action == 'records':
validate_records()
else:
RuntimeError("I'm sorry, Dave. I'm afraid I can't do that.")
def validate_schema():
db_repo_path = Path(".")
schema_path = db_repo_path / "schema.yaml"
assert schema_path.is_file()
with open(schema_path, "r") as f:
schema = yaml.load(f, Loader)
Draft202012Validator.check_schema(schema)
def validate_records():
db_repo_path = Path(".")
schema_path = db_repo_path / "schema.yaml"
records_path = db_repo_path / "records"
assert schema_path.is_file()
assert records_path.is_dir()
with open(schema_path, "r") as f:
schema = yaml.load(f, Loader)
validator = Draft202012Validator(schema)
count = 0
error_count = 0
for p in records_path.iterdir():
count += 1
if p.suffix != ".yaml":
if error_count == 0: print("ERRORS:")
print(f"+ Record '{str(p.name)}' must have yaml file extension")
error_count += 1
try:
with open(p, "r") as f:
instance = yaml.load(f, Loader)
except Exception as e:
if error_count == 0: print("ERRORS:")
print(f"+ Record '{str(p.name)}' failed to load with the "
"following error:")
print(" " + str(e))
error_count += 1
continue
errors = list(validator.iter_errors(instance))
if not errors: continue
if error_count == 0: print("ERRORS:")
print(f"+ Record '{str(p.name)}' failed validation with the following "
"errors:")
for error in sorted(errors, key=str):
print(" " + error.message)
error_count += 1
if error_count:
print("")
raise RuntimeError(f"{error_count} validation error(s) detected")
print(f"Validated {count} records")
if __name__ == "__main__":
main()