aboutsummaryrefslogtreecommitdiff
path: root/src/shared/settings/validator.js
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2018-01-04 18:55:24 +0900
committerShin'ya Ueoka <ueokande@i-beam.org>2018-01-05 22:55:25 +0900
commitfbdec04786e28bad45021bef4a74e7077e34282f (patch)
treebf992367f3a60da4b823c593714b10cc09cb031e /src/shared/settings/validator.js
parentc3d1535224231cd379cf503a4c4937342ef27383 (diff)
move settings validator
Diffstat (limited to 'src/shared/settings/validator.js')
-rw-r--r--src/shared/settings/validator.js61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/shared/settings/validator.js b/src/shared/settings/validator.js
new file mode 100644
index 0000000..949ab29
--- /dev/null
+++ b/src/shared/settings/validator.js
@@ -0,0 +1,61 @@
+import operations from 'shared/operations';
+
+const VALID_TOP_KEYS = ['keymaps', 'search', 'blacklist'];
+const VALID_OPERATION_VALUES = Object.keys(operations).map((key) => {
+ return operations[key];
+});
+
+const validateInvalidTopKeys = (settings) => {
+ let invalidKey = Object.keys(settings).find((key) => {
+ return !VALID_TOP_KEYS.includes(key);
+ });
+ if (invalidKey) {
+ throw Error(`Unknown key: "${invalidKey}"`);
+ }
+};
+
+const validateKeymaps = (keymaps) => {
+ for (let key of Object.keys(keymaps)) {
+ let value = keymaps[key];
+ if (!VALID_OPERATION_VALUES.includes(value.type)) {
+ throw Error(`Unknown operation: "${value.type}"`);
+ }
+ }
+};
+
+const validateSearch = (search) => {
+ let engines = search.engines;
+ for (let key of Object.keys(engines)) {
+ if (/\s/.test(key)) {
+ throw new Error(
+ `While space in search engine name is not allowed: "${key}"`
+ );
+ }
+ let url = engines[key];
+ if (!url.match(/{}/)) {
+ throw new Error(`No {}-placeholders in URL of "${key}"`);
+ }
+ if (url.match(/{}/g).length > 1) {
+ throw new Error(`Multiple {}-placeholders in URL of "${key}"`);
+ }
+ }
+
+ if (!search.default) {
+ throw new Error(`Default engine is not set`);
+ }
+ if (!Object.keys(engines).includes(search.default)) {
+ throw new Error(`Default engine "${search.default}" not found`);
+ }
+};
+
+const validate = (settings) => {
+ validateInvalidTopKeys(settings);
+ if (settings.keymaps) {
+ validateKeymaps(settings.keymaps);
+ }
+ if (settings.search) {
+ validateSearch(settings.search);
+ }
+};
+
+export { validate };