blob: 744f63d941efbc4c93c52fabd17637b9c11ecc3d (
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
import operations from 'shared/operations';
const VALID_TOP_KEYS = ['keymaps', 'search', 'blacklist', 'properties'];
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 validateProperties = (properties) => {
for (let name of Object.keys(properties)) {
if (!properties.types[name]) {
throw new Error(`Unknown property name: "${name}"`);
}
if (typeof properties[name] !== properties.types[name]) {
throw new Error(`Invalid type for property: "${name}"`);
}
}
};
const validate = (settings) => {
validateInvalidTopKeys(settings);
if (settings.keymaps) {
validateKeymaps(settings.keymaps);
}
if (settings.search) {
validateSearch(settings.search);
}
if (settings.properties) {
validateProperties(settings.properties);
}
};
export { validate };
|