Optimize our jsonschema by using refs (#13271)
* fix some broken info.json files * optimize our jsonschema using refs * fix formatting after vscode broke it * make flake8 happy * cleanup * make our schema validation more compact and flexible
This commit is contained in:
parent
9d4412cb8b
commit
b908275354
9 changed files with 351 additions and 310 deletions
|
@ -8,7 +8,7 @@ from jsonschema import ValidationError
|
|||
from milc import cli
|
||||
|
||||
from qmk.info import info_json
|
||||
from qmk.json_schema import json_load, keyboard_validate
|
||||
from qmk.json_schema import json_load, validate
|
||||
from qmk.json_encoders import InfoJSONEncoder, KeymapJSONEncoder
|
||||
from qmk.path import normpath
|
||||
|
||||
|
@ -23,14 +23,13 @@ def format_json(cli):
|
|||
|
||||
if cli.args.format == 'auto':
|
||||
try:
|
||||
keyboard_validate(json_file)
|
||||
validate(json_file, 'qmk.keyboard.v1')
|
||||
json_encoder = InfoJSONEncoder
|
||||
|
||||
except ValidationError as e:
|
||||
cli.log.warning('File %s did not validate as a keyboard:\n\t%s', cli.args.json_file, e)
|
||||
cli.log.info('Treating %s as a keymap file.', cli.args.json_file)
|
||||
json_encoder = KeymapJSONEncoder
|
||||
|
||||
elif cli.args.format == 'keyboard':
|
||||
json_encoder = InfoJSONEncoder
|
||||
elif cli.args.format == 'keymap':
|
||||
|
|
|
@ -9,7 +9,7 @@ from milc import cli
|
|||
|
||||
from qmk.constants import CHIBIOS_PROCESSORS, LUFA_PROCESSORS, VUSB_PROCESSORS
|
||||
from qmk.c_parse import find_layouts
|
||||
from qmk.json_schema import deep_update, json_load, keyboard_validate, keyboard_api_validate
|
||||
from qmk.json_schema import deep_update, json_load, validate
|
||||
from qmk.keyboard import config_h, rules_mk
|
||||
from qmk.keymap import list_keymaps
|
||||
from qmk.makefile import parse_rules_mk_file
|
||||
|
@ -66,7 +66,7 @@ def info_json(keyboard):
|
|||
|
||||
# Validate against the jsonschema
|
||||
try:
|
||||
keyboard_api_validate(info_data)
|
||||
validate(info_data, 'qmk.api.keyboard.v1')
|
||||
|
||||
except jsonschema.ValidationError as e:
|
||||
json_path = '.'.join([str(p) for p in e.absolute_path])
|
||||
|
@ -490,7 +490,7 @@ def merge_info_jsons(keyboard, info_data):
|
|||
continue
|
||||
|
||||
try:
|
||||
keyboard_validate(new_info_data)
|
||||
validate(new_info_data, 'qmk.keyboard.v1')
|
||||
except jsonschema.ValidationError as e:
|
||||
json_path = '.'.join([str(p) for p in e.absolute_path])
|
||||
cli.log.error('Not including data from file: %s', info_file)
|
||||
|
|
|
@ -24,9 +24,10 @@ def json_load(json_file):
|
|||
|
||||
def load_jsonschema(schema_name):
|
||||
"""Read a jsonschema file from disk.
|
||||
|
||||
FIXME(skullydazed/anyone): Refactor to make this a public function.
|
||||
"""
|
||||
if Path(schema_name).exists():
|
||||
return json_load(schema_name)
|
||||
|
||||
schema_path = Path(f'data/schemas/{schema_name}.jsonschema')
|
||||
|
||||
if not schema_path.exists():
|
||||
|
@ -35,28 +36,33 @@ def load_jsonschema(schema_name):
|
|||
return json_load(schema_path)
|
||||
|
||||
|
||||
def keyboard_validate(data):
|
||||
"""Validates data against the keyboard jsonschema.
|
||||
def create_validator(schema):
|
||||
"""Creates a validator for the given schema id.
|
||||
"""
|
||||
schema = load_jsonschema('keyboard')
|
||||
validator = jsonschema.Draft7Validator(schema).validate
|
||||
schema_store = {}
|
||||
|
||||
return validator(data)
|
||||
for schema_file in Path('data/schemas').glob('*.jsonschema'):
|
||||
schema_data = load_jsonschema(schema_file)
|
||||
if not isinstance(schema_data, dict):
|
||||
cli.log.debug('Skipping schema file %s', schema_file)
|
||||
continue
|
||||
schema_store[schema_data['$id']] = schema_data
|
||||
|
||||
resolver = jsonschema.RefResolver.from_schema(schema_store['qmk.keyboard.v1'], store=schema_store)
|
||||
|
||||
return jsonschema.Draft7Validator(schema_store[schema], resolver=resolver).validate
|
||||
|
||||
|
||||
def keyboard_api_validate(data):
|
||||
"""Validates data against the api_keyboard jsonschema.
|
||||
def validate(data, schema):
|
||||
"""Validates data against a schema.
|
||||
"""
|
||||
base = load_jsonschema('keyboard')
|
||||
relative = load_jsonschema('api_keyboard')
|
||||
resolver = jsonschema.RefResolver.from_schema(base)
|
||||
validator = jsonschema.Draft7Validator(relative, resolver=resolver).validate
|
||||
validator = create_validator(schema)
|
||||
|
||||
return validator(data)
|
||||
|
||||
|
||||
def deep_update(origdict, newdict):
|
||||
"""Update a dictionary in place, recursing to do a deep copy.
|
||||
"""Update a dictionary in place, recursing to do a depth-first deep copy.
|
||||
"""
|
||||
for key, value in newdict.items():
|
||||
if isinstance(value, Mapping):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue