Refactor common JSON schema definitions into a seperate file

- Moved all common definitions from schema_app and schema_lib into a
  separate definitions file.

- Changed the calling code to resolve multiple schema files correctly.
pull/5022/head
Steven Cartmell 2017-09-06 12:55:57 +01:00
parent 3ff7de9771
commit 151f2fa3a6
4 changed files with 115 additions and 176 deletions

View File

@ -25,7 +25,7 @@ from os.path import splitext, relpath
from intelhex import IntelHex
from jinja2 import FileSystemLoader, StrictUndefined
from jinja2.environment import Environment
from jsonschema import validate, Draft4Validator
from jsonschema import Draft4Validator, RefResolver
# Implementation of mbed configuration mechanism
from tools.utils import json_file_to_dict, intelhex_offset
from tools.arm_pack_manager import Cache
@ -408,8 +408,12 @@ class Config(object):
% self.app_config_location))
# Validate the format of the JSON file based on the schema_app.json
schema_path = os.path.join(os.path.dirname(__file__), "schema_app.json")
validator = Draft4Validator(json_file_to_dict(schema_path))
schema_root = os.path.dirname(__file__)
schema_path = os.path.join(schema_root, "schema_app.json")
schema_file = json_file_to_dict(schema_path)
resolver = RefResolver("file://{}/".format(schema_root), schema_file)
validator = Draft4Validator(schema_file, resolver=resolver)
errors = sorted(validator.iter_errors(self.app_config_data))
@ -466,9 +470,13 @@ class Config(object):
continue
# Validate the format of the JSON file based on the schema_lib.json
schema_path = os.path.join(os.path.dirname(__file__),
"schema_lib.json")
validator = Draft4Validator(json_file_to_dict(schema_path))
schema_root = os.path.dirname(__file__)
schema_path = os.path.join(schema_root, "schema_lib.json")
schema_file = json_file_to_dict(schema_path)
resolver = RefResolver("file://{}/".format(schema_root),
schema_file)
validator = Draft4Validator(schema_file, resolver=resolver)
errors = sorted(validator.iter_errors(cfg))

View File

@ -0,0 +1,93 @@
{
"name_definition": {
"description": "Name of the library",
"type": "string",
"items": {
"type": "string"
}
},
"macro_definition": {
"description": "A list of extra macros that will be defined when compiling a project that includes this library.",
"type": "array",
"items": {
"type": "string",
"pattern": "(^[\\w]+$|^[\\w]+=.+$)"
}
},
"config_definition": {
"description": "List of configuration parameters",
"type": "object",
"patternProperties": {
"^[^ ]+$": {
"$ref": "#/config_parameter_base"
}
},
"additionalProperties": false
},
"target_overrides_definition": {
"description": "List of overrides for specific targets",
"type": "object",
"patternProperties": {
"\\*": {
"type": "object",
"patternProperties": {
".*\\..*": {}
},
"additionalProperties": false
},
"^\\S+$": {
"$ref": "#/target_override_entry"
}
},
"additionalProperties": false
},
"config_parameter_long": {
"type": "object",
"properties": {
"help": {
"description": "An optional help message that describes the purpose of the parameter",
"type": "string"
},
"value": {
"description": "An optional field that defines the value of the parameter",
"type": [
"integer",
"string",
"boolean"
]
},
"required": {
"description": "An optional field that specifies whether the parameter must be given a value before compiling the code. (False by default)",
"type": "boolean"
},
"macro_name": {
"description": "An optional field for the macro defined at compile time for this configuration parameter. The system will automatically figure out the macro name from the configuration parameter, but this field will override it",
"type": "string"
}
}
},
"config_parameter_short": {
"type": [
"string",
"integer",
"boolean"
]
},
"config_parameter_base": {
"oneOf": [
{
"$ref": "#/config_parameter_long"
},
{
"$ref": "#/config_parameter_short"
}
]
},
"target_override_entry": {
"type": "object",
"patternProperties": {
"^\\S+$": {}
},
"additionalProperties": false
}
}

View File

