-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate.py
50 lines (41 loc) · 1.54 KB
/
validate.py
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
import json, jsonlines, logging
import jsonschema as jsonschema
from jsonschema import validate
from jsonschema import Draft7Validator
from pathlib import Path
# just loading this at the get go for now.
with open('https://git.yale.edu/raw/LUX/json-schema-validation/master/schema.json') as f:
schema = json.load(f)
v = Draft7Validator(schema)
def get_ids(jsonData):
for id in jsonData['identifiers']:
if id['identifier_type'] == 'ead':
fileId = id['identifier_value']
return fileId
def validateJson(jsonData):
try:
fileId = get_ids(jsonData)
except Exception as err:
print(err)
logging.info(err)
try:
validate(instance=jsonData, schema=schema)
except jsonschema.exceptions.ValidationError as err:
#only reports the first error in a file, but better than nothing (since i still need to read how this library works)
logging.info("%s %s", fileId, err.message)
return True
# to do... make logging work better. just the error and filename should suffice.
def get_started():
for file in Path('../ArchivesSpace-JSON-Lines').rglob('*.jsonl'):
print(file.name)
with jsonlines.open(file,'r') as reader:
for obj in reader:
validateJson(obj)
def main():
logging.info('Started')
get_started()
logging.info('Finished')
if __name__ == '__main__':
logging.basicConfig(filename='validation.log', level=logging.INFO, format='%(asctime)s %(message)s')
logging.getLogger("jsonschema").setLevel(logging.INFO)
main()