-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathvalidate.py
More file actions
30 lines (27 loc) · 834 Bytes
/
validate.py
File metadata and controls
30 lines (27 loc) · 834 Bytes
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
import fire
import jadn
import json
import os
SCHEMA_DIR = 'Schemas'
DATA_DIR = 'Data'
"""
Validate a file against a JADN schema
"""
def validate(file: str = 'checksums.json', schema: str = 'checksums.jidl') -> None:
filename, ext = os.path.splitext(file)
with open(os.path.join(SCHEMA_DIR, schema), encoding='utf-8') as fp:
sc = jadn.load_any(fp)
codec = jadn.codec.Codec(sc, verbose_rec=True, verbose_str=True)
item_type = sc['info']['exports'][0]
with open(os.path.join(DATA_DIR, file), encoding='utf-8') as fp:
data = json.load(fp)
print(f'{item_type}: {len(data)}')
try:
codec.decode(item_type, data)
except ValueError as e:
print(f' Error: {e}')
if __name__ == '__main__':
try:
fire.Fire(validate)
except FileNotFoundError as e:
print(e)