-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathSchemaValidator.py
35 lines (24 loc) · 1 KB
/
SchemaValidator.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
from json import loads
from os import listdir
from os.path import abspath, dirname, join, splitext
from typing import Any, Dict
from jsonschema import RefResolver, validate
class SchemaValidator:
_schemas: Dict[str, Dict[str, Any]]
_resolver: RefResolver
def __init__(self):
if not hasattr(self, "_schemas"):
schemas = {}
lib_root = abspath(dirname(__file__))
schema_dir = join(lib_root, "schemas")
for filename in listdir(schema_dir):
with open(join(schema_dir, filename), encoding="utf-8") as f:
schema_key = splitext(filename)[0]
schemas[schema_key] = loads(f.read())
SchemaValidator._schemas = schemas
SchemaValidator._resolver = RefResolver("", "", store=schemas)
@classmethod
def validate(cls, name: str, obj: Any):
validate(obj, cls._schemas[name], resolver=cls._resolver)
# initialize the schemas. A bit dirty, but the simplest
SchemaValidator()