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