-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathvalidators.py
30 lines (22 loc) · 1.04 KB
/
validators.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
import json
import urllib.parse
from pathlib import Path
from typing import Any, Type
import jsonschema
from jsonschema.protocols import Validator
from jsonschema.validators import RefResolver
def schemas_dir() -> Path:
return Path('content/schemas').resolve()
assert Path(__file__, '../../content/schemas').resolve().samefile(schemas_dir())
def for_schema(rel_path: str) -> Validator:
def schema_handler(uri: str) -> Any:
with schemas_dir().joinpath(urllib.parse.urlparse(uri).path.removeprefix('/schemas/')).open() as rf:
return json.load(rf)
schema_path = schemas_dir().joinpath(rel_path)
with schema_path.open() as f:
schema = json.load(f)
resolver = RefResolver(base_uri=schema_path.as_uri(), referrer=schema, handlers={'https': schema_handler})
validator_cls: Type[jsonschema.Validator] = jsonschema.validators.validator_for(schema)
validator = validator_cls(resolver=resolver, schema=schema)
jsonschema.validate(schema, validator_cls.META_SCHEMA)
return validator