gather-ts
Version:
A powerful code analysis and packaging tool designed for creating AI-friendly code representations for javascript and typescript projects.
155 lines • 5.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigValidator = void 0;
const errors_1 = require("@/errors");
class ConfigValidator {
validateConfig(config) {
// Validate required fields
const requiredFields = ["tokenizer", "outputFormat"];
const missingFields = requiredFields.filter((field) => !(field in config));
if (missingFields.length > 0) {
throw new errors_1.ValidationError("Missing required configuration fields", {
missingFields,
});
}
// Validate individual sections
this.validateMaxDepth(config);
this.validateTokenizerConfig(config);
this.validateOutputFormat(config);
this.validateCustomText(config);
}
validateMaxDepth(config) {
if (config.maxDepth !== undefined &&
(typeof config.maxDepth !== "number" || config.maxDepth < 0)) {
throw new errors_1.ValidationError("Invalid maxDepth value", {
maxDepth: config.maxDepth,
expectedType: "positive number or undefined",
});
}
}
validateTokenizerConfig(config) {
const validModels = [
"gpt-3.5-turbo",
"gpt-4",
"gpt-4o",
"gpt-4o-mini",
"o1",
"o1-mini",
"o3-mini",
];
if (!config.tokenizer) {
throw new errors_1.ValidationError("Missing tokenizer configuration");
}
if (!validModels.includes(config.tokenizer.model)) {
throw new errors_1.ValidationError("Invalid tokenizer model", {
providedModel: config.tokenizer.model,
validModels,
});
}
if (config.tokenizer.showWarning !== undefined &&
typeof config.tokenizer.showWarning !== "boolean") {
throw new errors_1.ValidationError("Invalid showWarning value in tokenizer config", {
providedValue: config.tokenizer.showWarning,
expectedType: "boolean",
});
}
}
validateOutputFormat(config) {
if (!config.outputFormat) {
throw new errors_1.ValidationError("Missing output format configuration");
}
const formatKeys = [
"includeSummaryInFile",
"includeGenerationTime",
"includeUsageGuidelines",
];
formatKeys.forEach((key) => {
if (config.outputFormat?.[key] !== undefined &&
typeof config.outputFormat[key] !== "boolean") {
throw new errors_1.ValidationError(`Invalid outputFormat.${key} value`, {
value: config.outputFormat[key],
expectedType: "boolean",
});
}
});
if (config.outputFormat.format !== undefined &&
!["json", "text", "markdown"].includes(config.outputFormat.format)) {
throw new errors_1.ValidationError("Invalid output format", {
providedValue: config.outputFormat.format,
allowedValues: ["json", "text", "markdown"],
});
}
}
validateCustomText(config) {
if (!config.customText) {
return;
}
const textKeys = [
"header",
"footer",
"beforeSummary",
"afterSummary",
"beforeFiles",
];
textKeys.forEach((key) => {
if (config.customText[key] !== undefined &&
typeof config.customText[key] !== "string") {
throw new errors_1.ValidationError(`Invalid customText.${key} value`, {
value: config.customText[key],
expectedType: "string",
});
}
});
}
validateRequiredFiles(config) {
if (!config.requiredFiles) {
return;
}
if (!Array.isArray(config.requiredFiles)) {
throw new errors_1.ValidationError("requiredFiles must be an array", {
providedValue: config.requiredFiles,
expectedType: "array",
});
}
config.requiredFiles.forEach((file, index) => {
if (typeof file !== "string") {
throw new errors_1.ValidationError(`Invalid required file at index ${index}`, {
providedValue: file,
expectedType: "string",
});
}
});
}
validateAll(config) {
const result = {
isValid: true,
errors: [],
warnings: [],
};
try {
this.validateConfig(config);
}
catch (error) {
result.isValid = false;
if (error instanceof errors_1.ValidationError) {
result.errors.push(error.message);
if (error.details) {
result.errors.push(JSON.stringify(error.details));
}
}
else {
result.errors.push(error instanceof Error ? error.message : String(error));
}
}
// Add warnings for potentially problematic configurations
if (config.maxDepth !== undefined && config.maxDepth > 10) {
result.warnings.push("High maxDepth value may impact performance");
}
if (config.topFilesCount && config.topFilesCount > 20) {
result.warnings.push("Large topFilesCount value may impact readability");
}
return result;
}
}
exports.ConfigValidator = ConfigValidator;
//# sourceMappingURL=ConfigValidator.js.map