aboutsummaryrefslogtreecommitdiff
path: root/src/shared/validators
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2017-10-01 11:58:34 +0900
committerShin'ya Ueoka <ueokande@i-beam.org>2017-10-01 12:17:58 +0900
commit2d0968a70b78710e854075d96a8e643e2cd2b3a7 (patch)
treed15d54b340cb461378627db035e6410c253b1052 /src/shared/validators
parent61806a4e7ff01e8b5fdfdcef0308a2c41fc98778 (diff)
add setting validator
Diffstat (limited to 'src/shared/validators')
-rw-r--r--src/shared/validators/setting.js33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/shared/validators/setting.js b/src/shared/validators/setting.js
new file mode 100644
index 0000000..df04e50
--- /dev/null
+++ b/src/shared/validators/setting.js
@@ -0,0 +1,33 @@
+import operations from '../../operations';
+
+const VALID_TOP_KEYS = ['keymaps'];
+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 validate = (settings) => {
+ validateInvalidTopKeys(settings);
+ if (settings.keymaps) {
+ validateKeymaps(settings.keymaps);
+ }
+};
+
+export { validate };