From fbfd5312b995a32af690c183cad0dc988f695e89 Mon Sep 17 00:00:00 2001 From: Erovia Date: Mon, 28 Feb 2022 20:02:39 +0000 Subject: CLI: Validate JSON keymap input (#16261) * Fix schema validator It should use the passed schema. * Add required attributes to keymap schema * Rework subcommands to validate the JSON keymaps The 'compile', 'flash' and 'json2c' subcommands were reworked to add JSON keymap validation so error is reported for non-JSON and non-compliant-JSON inputs. * Fix required fields in keymap schema * Add tests * Fix compiling keymaps directly from keymap directory * Schema should not require version for now. --- lib/python/qmk/json_schema.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'lib/python/qmk/json_schema.py') diff --git a/lib/python/qmk/json_schema.py b/lib/python/qmk/json_schema.py index ffc7c6bcd1..2b48782fbb 100644 --- a/lib/python/qmk/json_schema.py +++ b/lib/python/qmk/json_schema.py @@ -16,7 +16,11 @@ def json_load(json_file): Note: file must be a Path object. """ try: - return hjson.load(json_file.open(encoding='utf-8')) + # Get the IO Stream for Path objects + # Not necessary if the data is provided via stdin + if isinstance(json_file, Path): + json_file = json_file.open(encoding='utf-8') + return hjson.load(json_file) except (json.decoder.JSONDecodeError, hjson.HjsonDecodeError) as e: cli.log.error('Invalid JSON encountered attempting to load {fg_cyan}%s{fg_reset}:\n\t{fg_red}%s', json_file, e) @@ -62,7 +66,7 @@ def create_validator(schema): """Creates a validator for the given schema id. """ schema_store = compile_schema_store() - resolver = jsonschema.RefResolver.from_schema(schema_store['qmk.keyboard.v1'], store=schema_store) + resolver = jsonschema.RefResolver.from_schema(schema_store[schema], store=schema_store) return jsonschema.Draft7Validator(schema_store[schema], resolver=resolver).validate -- cgit v1.2.1