mirror of
https://github.com/betaflight/betaflight-configurator.git
synced 2025-07-24 00:35:26 +03:00
Add VTX Config JSON Schema validation
This commit is contained in:
parent
1d7ec1a764
commit
510056b278
2 changed files with 250 additions and 42 deletions
|
@ -10,6 +10,7 @@ TABS.vtx = {
|
|||
VTXTABLE_POWERLEVEL_LIST: [],
|
||||
analyticsChanges: {},
|
||||
updating: true,
|
||||
env: new djv(),
|
||||
};
|
||||
|
||||
TABS.vtx.initialize = function (callback) {
|
||||
|
@ -98,6 +99,35 @@ TABS.vtx.initialize = function (callback) {
|
|||
}
|
||||
}
|
||||
|
||||
// Validates the vtxConfig object against a JSON Schema
|
||||
function validateVtxJson(vtxConfig, callback_valid, callback_error) {
|
||||
|
||||
// At minimum the version must be defined
|
||||
if (!vtxConfig.version) {
|
||||
console.error("Validation against schema failed, version missing");
|
||||
callback_error();
|
||||
}
|
||||
|
||||
// Load schema
|
||||
const urlVtxSchema = chrome.runtime.getURL(`resources/jsonschema/vtxconfig_schema-${vtxConfig.version}.json`);
|
||||
|
||||
fetch(urlVtxSchema)
|
||||
.then(response => response.json())
|
||||
.catch(error => console.error('Error fetching VTX Schema:', error))
|
||||
.then(schemaJson => {
|
||||
|
||||
let valid = false;
|
||||
if (schemaJson !== undefined) {
|
||||
// Validate
|
||||
valid = (TABS.vtx.env.validate(schemaJson, vtxConfig) === undefined);
|
||||
}
|
||||
|
||||
console.log("Validation against schema result:", valid);
|
||||
valid ? callback_valid() : callback_error();
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
// Emulates the MSP read from a vtxConfig object (JSON)
|
||||
function read_vtx_config_json(vtxConfig, vtxcallback_after_read) {
|
||||
|
||||
|
@ -658,16 +688,32 @@ TABS.vtx.initialize = function (callback) {
|
|||
|
||||
let text = e.target.result;
|
||||
try {
|
||||
|
||||
let vtxConfig = JSON.parse(text);
|
||||
read_vtx_config_json(vtxConfig, load_html);
|
||||
|
||||
TABS.vtx.vtxTableSavePending = true;
|
||||
validateVtxJson(
|
||||
vtxConfig,
|
||||
function() {
|
||||
|
||||
self.analyticsChanges['VtxTableLoadFromClipboard'] = undefined;
|
||||
self.analyticsChanges['VtxTableLoadFromFile'] = file.name;
|
||||
// JSON is valid
|
||||
read_vtx_config_json(vtxConfig, load_html);
|
||||
|
||||
console.log('Load VTX file end');
|
||||
GUI.log(i18n.getMessage('vtxLoadFileOk'));
|
||||
TABS.vtx.vtxTableSavePending = true;
|
||||
|
||||
self.analyticsChanges['VtxTableLoadFromClipboard'] = undefined;
|
||||
self.analyticsChanges['VtxTableLoadFromFile'] = file.name;
|
||||
|
||||
console.log('Load VTX file end');
|
||||
GUI.log(i18n.getMessage('vtxLoadFileOk'));
|
||||
},
|
||||
function() {
|
||||
|
||||
// JSON is NOT valid
|
||||
console.error('VTX Config from file failed validation against schema');
|
||||
GUI.log(i18n.getMessage('vtxLoadFileKo'));
|
||||
|
||||
}
|
||||
);
|
||||
|
||||
} catch (err) {
|
||||
console.error('Failed loading VTX file config');
|
||||
|
@ -694,15 +740,29 @@ TABS.vtx.initialize = function (callback) {
|
|||
console.log('Pasted content: ', text);
|
||||
|
||||
let vtxConfig = JSON.parse(text);
|
||||
read_vtx_config_json(vtxConfig, load_html);
|
||||
|
||||
TABS.vtx.vtxTableSavePending = true;
|
||||
validateVtxJson(
|
||||
vtxConfig,
|
||||
function() {
|
||||
|
||||
self.analyticsChanges['VtxTableLoadFromFile'] = undefined;
|
||||
self.analyticsChanges['VtxTableLoadFromClipboard'] = text.length;
|
||||
// JSON is valid
|
||||
read_vtx_config_json(vtxConfig, load_html);
|
||||
|
||||
console.log('Load VTX clipboard end');
|
||||
GUI.log(i18n.getMessage('vtxLoadClipboardOk'));
|
||||
TABS.vtx.vtxTableSavePending = true;
|
||||
|
||||
self.analyticsChanges['VtxTableLoadFromFile'] = undefined;
|
||||
self.analyticsChanges['VtxTableLoadFromClipboard'] = text.length;
|
||||
|
||||
console.log('Load VTX clipboard end');
|
||||
GUI.log(i18n.getMessage('vtxLoadClipboardOk'));
|
||||
},
|
||||
function() {
|
||||
|
||||
// JSON is NOT valid
|
||||
GUI.log(i18n.getMessage('vtxLoadClipboardKo'));
|
||||
console.error('VTX Config from clipboard failed validation against schema');
|
||||
}
|
||||
);
|
||||
|
||||
}, function(err) {
|
||||
GUI.log(i18n.getMessage('vtxLoadClipboardKo'));
|
||||
|
|
148
src/resources/jsonschema/vtxconfig_schema-1.0.json
Normal file
148
src/resources/jsonschema/vtxconfig_schema-1.0.json
Normal file
|
@ -0,0 +1,148 @@
|
|||
{
|
||||
"definitions": {},
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"$id": "http://example.com/root.json",
|
||||
"type": "object",
|
||||
"title": "The Root Schema",
|
||||
"required": [
|
||||
"description",
|
||||
"version",
|
||||
"vtx_table"
|
||||
],
|
||||
"properties": {
|
||||
"description": {
|
||||
"$id": "#/properties/description",
|
||||
"type": "string",
|
||||
"title": "The Description Schema",
|
||||
"default": "",
|
||||
"examples": [
|
||||
"Betaflight VTX Config file for SmartAudio 2.0 (US version)"
|
||||
],
|
||||
"pattern": "^(.*)$"
|
||||
},
|
||||
"version": {
|
||||
"$id": "#/properties/version",
|
||||
"type": "string",
|
||||
"title": "The Version Schema",
|
||||
"default": "",
|
||||
"examples": [
|
||||
"1.0"
|
||||
],
|
||||
"pattern": "^(.*)$"
|
||||
},
|
||||
"vtx_table": {
|
||||
"$id": "#/properties/vtx_table",
|
||||
"type": "object",
|
||||
"title": "The Vtx_table Schema",
|
||||
"required": [
|
||||
"bands_list",
|
||||
"powerlevels_list"
|
||||
],
|
||||
"properties": {
|
||||
"bands_list": {
|
||||
"$id": "#/properties/vtx_table/properties/bands_list",
|
||||
"type": "array",
|
||||
"title": "The Bands_list Schema",
|
||||
"items": {
|
||||
"$id": "#/properties/vtx_table/properties/bands_list/items",
|
||||
"type": "object",
|
||||
"title": "The Items Schema",
|
||||
"required": [
|
||||
"name",
|
||||
"letter",
|
||||
"is_factory_band",
|
||||
"frequencies"
|
||||
],
|
||||
"properties": {
|
||||
"name": {
|
||||
"$id": "#/properties/vtx_table/properties/bands_list/items/properties/name",
|
||||
"type": "string",
|
||||
"title": "The Name Schema",
|
||||
"default": "",
|
||||
"examples": [
|
||||
"BOSCAM_A"
|
||||
],
|
||||
"pattern": "^(.*)$"
|
||||
},
|
||||
"letter": {
|
||||
"$id": "#/properties/vtx_table/properties/bands_list/items/properties/letter",
|
||||
"type": "string",
|
||||
"title": "The Letter Schema",
|
||||
"default": "",
|
||||
"examples": [
|
||||
"A"
|
||||
],
|
||||
"pattern": "^(.*)$"
|
||||
},
|
||||
"is_factory_band": {
|
||||
"$id": "#/properties/vtx_table/properties/bands_list/items/properties/is_factory_band",
|
||||
"type": "boolean",
|
||||
"title": "The Is_factory_band Schema",
|
||||
"default": false,
|
||||
"examples": [
|
||||
true
|
||||
]
|
||||
},
|
||||
"frequencies": {
|
||||
"$id": "#/properties/vtx_table/properties/bands_list/items/properties/frequencies",
|
||||
"type": "array",
|
||||
"title": "The Frequencies Schema",
|
||||
"items": {
|
||||
"$id": "#/properties/vtx_table/properties/bands_list/items/properties/frequencies/items",
|
||||
"type": "integer",
|
||||
"title": "The Items Schema",
|
||||
"default": 0,
|
||||
"examples": [
|
||||
5865,
|
||||
5845,
|
||||
5825,
|
||||
5805,
|
||||
5785,
|
||||
5765,
|
||||
5745,
|
||||
5725
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"powerlevels_list": {
|
||||
"$id": "#/properties/vtx_table/properties/powerlevels_list",
|
||||
"type": "array",
|
||||
"title": "The Powerlevels_list Schema",
|
||||
"items": {
|
||||
"$id": "#/properties/vtx_table/properties/powerlevels_list/items",
|
||||
"type": "object",
|
||||
"title": "The Items Schema",
|
||||
"required": [
|
||||
"value",
|
||||
"label"
|
||||
],
|
||||
"properties": {
|
||||
"value": {
|
||||
"$id": "#/properties/vtx_table/properties/powerlevels_list/items/properties/value",
|
||||
"type": "integer",
|
||||
"title": "The Value Schema",
|
||||
"default": 0,
|
||||
"examples": [
|
||||
0
|
||||
]
|
||||
},
|
||||
"label": {
|
||||
"$id": "#/properties/vtx_table/properties/powerlevels_list/items/properties/label",
|
||||
"type": "string",
|
||||
"title": "The Label Schema",
|
||||
"default": "",
|
||||
"examples": [
|
||||
"25 "
|
||||
],
|
||||
"pattern": "^(.*)$"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue