optimize our jsonschema using refs

This commit is contained in:
Zach White 2021-06-20 11:52:13 -07:00
parent 8c361d6c41
commit 0ca53fa307
4 changed files with 269 additions and 264 deletions

View file

@ -1,34 +1,22 @@
{ {
"$id": "qmk.api.keyboard.v1",
"allOf": [ "allOf": [
{ "$ref": "qmk.keyboard.v1" }, {"$ref": "qmk.keyboard.v1"},
{ {
"$id": "qmk.api.keyboard.v1", "properties": {
"keymaps": { "keymaps": {
"type": "string" "type": "object",
}, "properties": {
"parse_errors": { "url": {"type": "string"}
"type": "array", }
"items": {
"type": "string" },
} "parse_errors": {"$ref": "qmk.definitions.v1#/string_array"},
}, "parse_warnings": {"$ref": "qmk.definitions.v1#/string_array"},
"parse_warnings": { "processor_type": {"type": "string"},
"type": "array", "protocol": {"type": "string"},
"items": { "keyboard_folder": {"type": "string"},
"type": "string" "platform": {"type": "string"}
}
},
"processor_type": {
"type": "string"
},
"protocol": {
"type": "string"
},
"keyboard_folder": {
"type": "string"
},
"platform": {
"type": "string"
} }
} }
] ]

View file

@ -0,0 +1,94 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "qmk.definitions.v1",
"title": "Common definitions used across QMK's jsonschemas.",
"type": "object",
"boolean_array": {
"type": "object",
"additionalProperties": {"type": "boolean"}
},
"filename": {
"type": "string",
"minLength": 1,
"pattern": "^[0-9a-z_]*$"
},
"hex_number_2d": {
"type": "string",
"pattern": "^0x[0-9A-F]{2}$"
},
"hex_number_4d": {
"type": "string",
"pattern": "^0x[0-9A-F]{4}$"
},
"text_identifier": {
"type": "string",
"minLength": 1,
"maxLength": 250
},
"layout_macro": {
"oneOf": [
{
"type": "string",
"enum": ["LAYOUT", "LAYOUT_planck_1x2uC"]
},
{
"type": "string",
"pattern": "^LAYOUT_[0-9a-z_]*$"
}
]
},
"key_unit": {
"type": "number",
"min": 0.25
},
"mcu_pin_array": {
"type": "array",
"items": {"$ref": "#/mcu_pin"}
},
"mcu_pin": {
"oneOf": [
{
"type": "string",
"pattern": "^[A-K]\\d{1,2}$"
},
{
"type": "string",
"pattern": "^LINE_PIN\\d{1,2}$"
},
{
"type": "number",
"multipleOf": 1
},
{
"type": "null"
}
]
},
"string_array": {
"type": "array",
"items": {
"type": "string"
}
},
"string_object": {
"type": "object",
"additionalProperties": {
"type": "string"
}
},
"unsigned_decimal": {
"type": "number",
"min": 0
},
"unsigned_integer": {
"type": "number",
"min": 0,
"multipleOf": 1
}
"unsigned_integer_8": {
"type": "number",
"min": 0,
"max": 255,
"multipleOf": 1
}
}

View file

@ -1,24 +1,102 @@
{ {
"$schema": "http://json-schema.org/schema#", "$schema": "http://json-schema.org/draft-07/schema#",
"$id": "qmk.keyboard.v1", "$id": "qmk.keyboard.v1",
"title": "Keyboard Information", "title": "Keyboard Information",
"type": "object", "type": "object",
"definitions": {
"boolean_array": {
"type": "object",
"additionalProperties": {"type": "boolean"}
},
"filename": {
"type": "string",
"minLength": 1,
"pattern": "^[0-9a-z_]*$"
},
"hex_number_2d": {
"type": "string",
"pattern": "^0x[0-9A-F]{2}$"
},
"hex_number_4d": {
"type": "string",
"pattern": "^0x[0-9A-F]{4}$"
},
"text_identifier": {
"type": "string",
"minLength": 1,
"maxLength": 250
},
"layout_macro": {
"oneOf": [
{
"type": "string",
"enum": ["LAYOUT", "LAYOUT_planck_1x2uC"]
},
{
"type": "string",
"pattern": "^LAYOUT_[0-9a-z_]*$"
}
]
},
"key_unit": {
"type": "number",
"min": 0.25
},
"mcu_pin_array": {
"type": "array",
"items": {"$ref": "qmk.definitions.v1#/mcu_pin"}
},
"mcu_pin": {
"oneOf": [
{
"type": "string",
"pattern": "^[A-K]\\d{1,2}$"
},
{
"type": "string",
"pattern": "^LINE_PIN\\d{1,2}$"
},
{
"type": "number",
"multipleOf": 1
},
{
"type": "null"
}
]
},
"string_array": {
"type": "array",
"items": {
"type": "string"
}
},
"string_object": {
"type": "object",
"additionalProperties": {
"type": "string"
}
},
"unsigned_decimal": {
"type": "number",
"min": 0
},
"unsigned_integer": {
"type": "number",
"min": 0,
"multipleOf": 1
}
"unsigned_integer_8": {
"type": "number",
"min": 0,
"max": 255,
"multipleOf": 1
}
}
"properties": { "properties": {
"keyboard_name": { "keyboard_name": {"$ref": "qmk.definitions.v1#/text_identifier"},
"type": "string", "maintainer": {"$ref": "qmk.definitions.v1#/text_identifier"},
"minLength": 2, "manufacturer": {"$ref": "qmk.definitions.v1#/text_identifier"},
"maxLength": 250
},
"maintainer": {
"type": "string",
"minLength": 2,
"maxLength": 250
},
"manufacturer": {
"type": "string",
"minLength": 2,
"maxLength": 250
},
"url": { "url": {
"type": "string", "type": "string",
"format": "uri" "format": "uri"
@ -40,62 +118,25 @@
"type": "string", "type": "string",
"enum": ["COL2ROW", "ROW2COL"] "enum": ["COL2ROW", "ROW2COL"]
}, },
"debounce": { "debounce": {"$ref": "qmk.definitions.v1#/unsigned_integer"},
"type": "number", "height": {"$ref": "qmk.definitions.v1#/key_unit"},
"min": 0, "width": {"$ref": "qmk.definitions.v1#/key_unit"},
"multipleOf": 1
},
"height": {
"type": "number",
"min": 0.25
},
"width": {
"type": "number",
"min": 0.25
},
"community_layouts": { "community_layouts": {
"type": "array", "type": "array",
"items": { "items": {"$ref": "qmk.definitions.v1#/filename"}
"type": "string",
"minLength": 2,
"pattern": "^[0-9a-z_]*$"
}
},
"features": {
"type": "object",
"additionalProperties": {"type": "boolean"}
}, },
"features": {"$ref": "qmk.definitions.v1#/boolean_array"},
"indicators": { "indicators": {
"type": "object", "type": "object",
"properties": { "properties": {
"caps_lock": { "caps_lock": {"$ref": "qmk.definitions.v1#/mcu_pin"},
"type": "string", "num_lock": {"$ref": "qmk.definitions.v1#/mcu_pin"},
"pattern": "^[A-K]\\d{1,2}$" "scroll_lock": {"$ref": "qmk.definitions.v1#/mcu_pin"}
},
"num_lock": {
"type": "string",
"pattern": "^[A-K]\\d{1,2}$"
},
"scroll_lock": {
"type": "string",
"pattern": "^[A-K]\\d{1,2}$"
}
} }
}, },
"layout_aliases": { "layout_aliases": {
"type": "object", "type": "object",
"additionalProperties": { "additionalProperties": {"$ref": "qmk.definitions.v1#/layout_macro"}
"oneOf": [
{
"type": "string",
"enum": ["LAYOUT", "LAYOUT_planck_1x2uC"]
},
{
"type": "string",
"pattern": "^LAYOUT_[0-9a-z_]*$"
}
]
}
}, },
"layouts": { "layouts": {
"type": "object", "type": "object",
@ -109,11 +150,7 @@
"c_macro": { "c_macro": {
"type": "boolean" "type": "boolean"
}, },
"key_count": { "key_count": {"$ref": "qmk.definitions.v1#/key_unit"},
"type": "number",
"min": 0,
"multipleOf": 1
},
"layout": { "layout": {
"type": "array", "type": "array",
"items": { "items": {
@ -131,34 +168,14 @@
"multipleOf": 1 "multipleOf": 1
} }
}, },
"h": { "key_count": {"$ref": "qmk.definitions.v1#/key_unit"},
"type": "number", "r": {"$ref": "qmk.definitions.v1#/unsigned_decimal"},
"min": 0.25 "rx": {"$ref": "qmk.definitions.v1#/unsigned_decimal"},
}, "ry": {"$ref": "qmk.definitions.v1#/unsigned_decimal"},
"r": { "h": {"$ref": "qmk.definitions.v1#/key_unit"},
"type": "number", "w": {"$ref": "qmk.definitions.v1#/key_unit"},
"min": 0 "x": {"$ref": "qmk.definitions.v1#/key_unit"},
}, "y": {"$ref": "qmk.definitions.v1#/key_unit"}
"rx": {
"type": "number",
"min": 0
},
"ry": {
"type": "number",
"min": 0
},
"w": {
"type": "number",
"min": 0.25
},
"x": {
"type": "number",
"min": 0
},
"y": {
"type": "number",
"min": 0
}
} }
} }
} }
@ -171,73 +188,10 @@
"properties": { "properties": {
"direct": { "direct": {
"type": "array", "type": "array",
"items": { "items": {$ref": "qmk.definitions.v1#/mcu_pin_array"}
"type": "array",
"items": {
"oneOf": [
{
"type": "string",
"pattern": "^[A-K]\\d{1,2}$"
},
{
"type": "string",
"pattern": "^LINE_PIN\\d{1,2}$"
},
{
"type": "number",
"multipleOf": 1
},
{
"type": "null"
}
]
}
}
}, },
"cols": { "cols": {"$ref": "qmk.definitions.v1#/mcu_pin_array"},
"type": "array", "rows": {"$ref": "qmk.definitions.v1#/mcu_pin_array"}
"items": {
"oneOf": [
{
"type": "string",
"pattern": "^[A-K]\\d{1,2}$"
},
{
"type": "string",
"pattern": "^LINE_PIN\\d{1,2}$"
},
{
"type": "number",
"multipleOf": 1
},
{
"type": "null"
}
]
}
},
"rows": {
"type": "array",
"items": {
"oneOf": [
{
"type": "string",
"pattern": "^[A-K]\\d{1,2}$"
},
{
"type": "string",
"pattern": "^LINE_PIN\\d{1,2}$"
},
{
"type": "number",
"multipleOf": 1
},
{
"type": "null"
}
]
}
}
} }
}, },
"rgblight": { "rgblight": {
@ -250,47 +204,19 @@
"type": "boolean" "type": "boolean"
} }
}, },
"brightness_steps": { "brightness_steps": {"$ref": "qmk.definitions.v1#/unsigned_integer"},
"type": "number", "hue_steps": {"$ref": "qmk.definitions.v1#/unsigned_integer"},
"min": 0, "led_count": {"$ref": "qmk.definitions.v1#/unsigned_integer"},
"multipleOf": 1 "max_brightness": {"$ref": "qmk.definitions.v1#/unsigned_integer_8"},
}, "pin": {"$ref": "qmk.definitions.v1#/mcu_pin"},
"hue_steps": { "saturation_steps": {"$ref": "qmk.definitions.v1#/unsigned_integer"},
"type": "number",
"min": 0,
"multipleOf": 1
},
"led_count": {
"type": "number",
"min": 0,
"multipleOf": 1
},
"max_brightness": {
"type": "number",
"min": 0,
"max": 255,
"multipleOf": 1
},
"pin": {
"type": "string",
"pattern": "^([A-K]\\d{1,2}|LINE_PIN\\d{1,2})$"
},
"saturation_steps": {
"type": "number",
"min": 0,
"multipleOf": 1
},
"sleep": {"type": "boolean"}, "sleep": {"type": "boolean"},
"split": {"type": "boolean"}, "split": {"type": "boolean"},
"split_count": { "split_count": {
"type": "array", "type": "array",
"minLength": 2, "minLength": 2,
"maxLength": 2, "maxLength": 2,
"items": { "items": {"$ref": "qmk.definitions.v1#/unsigned_integer"}
"type": "number",
"min": 0,
"multipleOf": 1
}
} }
} }
}, },
@ -298,40 +224,19 @@
"type": "object", "type": "object",
"additionalProperties": false, "additionalProperties": false,
"properties": { "properties": {
"device_ver": { "device_ver": {"$ref": "qmk.definitions.v1#/hex_number_4d"},
"type": "string", "pid": {"$ref": "qmk.definitions.v1#/hex_number_4d"},
"pattern": "^[0-9A-F]x[0-9A-F][0-9A-F][0-9A-F][0-9A-F]" "vid": {"$ref": "qmk.definitions.v1#/hex_number_4d"}
},
"pid": {
"type": "string",
"pattern": "^[0-9A-F]x[0-9A-F][0-9A-F][0-9A-F][0-9A-F]"
},
"vid": {
"type": "string",
"pattern": "^[0-9A-F]x[0-9A-F][0-9A-F][0-9A-F][0-9A-F]"
}
} }
}, },
"qmk_lufa_bootloader": { "qmk_lufa_bootloader": {
"type": "object", "type": "object",
"additionalProperties": false, "additionalProperties": false,
"properties": { "properties": {
"esc_output": { "esc_output": {"$ref": "qmk.definitions.v1#/mcu_pin"},
"type": "string", "esc_input": {"$ref": "qmk.definitions.v1#/mcu_pin"},
"pattern": "^[A-K]\\d{1,2}$" "led": {"$ref": "qmk.definitions.v1#/mcu_pin"},
}, "speaker": {"$ref": "qmk.definitions.v1#/mcu_pin"}
"esc_input": {
"type": "string",
"pattern": "^[A-K]\\d{1,2}$"
},
"led": {
"type": "string",
"pattern": "^[A-K]\\d{1,2}$"
},
"speaker": {
"type": "string",
"pattern": "^[A-K]\\d{1,2}$"
}
} }
} }
} }

