-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path__init__.py
More file actions
60 lines (46 loc) · 1.69 KB
/
__init__.py
File metadata and controls
60 lines (46 loc) · 1.69 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
import os
import sys
import time
from jsonschema import Draft202012Validator, ValidationError, validate
from referencing import Registry, Resource
from jsondoc.utils import load_json_file
# from referencing.jsonschema import DRAFT202012
def resolve_schema(uri: str):
if uri.startswith("file://"):
path = uri[7:]
else:
path = uri
contents = load_json_file(path)
return Resource.from_contents(contents)
def validate_json(schema_path, data_path, root=None):
schema = load_json_file(schema_path)
data = load_json_file(data_path)
# Create a registry that can handle file:// URIs and relative paths
schema_dir = os.path.dirname(os.path.abspath(schema_path))
if root is not None:
root = os.path.abspath(root)
base_uri = f"file://{schema_dir}/"
def retrieve(uri: str):
if uri.startswith(base_uri):
relative_path = uri[len(base_uri) :]
full_path = os.path.join(schema_dir, relative_path)
return resolve_schema(full_path)
else:
if root is not None:
full_path = os.path.join(root, "." + uri)
else:
full_path = os.path.join(schema_dir, uri)
return resolve_schema(full_path)
registry = Registry(retrieve=retrieve)
# Create the validator with the registry
validator = Draft202012Validator(schema, registry=registry)
try:
start = time.time()
validator.validate(data)
end = time.time()
elapsed_ms = (end - start) * 1000
print(f"{data_path} is valid (took {elapsed_ms:.3f}ms)")
sys.exit(0)
except ValidationError as e:
print(f"Validation error: {e}")
sys.exit(1)