diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2018-01-04 18:55:24 +0900 |
---|---|---|
committer | Shin'ya Ueoka <ueokande@i-beam.org> | 2018-01-05 22:55:25 +0900 |
commit | fbdec04786e28bad45021bef4a74e7077e34282f (patch) | |
tree | bf992367f3a60da4b823c593714b10cc09cb031e /src/shared/validators | |
parent | c3d1535224231cd379cf503a4c4937342ef27383 (diff) |
move settings validator
Diffstat (limited to 'src/shared/validators')
-rw-r--r-- | src/shared/validators/setting.js | 61 |
1 files changed, 0 insertions, 61 deletions
diff --git a/src/shared/validators/setting.js b/src/shared/validators/setting.js deleted file mode 100644 index 949ab29..0000000 --- a/src/shared/validators/setting.js +++ /dev/null @@ -1,61 +0,0 @@ -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 }; |