@ -4,99 +4,18 @@
"description": "Configuration file for an mbed library",
"type": "object",
"$id": "http://example.com/root.json",
"definitions": {
"config_parameter_long": {
"type": "object",
"properties": {
"help": {
"description": "An optional help message that describes the purpose of the parameter",
"type": "string"
},
"value": {
"description": "An optional field that defines the value of the parameter",
"type": [
"integer",
"string",
"boolean"
]
},
"required": {
"description": "An optional field that specifies whether the parameter must be given a value before compiling the code. (False by default)",
"type": "boolean"
},
"macro_name": {
"description": "An optional field for the macro defined at compile time for this configuration parameter. The system will automatically figure out the macro name from the configuration parameter, but this field will override it",
"type": "string"
}
}
},
"config_parameter_short": {
"type": [
"string",
"integer",
"boolean"
]
},
"config_parameter_base": {
"oneOf": [
{
"$ref": "#/definitions/config_parameter_long"
},
{
"$ref": "#/definitions/config_parameter_short"
}
]
},
"target_override_entry": {
"type": "object",
"patternProperties": {
"^\\S+$": {}
},
"additionalProperties": false
}
},
"properties": {
"name": {
"description": "Name of the library",
"type": "string",
"items": {
"type": "string"
}
"$ref": "file:definitions.json#/name_definition"
},
"config": {
"description": "List of configuration parameters",
"type": "object",
"patternProperties": {
"^[^ ]+$": {
"$ref": "#/definitions/config_parameter_base"
}
},
"additionalProperties": false
"$ref": "file:definitions.json#/config_definition"
},
"target_overrides": {
"description": "List of overrides for specific targets",
"type": "object",
"patternProperties": {
"\\*": {
"type": "object",
"patternProperties": {
".*\\..*": {}
},
"additionalProperties": false
},
"^\\S+$": {
"$ref": "#/definitions/target_override_entry"
}
},
"additionalProperties": false
"$ref": "file:definitions.json#/target_overrides_definition"
},
"macros": {
"description": "A list of extra macros that will be defined when compiling a project that includes this library.",
"type": "array",
"items": {
"type": "string",
"pattern": "(^[\\w]+$|^[\\w]+=.+$)"
}
"$ref": "file:definitions.json#/macro_definition"
},
"artifact_name": {
"type": "string"

View File

@ -4,99 +4,18 @@
"description": "Configuration file for an mbed library",
"type": "object",
"$id": "http://example.com/root.json",
"definitions": {
"config_parameter_long": {
"type": "object",
"properties": {
"help": {
"description": "An optional help message that describes the purpose of the parameter",
"type": "string"
},
"value": {
"description": "An optional field that defines the value of the parameter",
"type": [
"integer",
"string",
"boolean"
]
},
"required": {
"description": "An optional field that specifies whether the parameter must be given a value before compiling the code. (False by default)",
"type": "boolean"
},
"macro_name": {
"description": "An optional field for the macro defined at compile time for this configuration parameter. The system will automatically figure out the macro name from the configuration parameter, but this field will override it",
"type": "string"
}
}
},
"config_parameter_short": {
"type": [
"string",
"integer",
"boolean"
]
},
"config_parameter_base": {
"oneOf": [
{
"$ref": "#/definitions/config_parameter_long"
},
{
"$ref": "#/definitions/config_parameter_short"
}
]
},
"target_override_entry": {
"type": "object",
"patternProperties": {
"^\\S+$": {}
},
"additionalProperties": false
}
},
"properties": {
"name": {
"description": "Name of the library",
"type": "string",
"items": {
"type": "string"
}
"$ref": "file:definitions.json#/name_definition"
},
"config": {
"description": "List of configuration parameters",
"type": "object",
"patternProperties": {
"^[^ ]+$": {
"$ref": "#/definitions/config_parameter_base"
}
},
"additionalProperties": false
"$ref": "file:definitions.json#/config_definition"
},
"target_overrides": {
"description": "List of overrides for specific targets",
"type": "object",
"patternProperties": {
"\\*": {
"type": "object",
"patternProperties": {
".*\\..*": {}
},
"additionalProperties": false
},
"^\\S+$": {
"$ref": "#/definitions/target_override_entry"
}
},
"additionalProperties": false
"$ref": "file:definitions.json#/target_overrides_definition"
},
"macros": {
"description": "A list of extra macros that will be defined when compiling a project that includes this library.",
"type": "array",
"items": {
"type": "string",
"pattern": "(^[\\w]+$|^[\\w]+=.+$)"
}
"$ref": "file:definitions.json#/macro_definition"
}
},
"required": [