aboutsummaryrefslogtreecommitdiff
path: root/lib/python/qmk/json_schema.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/python/qmk/json_schema.py')
-rw-r--r--lib/python/qmk/json_schema.py34
1 files changed, 20 insertions, 14 deletions
diff --git a/lib/python/qmk/json_schema.py b/lib/python/qmk/json_schema.py
index f3992ee71..cbc5bff51 100644
--- a/lib/python/qmk/json_schema.py
+++ b/lib/python/qmk/json_schema.py
@@ -27,9 +27,10 @@ def json_load(json_file):
27 27
28def load_jsonschema(schema_name): 28def load_jsonschema(schema_name):
29 """Read a jsonschema file from disk. 29 """Read a jsonschema file from disk.
30
31 FIXME(skullydazed/anyone): Refactor to make this a public function.
32 """ 30 """
31 if Path(schema_name).exists():
32 return json_load(schema_name)
33
33 schema_path = Path(f'data/schemas/{schema_name}.jsonschema') 34 schema_path = Path(f'data/schemas/{schema_name}.jsonschema')
34 35
35 if not schema_path.exists(): 36 if not schema_path.exists():
@@ -38,28 +39,33 @@ def load_jsonschema(schema_name):
38 return json_load(schema_path) 39 return json_load(schema_path)
39 40
40 41
41def keyboard_validate(data): 42def create_validator(schema):
42 """Validates data against the keyboard jsonschema. 43 """Creates a validator for the given schema id.
43 """ 44 """
44 schema = load_jsonschema('keyboard') 45 schema_store = {}
45 validator = jsonschema.Draft7Validator(schema).validate
46 46
47 return validator(data) 47 for schema_file in Path('data/schemas').glob('*.jsonschema'):
48 schema_data = load_jsonschema(schema_file)
49 if not isinstance(schema_data, dict):
50 cli.log.debug('Skipping schema file %s', schema_file)
51 continue
52 schema_store[schema_data['$id']] = schema_data
53
54 resolver = jsonschema.RefResolver.from_schema(schema_store['qmk.keyboard.v1'], store=schema_store)
55
56 return jsonschema.Draft7Validator(schema_store[schema], resolver=resolver).validate
48 57
49 58
50def keyboard_api_validate(data): 59def validate(data, schema):
51 """Validates data against the api_keyboard jsonschema. 60 """Validates data against a schema.
52 """ 61 """
53 base = load_jsonschema('keyboard') 62 validator = create_validator(schema)
54 relative = load_jsonschema('api_keyboard')
55 resolver = jsonschema.RefResolver.from_schema(base)
56 validator = jsonschema.Draft7Validator(relative, resolver=resolver).validate
57 63
58 return validator(data) 64 return validator(data)
59 65
60 66
61def deep_update(origdict, newdict): 67def deep_update(origdict, newdict):
62 """Update a dictionary in place, recursing to do a deep copy. 68 """Update a dictionary in place, recursing to do a depth-first deep copy.
63 """ 69 """
64 for key, value in newdict.items(): 70 for key, value in newdict.items():
65 if isinstance(value, Mapping): 71 if isinstance(value, Mapping):