-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathvalidate.js
57 lines (51 loc) · 1.19 KB
/
validate.js
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
'use strict';
const {
difference,
forEach,
isArray,
isBoolean,
isFunction,
isPlainObject,
isRegExp,
isString,
keys,
} = require('lodash');
const rules = {
// hook
camelCase: 'boolean|string',
devMode: 'boolean',
extensions: 'array|string',
ignore: 'function|regex|string',
preprocessCss: 'function',
processCss: 'function',
processorOpts: 'object',
// plugins
append: 'array',
prepend: 'array',
use: 'array',
createImportedName: 'function',
generateScopedName: 'function|string',
hashPrefix: 'string',
mode: 'string',
rootDir: 'string',
noCache: 'boolean',
};
const tests = {
array: isArray,
boolean: isBoolean,
function: isFunction,
object: isPlainObject,
regex: isRegExp,
string: isString,
};
module.exports = function validate(options) {
const unknownOptions = difference(keys(options), keys(rules));
if (unknownOptions.length)
throw new Error(`unknown arguments: ${unknownOptions.join(', ')}.`);
forEach(rules, (types, rule) => {
if (typeof options[rule] === 'undefined')
return;
if (!types.split('|').some(type => tests[type](options[rule])))
throw new TypeError(`should specify ${types} as ${rule}`);
});
};