1
- import { Plugin , RulesConfig } from '@commitlint/types' ;
1
+ import { UserConfig } from '@commitlint/types' ;
2
2
3
3
export function isObjectLike ( obj : unknown ) : obj is Record < string , unknown > {
4
4
return Boolean ( obj ) && typeof obj === 'object' ; // typeof null === 'object'
@@ -21,21 +21,22 @@ export function isParserOptsFunction<T extends Record<string, unknown>>(
21
21
return typeof obj . parserOpts === 'function' ;
22
22
}
23
23
24
- export function validateConfig (
25
- config : Record < string , unknown >
26
- ) : asserts config is {
27
- formatter : string ;
28
- ignores ?: ( ( commit : string ) => boolean ) [ ] ;
29
- defaultIgnores ?: boolean ;
30
- plugins ?: ( Plugin | string ) [ ] ;
31
- rules : Partial < RulesConfig > ;
32
- helpUrl : string ;
33
- [ key : string ] : unknown ;
34
- } {
24
+ export function validateConfig < T extends Record < string , unknown > > (
25
+ config : T
26
+ ) : asserts config is Omit < UserConfig , 'parserPreset' > & T {
35
27
if ( ! isObjectLike ( config ) ) {
36
28
throw new Error ( 'Invalid configuration, `parserPreset` must be an object' ) ;
37
29
}
38
- if ( typeof config . formatter !== 'string' ) {
30
+ if (
31
+ config . extends &&
32
+ typeof config . extends !== 'string' &&
33
+ ! Array . isArray ( config . extends )
34
+ ) {
35
+ throw new Error (
36
+ 'Invalid configuration, `extends` must be a array or string'
37
+ ) ;
38
+ }
39
+ if ( config . formatter && typeof config . formatter !== 'string' ) {
39
40
throw new Error ( 'Invalid configuration, `formatter` must be a string' ) ;
40
41
}
41
42
if ( config . ignores && ! Array . isArray ( config . ignores ) ) {
@@ -44,7 +45,11 @@ export function validateConfig(
44
45
if ( config . plugins && ! Array . isArray ( config . plugins ) ) {
45
46
throw new Error ( 'Invalid configuration, `plugins` must ba an array' ) ;
46
47
}
48
+ if ( config . rules && typeof config . rules !== 'object' ) {
49
+ throw new Error ( 'Invalid configuration, `rules` must ba an object' ) ;
50
+ }
47
51
if (
52
+ config . defaultIgnores &&
48
53
typeof config . defaultIgnores !== 'boolean' &&
49
54
typeof config . defaultIgnores !== 'undefined'
50
55
) {
@@ -56,3 +61,17 @@ export function validateConfig(
56
61
throw new Error ( 'Invalid configuration, `helpUrl` must be a string' ) ;
57
62
}
58
63
}
64
+
65
+ export function validateParser (
66
+ parser : unknown
67
+ ) : asserts parser is { name ?: string ; path ?: string ; [ key : string ] : unknown } {
68
+ if ( ! isObjectLike ( parser ) ) {
69
+ throw new Error ( 'Invalid configuration, `parserPreset` must be an object' ) ;
70
+ }
71
+ if ( parser . name && typeof parser . name !== 'string' ) {
72
+ throw new Error ( 'Invalid configuration, `parserPreset` must have a name' ) ;
73
+ }
74
+ if ( parser . path && typeof parser . path !== 'string' ) {
75
+ throw new Error ( 'Invalid configuration, `parserPreset` must have a name' ) ;
76
+ }
77
+ }
0 commit comments