-
-
Notifications
You must be signed in to change notification settings - Fork 412
/
validateConfig.js
67 lines (51 loc) Β· 1.57 KB
/
validateConfig.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
58
59
60
61
62
63
64
65
66
67
/* eslint no-prototype-builtins: 0 */
'use strict'
const chalk = require('chalk')
const format = require('stringify-object')
const debug = require('debug')('lint-staged:cfg')
const formatError = helpMsg => `β Validation Error:
${helpMsg}
Please refer to https://github.com/okonet/lint-staged#configuration for more information...`
const createError = (opt, helpMsg, value) =>
formatError(`Invalid value for '${chalk.bold(opt)}'.
${helpMsg}.
Configured value is: ${chalk.bold(
format(value, { inlineCharacterLimit: Number.POSITIVE_INFINITY })
)}`)
/**
* Runs config validation. Throws error if the config is not valid.
* @param config {Object}
* @returns config {Object}
*/
module.exports = function validateConfig(config) {
debug('Validating config')
const errors = []
if (!config || typeof config !== 'object') {
errors.push('Configuration should be an object!')
} else {
const globs = Object.keys(config)
if (globs.length === 0) {
errors.push('Configuration should not be empty!')
}
globs.forEach(key => {
if (
(!Array.isArray(config[key]) ||
config[key].some(item => typeof item !== 'string' && typeof item !== 'function')) &&
typeof config[key] !== 'string' &&
typeof config[key] !== 'function'
) {
errors.push(
createError(
key,
'Should be a string, a function, or an array of strings and functions',
key
)
)
}
})
}
if (errors.length) {
throw new Error(errors.join('\n'))
}
return config
}