aboutsummaryrefslogtreecommitdiff
path: root/src/shared/validators/setting.js
blob: 0b853a89a92a042ed695e2f7ee7592293d0c3ccf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import operations from '../../operations';

const VALID_TOP_KEYS = ['keymaps', 'search'];
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 };