@@ -17,68 +17,75 @@ export const validateConfig = (config: GitProxyConfig): boolean => {
1717 * @returns true if the commit configuration is valid, false otherwise
1818 */
1919function validateCommitConfig ( config : GitProxyConfig ) : boolean {
20- if ( config . commitConfig ?. author ?. email ?. local ?. block ) {
21- try {
22- new RegExp ( config . commitConfig . author . email . local . block ) ;
23- } catch ( error : unknown ) {
24- console . error (
25- `Invalid regular expression for commitConfig.author.email.local.block: ${ config . commitConfig . author . email . local . block } ` ,
26- ) ;
27- return false ;
28- }
29- }
20+ return (
21+ validateConfigRegex ( config , 'commitConfig.author.email.local.block' ) &&
22+ validateConfigRegex ( config , 'commitConfig.author.email.domain.allow' ) &&
23+ validateConfigRegex ( config , 'commitConfig.message.block.patterns' ) &&
24+ validateConfigRegex ( config , 'commitConfig.diff.block.patterns' ) &&
25+ validateConfigRegex ( config , 'commitConfig.diff.block.providers' )
26+ ) ;
27+ }
3028
31- if ( config . commitConfig ?. author ?. email ?. domain ?. allow ) {
32- try {
33- new RegExp ( config . commitConfig . author . email . domain . allow ) ;
34- } catch ( error : unknown ) {
35- console . error (
36- `Invalid regular expression for commitConfig.author.email.domain.allow: ${ config . commitConfig . author . email . domain . allow } ` ,
37- ) ;
38- return false ;
39- }
29+ /**
30+ * Validates that a regular expression is valid.
31+ * @param pattern The regular expression to validate
32+ * @param context The context of the regular expression
33+ * @returns true if the regular expression is valid, false otherwise
34+ */
35+ function isValidRegex ( pattern : string , context : string ) : boolean {
36+ try {
37+ new RegExp ( pattern ) ;
38+ return true ;
39+ } catch {
40+ console . error ( `Invalid regular expression for ${ context } : ${ pattern } ` ) ;
41+ return false ;
4042 }
43+ }
4144
42- if ( config . commitConfig ?. message ?. block ?. patterns ) {
43- for ( const pattern of config . commitConfig . message . block . patterns ) {
44- try {
45- new RegExp ( pattern ) ;
46- } catch ( error : unknown ) {
47- console . error (
48- `Invalid regular expression for commitConfig.message.block.patterns: ${ pattern } ` ,
49- ) ;
50- return false ;
45+ /**
46+ * Validates that a value in the configuration is a valid regular expression.
47+ * @param config The configuration to validate
48+ * @param path The path to the value to validate
49+ * @returns true if the value is a valid regular expression, false otherwise
50+ */
51+ function validateConfigRegex ( config : GitProxyConfig , path : string ) : boolean {
52+ const getValueAtPath = ( obj : unknown , path : string ) : unknown => {
53+ return path . split ( '.' ) . reduce ( ( current , key ) => {
54+ if ( current == null || typeof current !== 'object' ) {
55+ return undefined ;
5156 }
52- }
57+ return ( current as Record < string , unknown > ) [ key ] ;
58+ } , obj ) ;
59+ } ;
60+
61+ const value = getValueAtPath ( config , path ) ;
62+
63+ if ( ! value ) return true ;
64+
65+ if ( typeof value === 'string' ) {
66+ return isValidRegex ( value , path ) ;
5367 }
5468
55- if ( config . commitConfig ?. diff ?. block ?. patterns ) {
56- for ( const pattern of config . commitConfig . diff . block . patterns ) {
57- try {
58- new RegExp ( pattern ) ;
59- } catch ( error : unknown ) {
60- console . error (
61- `Invalid regular expression for commitConfig.diff.block.patterns: ${ pattern } ` ,
62- ) ;
63- return false ;
64- }
69+ if ( Array . isArray ( value ) ) {
70+ for ( const pattern of value ) {
71+ if ( ! isValidRegex ( pattern , path ) ) return false ;
6572 }
73+ return true ;
6674 }
6775
68- if ( config . commitConfig ?. diff ?. block ?. providers ) {
69- for ( const [ key , value ] of Object . entries ( config . commitConfig . diff . block . providers ) ) {
70- try {
71- new RegExp ( value ) ;
72- } catch ( error : unknown ) {
73- console . error ( `Invalid regular expression for commitConfig.diff.block.providers: ${ value } ` ) ;
74- return false ;
75- }
76- }
76+ if ( typeof value === 'object' ) {
77+ return Object . values ( value ) . every ( ( pattern ) => isValidRegex ( pattern as string , path ) ) ;
7778 }
7879
7980 return true ;
8081}
8182
83+ /**
84+ * Loads and parses a GitProxyConfig object from a given context and loading strategy.
85+ * @param context The context of the configuration
86+ * @param loader The loading strategy to use
87+ * @returns The parsed GitProxyConfig object
88+ */
8289export async function loadConfig (
8390 context : string ,
8491 loader : ( ) => Promise < string > ,
@@ -87,6 +94,12 @@ export async function loadConfig(
8794 return parseGitProxyConfig ( raw , context ) ;
8895}
8996
97+ /**
98+ * Parses a raw string into a GitProxyConfig object.
99+ * @param raw The raw string to parse
100+ * @param context The context of the configuration
101+ * @returns The parsed GitProxyConfig object
102+ */
90103function parseGitProxyConfig ( raw : string , context : string ) : GitProxyConfig {
91104 try {
92105 return Convert . toGitProxyConfig ( raw ) ;
0 commit comments