From 2fa41a54acfa5e25b5ccad5b3c91210cc42ce00d Mon Sep 17 00:00:00 2001 From: Wojtek Kosior Date: Tue, 27 Jul 2021 11:41:43 +0200 Subject: validate settings on import --- html/options_main.js | 44 ++++++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 14 deletions(-) (limited to 'html/options_main.js') diff --git a/html/options_main.js b/html/options_main.js index 026b9ba..6aed8bb 100644 --- a/html/options_main.js +++ b/html/options_main.js @@ -12,6 +12,7 @@ * IMPORT TYPE_NAME * IMPORT list_prefixes * IMPORT nice_name + * IMPORT parse_json_with_schema * IMPORTS_END */ @@ -223,6 +224,7 @@ function reset_work_bag_li(ul, item, components) ul.work_li.insertBefore(bag_components_ul, old_components_ul); ul.work_li.removeChild(old_components_ul); + console.log("bag components", components); add_bag_components(components); } @@ -543,6 +545,32 @@ function read_file(file) _read_file(file, resolve, reject)); } +const url_regex = /^[a-z0-9]+:\/\/[^/]+\.[^/]{2}(\/[^?#]*)?$/; +const sha256_regex = /^[0-9a-f]{64}$/; +const component_schema = [ + new RegExp(`^[${TYPE_PREFIX.SCRIPT}${TYPE_PREFIX.BAG}]$`), + /.+/ +]; + +const settings_schema = [ + [{}, "matchentry", "minentries", 1, + new RegExp(`^${TYPE_PREFIX.SCRIPT}`), { + /* script data */ + "url": ["optional", url_regex], + "sha256": ["optional", sha256_regex], + "text": ["optional", "string"] + }, + new RegExp(`^${TYPE_PREFIX.BAG}`), [ + "optional", + [component_schema, "repeat"], + "default", undefined + ], + new RegExp(`^${TYPE_PREFIX.PAGE}`), { + /* page data */ + "components": ["optional", component_schema] + }], "repeat" +] + async function import_from_file(event) { let files = event.target.files; @@ -555,30 +583,18 @@ async function import_from_file(event) let result = undefined; try { - result = JSON.parse(await read_file(files[0])); + result = parse_json_with_schema(settings_schema, + await read_file(files[0])); } catch(e) { bad_file_errormsg.textContent = "" + e; import_failed_radio.checked = true; return; } - let errormsg = validate_settings(result); - if (errormsg !== false) { - bad_file_errormsg.textContent = errormsg; - import_failed_radio.checked = true; - return; - } - populate_import_list(result); import_selection_radio.checked = true; } -function validate_settings(settings) -{ - // TODO - return false; -} - function import_li_id(prefix, item) { return `ili_${prefix}_${item}`; -- cgit v1.2.3