Skip to content

Commit 6239643

Browse files
committed
fix: run custom validation on config init or reload
1 parent ae8bfaa commit 6239643

3 files changed

Lines changed: 25 additions & 2 deletions

File tree

index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ if (argv.v) {
4848
process.exit(0);
4949
}
5050

51-
console.log('validating config');
51+
console.log('Validating config');
5252
validate();
5353

5454
console.log('Setting up the proxy and Service');

src/config/ConfigLoader.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { EventEmitter } from 'events';
77
import envPaths from 'env-paths';
88
import { GitProxyConfig, Convert } from './generated/config';
99
import { Configuration, ConfigurationSource, FileSource, HttpSource, GitSource } from './types';
10+
import { validateConfig } from '.';
1011

1112
const execFileAsync = promisify(execFile);
1213

@@ -189,6 +190,11 @@ export class ConfigLoader extends EventEmitter {
189190
} else {
190191
console.log('Configuration has not changed, no update needed');
191192
}
193+
194+
if (!validateConfig(newConfig)) {
195+
console.error('Invalid configuration, skipping reload');
196+
return;
197+
}
192198
} catch (error: any) {
193199
console.error('Error reloading configuration:', error);
194200
this.emit('configurationError', error);

src/config/index.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { existsSync, readFileSync } from 'fs';
22

33
import defaultSettings from '../../proxy.config.json';
4-
import { GitProxyConfig, Convert } from './generated/config';
4+
import { GitProxyConfig, Convert, CommitConfig } from './generated/config';
55
import { ConfigLoader } from './ConfigLoader';
66
import { Configuration } from './types';
77
import { serverConfig } from './env';
@@ -69,9 +69,26 @@ function loadFullConfiguration(): GitProxyConfig {
6969

7070
_currentConfig = mergeConfigurations(defaultConfig, userSettings);
7171

72+
if (!validateConfig(_currentConfig)) {
73+
console.error(
74+
'Invalid configuration: Please check your configuration file and restart GitProxy.',
75+
);
76+
process.exit(1);
77+
}
78+
7279
return _currentConfig;
7380
}
7481

82+
/**
83+
* Performs additional custom validations on the configuration
84+
*
85+
* @param config The configuration to validate
86+
* @returns true if the configuration is valid, false otherwise
87+
*/
88+
export const validateConfig = (config: GitProxyConfig): boolean => {
89+
return validateCommitConfig(config.commitConfig ?? {});
90+
};
91+
7592
/**
7693
* Merge configurations with environment variable overrides
7794
* @param {GitProxyConfig} defaultConfig - The default configuration

0 commit comments

Comments
 (0)