View file

@ -24,9 +24,10 @@ def json_load(json_file):
def load_jsonschema(schema_name): def load_jsonschema(schema_name):
"""Read a jsonschema file from disk. """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') schema_path = Path(f'data/schemas/{schema_name}.jsonschema')
if not schema_path.exists(): if not schema_path.exists():
@ -35,24 +36,41 @@ def load_jsonschema(schema_name):
return json_load(schema_path) return json_load(schema_path)
def create_validator(schema):
"""Creates a validator for the given schema id.
"""
schema_store = {}
for schema_file in Path(f'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 validate(data, schema):
"""Validates data against a schema.
"""
validator = create_validator(schema)
return validator(data)
def keyboard_validate(data): def keyboard_validate(data):
"""Validates data against the keyboard jsonschema. """Validates data against the keyboard jsonschema.
""" """
schema = load_jsonschema('keyboard') return validate(data, 'qmk.keyboard.v1')
validator = jsonschema.Draft7Validator(schema).validate
return validator(data)
def keyboard_api_validate(data): def keyboard_api_validate(data):
"""Validates data against the api_keyboard jsonschema. """Validates data against the api_keyboard jsonschema.
""" """
base = load_jsonschema('keyboard') return validate(data, 'qmk.api.keyboard.v1')
relative = load_jsonschema('api_keyboard')
resolver = jsonschema.RefResolver.from_schema(base)
validator = jsonschema.Draft7Validator(relative, resolver=resolver).validate
return validator(data)
def deep_update(origdict, newdict): def deep_update(origdict, newdict):