summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWojtek Kosior <koszko@koszko.org>2021-07-27 11:41:43 +0200
committerWojtek Kosior <koszko@koszko.org>2021-07-27 11:41:43 +0200
commit2fa41a54acfa5e25b5ccad5b3c91210cc42ce00d (patch)
tree05564233adc57a489f0e07276c85e9e33df1d993
parent64afd5b9415d62c1f178ca78a8358bd3503d5855 (diff)
downloadbrowser-extension-2fa41a54acfa5e25b5ccad5b3c91210cc42ce00d.tar.gz
browser-extension-2fa41a54acfa5e25b5ccad5b3c91210cc42ce00d.zip
validate settings on import
-rw-r--r--common/sanitize_JSON.js18
-rw-r--r--html/options_main.js44
2 files changed, 33 insertions, 29 deletions
diff --git a/common/sanitize_JSON.js b/common/sanitize_JSON.js
index 3fc5007..8268d3e 100644
--- a/common/sanitize_JSON.js
+++ b/common/sanitize_JSON.js
@@ -31,7 +31,6 @@ function error_message(cause)
function sanitize_unknown(schema, item)
{
- console.log(`sanitize_unknown ${JSON.stringify(schema)}`);
let error_msg = undefined;
let schema_options = [];
let has_default = false;
@@ -89,9 +88,7 @@ function sanitize_unknown(schema, item)
function sanitize_unknown_no_alternatives(schema, item)
{
- console.log(`sanitize_unknown_no_alternatives ${JSON.stringify(schema)}`);
for (const [schema_check, item_check, sanitizer, type_name] of checks) {
- console.log(`checking ${type_name}`);
if (schema_check(schema)) {
if (item_check(item))
return sanitizer(schema, item);
@@ -205,7 +202,6 @@ function sanitize_genobj(schema, object)
function sanitize_array(schema, array)
{
- console.log(`sanitize_array ${JSON.stringify(schema)}`);
let min_length = 0;
let max_length = Infinity;
let repeat_length = 1;
@@ -231,14 +227,14 @@ function sanitize_array(schema, array)
}
if (["repeat", "repeatfull"].includes(schema[schema.length - 1])) {
var repeat_directive = schema.pop();
- repeat = schema.splice(schema.length - repeat_length);
+ var repeat = schema.splice(schema.length - repeat_length);
} else if (schema.length !== array.length) {
- throw error_message(`does not not have exactly ${schema.length} items`);
+ throw error_message(`does not have exactly ${schema.length} items`);
}
if (repeat_directive === "repeatfull" &&
(array.length - schema.length) % repeat_length !== 0)
- throw error_message(`does not not contain a full number of item group repetitions`);
+ throw error_message(`does not contain a full number of item group repetitions`);
if (array.length < min_length)
throw error_message(`has less than ${min_length} element${min_length === 1 ? "" : "s"}`);
@@ -246,8 +242,6 @@ function sanitize_array(schema, array)
if (array.length > max_length)
throw error_message(`has more than ${max_length} element${max_length === 1 ? "" : "s"}`);
- console.log(schema, repeat);
-
for (const item of array) {
if (i >= schema.length) {
i = 0;
@@ -268,7 +262,6 @@ function sanitize_array(schema, array)
function sanitize_regex(schema, string)
{
- console.log(`sanitize_regex ${schema}`);
if (schema.test(string))
return string;
@@ -279,7 +272,6 @@ const string_spec_regex = /^string(:(.*))?$/;
function sanitize_string(schema, string)
{
- console.log(`sanitize_string ${JSON.stringify(schema)}`);
const regex = string_spec_regex.exec(schema)[2];
if (regex === undefined)
@@ -290,7 +282,6 @@ function sanitize_string(schema, string)
function sanitize_object(schema, object)
{
- console.log(`sanitize_object ${JSON.stringify(schema)}`);
const result = {};
for (let [key, entry_schema] of Object.entries(schema)) {
@@ -304,7 +295,6 @@ function sanitize_object(schema, object)
function sanitize_object_entry(result, key, entry_schema, object)
{
- console.log(`sanitize_object_entry ${JSON.stringify(entry_schema)}`);
let optional = false;
let has_default = false;
let _default = undefined;
@@ -347,7 +337,6 @@ function sanitize_object_entry(result, key, entry_schema, object)
function take_literal(schema, item)
{
- console.log(`take_literal ${JSON.stringify(schema)}`);
return item;
}
@@ -357,7 +346,6 @@ function take_literal(schema, item)
*/
function discard(schema, item)
{
- console.log(`discard ${JSON.stringify(schema)}`);
return discard;
}
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}`;