-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.py
93 lines (75 loc) · 2.93 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
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
93
import json, os
from jsonschema import RefResolver, Draft4Validator
from os import listdir
from os.path import isfile, join
import logging
import re
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
DATS_schemasPath = os.path.join(os.path.dirname(__file__), "")
DATS_contextsPath = os.path.join(os.path.dirname(__file__), "")
def validate_dataset(path, filename, error_printing):
try:
dataset_schema_file = open(join(DATS_schemasPath,"dataset_schema.json"))
datasetSchema = json.load(dataset_schema_file)
resolver = RefResolver('file://'+DATS_schemasPath+'/'+"dataset_schema.json", datasetSchema) #, base_uri=schemasPath)
validator = Draft4Validator(datasetSchema, resolver=resolver)
logger.info("Validating %s", filename)
try:
dataset_file = open(join(path,filename))
instance = json.load(dataset_file)
if (error_printing == 0):
errors = sorted(validator.iter_errors(instance), key=lambda e: e.path)
for error in errors:
print(error.message)
if (len(errors)==0):
return True
else:
return False
elif (error_printing == 1):
errors = sorted(validator.iter_errors(instance), key=lambda e: e.path)
for error in errors:
for suberror in sorted(error.context, key=lambda e: e.schema_path):
print(list(suberror.schema_path), suberror.message, sep=", ")
if (len(errors)==0):
logger.info("...done")
return True
else:
return False
else:
try:
validator.validate(instance, datasetSchema)
logger.info("...done")
return True
except Exception as e:
logger.error(e)
return False
finally:
dataset_file.close()
finally:
dataset_schema_file.close()
def validate_schema(path, schemaFile):
try:
logger.info("Validating schema %s", schemaFile)
schema_file = open(join(path, schemaFile))
schema = json.load(schema_file)
try:
Draft4Validator.check_schema(schema)
return True
except Exception as e:
logger.error(e)
return False
logger.info("done.")
finally:
schema_file.close()
def validate_schemas(path):
result = True
files = [f for f in listdir(path) if (isfile(join(path, f)) and re.match(r'.*.json', f))]
for schemaFile in files:
result = result and validate_schema(path, schemaFile)
return result
def validate_dats_schemas():
return validate_schemas(DATS_schemasPath)
def validate_dats_contexts():
return validate_schemas(DATS_contextsPath)
validate_dats_schemas()