1 | const {isString, isNil, isArray, isPlainObject} = require('lodash');
|
2 | const AggregateError = require('aggregate-error');
|
3 | const getError = require('./get-error');
|
4 | const resolveConfig = require('./resolve-config');
|
5 |
|
6 | const isNonEmptyString = value => isString(value) && value.trim();
|
7 | const isStringOrStringArray = value => isNonEmptyString(value) || (isArray(value) && value.every(isNonEmptyString));
|
8 | const isArrayOf = validator => array => isArray(array) && array.every(value => validator(value));
|
9 | const canBeDisabled = validator => value => value === false || validator(value);
|
10 |
|
11 | const VALIDATORS = {
|
12 | assets: canBeDisabled(
|
13 | isArrayOf(asset => isStringOrStringArray(asset) || (isPlainObject(asset) && isStringOrStringArray(asset.path)))
|
14 | ),
|
15 | message: isNonEmptyString,
|
16 | };
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | module.exports = pluginConfig => {
|
28 | const options = resolveConfig(pluginConfig);
|
29 |
|
30 | const errors = Object.entries(options).reduce(
|
31 | (errors, [option, value]) =>
|
32 | !isNil(value) && !VALIDATORS[option](value)
|
33 | ? [...errors, getError(`EINVALID${option.toUpperCase()}`, {[option]: value})]
|
34 | : errors,
|
35 | []
|
36 | );
|
37 |
|
38 | if (errors.length > 0) {
|
39 | throw new AggregateError(errors);
|
40 | }
|
41 | };
|