/
validators.ts
77 lines (72 loc) 路 2.44 KB
/
validators.ts
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
76
77
import {UserConfig} from '@commitlint/types';
export function isObjectLike(obj: unknown): obj is Record<string, unknown> {
return Boolean(obj) && typeof obj === 'object'; // typeof null === 'object'
}
export function isPromiseLike(obj: unknown): obj is Promise<unknown> {
return (
(typeof obj === 'object' || typeof obj === 'function') &&
typeof (obj as any).then === 'function'
);
}
export function isParserOptsFunction<T extends Record<string, unknown>>(
obj: T
): obj is T & {
parserOpts: (
cb: (_: never, parserOpts: Record<string, unknown>) => unknown
) => Record<string, unknown> | undefined;
} {
return typeof obj.parserOpts === 'function';
}
export function validateConfig<T extends Record<string, unknown>>(
config: T
): asserts config is Omit<UserConfig, 'parserPreset'> & T {
if (!isObjectLike(config)) {
throw new Error('Invalid configuration, `parserPreset` must be an object');
}
if (
config.extends &&
typeof config.extends !== 'string' &&
!Array.isArray(config.extends)
) {
throw new Error(
'Invalid configuration, `extends` must be a array or string'
);
}
if (config.formatter && typeof config.formatter !== 'string') {
throw new Error('Invalid configuration, `formatter` must be a string');
}
if (config.ignores && !Array.isArray(config.ignores)) {
throw new Error('Invalid configuration, `ignores` must ba an array');
}
if (config.plugins && !Array.isArray(config.plugins)) {
throw new Error('Invalid configuration, `plugins` must ba an array');
}
if (config.rules && typeof config.rules !== 'object') {
throw new Error('Invalid configuration, `rules` must ba an object');
}
if (
config.defaultIgnores &&
typeof config.defaultIgnores !== 'boolean' &&
typeof config.defaultIgnores !== 'undefined'
) {
throw new Error(
'Invalid configuration, `defaultIgnores` must ba true/false'
);
}
if (typeof config.helpUrl !== 'string') {
throw new Error('Invalid configuration, `helpUrl` must be a string');
}
}
export function validateParser(
parser: unknown
): asserts parser is {name?: string; path?: string; [key: string]: unknown} {
if (!isObjectLike(parser)) {
throw new Error('Invalid configuration, `parserPreset` must be an object');
}
if (parser.name && typeof parser.name !== 'string') {
throw new Error('Invalid configuration, `parserPreset` must have a name');
}
if (parser.path && typeof parser.path !== 'string') {
throw new Error('Invalid configuration, `parserPreset` must have a name');
}
}