This repository was archived by the owner on Apr 14, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig-validator.ts
More file actions
408 lines (344 loc) · 12.4 KB
/
Copy pathconfig-validator.ts
File metadata and controls
408 lines (344 loc) · 12.4 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#!/usr/bin/env bun
/**
* Configuration Validator Script
*
* Validates configuration files against the JSON schema and
* checks for common configuration issues across profiles.
*/
import Ajv from "ajv";
import { readFileSync, readdirSync, existsSync } from "fs";
import { join, extname } from "path";
interface ValidationResult {
file: string;
valid: boolean;
errors: string[];
warnings: string[];
}
interface ValidationReport {
timestamp: Date;
schemaVersion: string;
totalFiles: number;
validFiles: number;
invalidFiles: number;
results: ValidationResult[];
summary: {
commonErrors: Record<string, number>;
commonWarnings: Record<string, number>;
};
}
class ConfigValidator {
private ajv: Ajv;
private schema: any;
private schemaPath: string;
private configPaths: string[];
constructor(schemaPath: string, configPaths: string[]) {
this.ajv = new Ajv({ allErrors: true });
this.schemaPath = schemaPath;
this.configPaths = configPaths;
this.loadSchema();
}
private loadSchema(): void {
if (!existsSync(this.schemaPath)) {
throw new Error(`Schema file not found: ${this.schemaPath}`);
}
const schemaContent = readFileSync(this.schemaPath, 'utf-8');
this.schema = JSON.parse(schemaContent);
}
/**
* Validate a single configuration file
*/
private validateConfig(filePath: string): ValidationResult {
const result: ValidationResult = {
file: filePath,
valid: false,
errors: [],
warnings: [],
};
try {
if (!existsSync(filePath)) {
result.errors.push('Configuration file not found');
return result;
}
const configContent = readFileSync(filePath, 'utf-8');
let config: any;
try {
config = JSON.parse(configContent);
} catch (parseError: any) {
result.errors.push(`JSON parsing error: ${parseError.message}`);
return result;
}
// Validate against schema
const validate = this.ajv.compile(this.schema);
const isValid = validate(config);
if (isValid) {
result.valid = true;
// Add custom warnings for best practices
this.addCustomWarnings(config, result);
} else {
result.valid = false;
if (validate.errors) {
for (const error of validate.errors) {
const errorMsg = this.formatAjvError(error);
result.errors.push(errorMsg);
}
}
}
// Additional semantic validations
this.performSemanticValidation(config, result);
} catch (error: any) {
result.errors.push(`Validation error: ${error.message}`);
}
return result;
}
/**
* Format AJV validation errors into readable messages
*/
private formatAjvError(error: any): string {
const instancePath = error.instancePath || 'root';
const message = error.message;
switch (error.keyword) {
case 'required':
return `Missing required property: ${error.params.missingProperty}`;
case 'type':
return `Property '${instancePath}' should be ${error.params.type}`;
case 'enum':
return `Property '${instancePath}' should be one of: ${error.params.allowedValues.join(', ')}`;
case 'minimum':
return `Property '${instancePath}' should be >= ${error.params.limit}`;
case 'maximum':
return `Property '${instancePath}' should be <= ${error.params.limit}`;
default:
return `${instancePath}: ${message}`;
}
}
/**
* Add custom warnings for configuration best practices
*/
private addCustomWarnings(config: any, result: ValidationResult): void {
// Check for potentially problematic settings
if (config.review_rules?.min_test_coverage < 50) {
result.warnings.push('Low test coverage requirement (< 50%) may indicate insufficient testing standards');
}
if (config.review_rules?.max_complexity > 20) {
result.warnings.push('High complexity threshold (> 20) may allow overly complex code');
}
if (config.review_rules?.max_function_length > 100) {
result.warnings.push('High function length limit (> 100) may allow overly long functions');
}
// Check security settings
if (config.security_rules && Object.values(config.security_rules).some((rule: any) => rule === false)) {
const disabledRules = Object.entries(config.security_rules)
.filter(([_, enabled]) => enabled === false)
.map(([rule]) => rule);
result.warnings.push(`Disabled security checks: ${disabledRules.join(', ')}`);
}
// Check exclusion patterns
if (config.exclusions?.file_patterns?.length > 20) {
result.warnings.push('Large number of file exclusion patterns may hide important files from review');
}
// Check response settings
if (config.response_settings?.max_response_length > 5000) {
result.warnings.push('Very high response length limit may lead to overly verbose responses');
}
// Check for deprecated or risky model settings
if (config.model && config.model.includes('haiku')) {
result.warnings.push('Using Haiku model may provide less comprehensive reviews than Sonnet');
}
// Check language-specific configurations
if (config.language_specific?.typescript?.strict_mode === false) {
result.warnings.push('TypeScript strict mode disabled - may miss type-related issues');
}
if (config.language_specific?.python?.check_type_hints === false) {
result.warnings.push('Python type hint checking disabled - may miss type-related issues');
}
}
/**
* Perform semantic validation beyond schema checking
*/
private performSemanticValidation(config: any, result: ValidationResult): void {
// Check for logical inconsistencies
if (config.review_rules?.check_tests === false && config.review_rules?.min_test_coverage > 0) {
result.errors.push('Inconsistent configuration: test coverage specified but test checking disabled');
}
if (config.review_rules?.check_documentation === false &&
(config.documentation_rules?.require_function_docs === true ||
config.documentation_rules?.require_class_docs === true)) {
result.errors.push('Inconsistent configuration: documentation requirements specified but documentation checking disabled');
}
// Check for circular or contradictory exclusions
if (config.exclusions?.file_patterns && config.exclusions?.directory_patterns) {
const filePatterns = config.exclusions.file_patterns;
const dirPatterns = config.exclusions.directory_patterns;
for (const filePattern of filePatterns) {
for (const dirPattern of dirPatterns) {
if (filePattern.includes(dirPattern.replace('/', ''))) {
result.warnings.push(`Potential redundant exclusion: file pattern '${filePattern}' may be covered by directory pattern '${dirPattern}'`);
}
}
}
}
// Validate notification settings
if (config.notification_settings?.slack_webhook &&
!config.notification_settings.slack_webhook.startsWith('https://hooks.slack.com/')) {
result.errors.push('Invalid Slack webhook URL format');
}
if (config.notification_settings?.discord_webhook &&
!config.notification_settings.discord_webhook.startsWith('https://discord.com/api/webhooks/')) {
result.errors.push('Invalid Discord webhook URL format');
}
}
/**
* Find all configuration files to validate
*/
private findConfigFiles(): string[] {
const files: string[] = [];
for (const configPath of this.configPaths) {
if (!existsSync(configPath)) {
console.warn(`⚠️ Configuration path not found: ${configPath}`);
continue;
}
try {
const stat = Bun.file(configPath);
if (stat.size === undefined) {
// It's a directory
const dirFiles = readdirSync(configPath);
for (const file of dirFiles) {
if (extname(file) === '.json') {
files.push(join(configPath, file));
}
}
} else {
// It's a file
if (extname(configPath) === '.json') {
files.push(configPath);
}
}
} catch (error: any) {
console.warn(`⚠️ Error reading path ${configPath}: ${error.message}`);
}
}
return files;
}
/**
* Generate summary statistics
*/
private generateSummary(results: ValidationResult[]): ValidationReport['summary'] {
const commonErrors: Record<string, number> = {};
const commonWarnings: Record<string, number> = {};
for (const result of results) {
for (const error of result.errors) {
commonErrors[error] = (commonErrors[error] || 0) + 1;
}
for (const warning of result.warnings) {
commonWarnings[warning] = (commonWarnings[warning] || 0) + 1;
}
}
return {
commonErrors,
commonWarnings,
};
}
/**
* Run the validation process
*/
async run(): Promise<ValidationReport> {
console.log(`🚀 Starting configuration validation`);
console.log(`📄 Schema: ${this.schemaPath}`);
const configFiles = this.findConfigFiles();
console.log(`📊 Configuration files found: ${configFiles.length}`);
if (configFiles.length === 0) {
throw new Error('No configuration files found to validate');
}
const results: ValidationResult[] = [];
let validFiles = 0;
let invalidFiles = 0;
for (const file of configFiles) {
console.log(`🔍 Validating: ${file}`);
const result = this.validateConfig(file);
results.push(result);
if (result.valid) {
validFiles++;
if (result.warnings.length > 0) {
console.log(` ⚠️ Valid with ${result.warnings.length} warnings`);
} else {
console.log(` ✅ Valid`);
}
} else {
invalidFiles++;
console.log(` ❌ Invalid (${result.errors.length} errors)`);
}
}
const report: ValidationReport = {
timestamp: new Date(),
schemaVersion: this.schema.title || 'Unknown',
totalFiles: configFiles.length,
validFiles,
invalidFiles,
results,
summary: this.generateSummary(results),
};
// Print detailed results
console.log(`\n📈 Validation Summary:`);
console.log(` ✅ Valid files: ${validFiles}`);
console.log(` ❌ Invalid files: ${invalidFiles}`);
console.log(` 📊 Success rate: ${((validFiles / configFiles.length) * 100).toFixed(1)}%`);
// Show common issues
if (Object.keys(report.summary.commonErrors).length > 0) {
console.log(`\n❌ Common Errors:`);
Object.entries(report.summary.commonErrors)
.sort(([,a], [,b]) => b - a)
.slice(0, 5)
.forEach(([error, count]) => {
console.log(` • ${error} (${count} files)`);
});
}
if (Object.keys(report.summary.commonWarnings).length > 0) {
console.log(`\n⚠️ Common Warnings:`);
Object.entries(report.summary.commonWarnings)
.sort(([,a], [,b]) => b - a)
.slice(0, 5)
.forEach(([warning, count]) => {
console.log(` • ${warning} (${count} files)`);
});
}
// Show detailed errors for invalid files
const invalidResults = results.filter(r => !r.valid);
if (invalidResults.length > 0) {
console.log(`\n🔍 Detailed Errors:`);
for (const result of invalidResults) {
console.log(`\n📁 ${result.file}:`);
for (const error of result.errors) {
console.log(` ❌ ${error}`);
}
}
}
return report;
}
}
// CLI usage
async function main() {
const schemaPath = process.argv[2] || './configs/schema.json';
const configPaths = process.argv.slice(3);
if (configPaths.length === 0) {
configPaths.push('./configs/profiles/', './configs/defaults.json');
}
try {
const validator = new ConfigValidator(schemaPath, configPaths);
const report = await validator.run();
// Save report
const reportPath = './validation-report.json';
Bun.write(reportPath, JSON.stringify(report, null, 2));
console.log(`\n📄 Validation report saved: ${reportPath}`);
// Exit with error code if validation failed
if (report.invalidFiles > 0) {
process.exit(1);
}
} catch (error) {
console.error("❌ Configuration validation failed:", error);
process.exit(1);
}
}
if (import.meta.main) {
main().catch(console.error);
}
export { ConfigValidator };