optimize our jsonschema using refs
This commit is contained in:
@ -1,34 +1,22 @@
|
||||
{
|
||||
"$id": "qmk.api.keyboard.v1",
|
||||
"allOf": [
|
||||
{ "$ref": "qmk.keyboard.v1" },
|
||||
{"$ref": "qmk.keyboard.v1"},
|
||||
{
|
||||
"$id": "qmk.api.keyboard.v1",
|
||||
"keymaps": {
|
||||
"type": "string"
|
||||
},
|
||||
"parse_errors": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"parse_warnings": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"processor_type": {
|
||||
"type": "string"
|
||||
},
|
||||
"protocol": {
|
||||
"type": "string"
|
||||
},
|
||||
"keyboard_folder": {
|
||||
"type": "string"
|
||||
},
|
||||
"platform": {
|
||||
"type": "string"
|
||||
"properties": {
|
||||
"keymaps": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"url": {"type": "string"}
|
||||
}
|
||||
|
||||
},
|
||||
"parse_errors": {"$ref": "qmk.definitions.v1#/string_array"},
|
||||
"parse_warnings": {"$ref": "qmk.definitions.v1#/string_array"},
|
||||
"processor_type": {"type": "string"},
|
||||
"protocol": {"type": "string"},
|
||||
"keyboard_folder": {"type": "string"},
|
||||
"platform": {"type": "string"}
|
||||
}
|
||||
}
|
||||
]
|
||||
|
94
data/schemas/definitions.jsonschema
Normal file
94
data/schemas/definitions.jsonschema
Normal 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
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -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,24 +36,41 @@ def load_jsonschema(schema_name):
|
||||
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):
|
||||
"""Validates data against the keyboard jsonschema.
|
||||
"""
|
||||
schema = load_jsonschema('keyboard')
|
||||
validator = jsonschema.Draft7Validator(schema).validate
|
||||
|
||||
return validator(data)
|
||||
return validate(data, 'qmk.keyboard.v1')
|
||||
|
||||
|
||||
def keyboard_api_validate(data):
|
||||
"""Validates data against the api_keyboard jsonschema.
|
||||
"""
|
||||
base = load_jsonschema('keyboard')
|
||||
relative = load_jsonschema('api_keyboard')
|
||||
resolver = jsonschema.RefResolver.from_schema(base)
|
||||
validator = jsonschema.Draft7Validator(relative, resolver=resolver).validate
|
||||
|
||||
return validator(data)
|
||||
return validate(data, 'qmk.api.keyboard.v1')
|
||||
|
||||
|
||||
def deep_update(origdict, newdict):
|
||||
|
Reference in New Issue
Block a user