/**
* Extracts configuration values for hypothesis validation with defensive checks.
* @returns {object} An object containing the extracted configuration values.
* @throws {Error} If a required configuration value is missing or invalid.
*/
function extractConfiguration() {
const config = {};
// Default values with validation
const defaultValues = {
threshold: 0.05,
tolerance: 0.01,
maxIterations: 100,
learningRate: 0.01,
modelType: "linear", //Example of a string type
};
// Get configuration values from environment variables or other sources
config.threshold = parseFloat(process.env.THRESHOLD) || defaultValues.threshold;
if (isNaN(config.threshold)) {
throw new Error("Invalid threshold value. Must be a number.");
}
config.tolerance = parseFloat(process.env.TOLERANCE) || defaultValues.tolerance;
if (isNaN(config.tolerance)) {
throw new Error("Invalid tolerance value. Must be a number.");
}
config.maxIterations = parseInt(process.env.MAX_ITERATIONS) || defaultValues.maxIterations;
if (isNaN(config.maxIterations) || config.maxIterations <= 0) {
throw new Error("Invalid maxIterations value. Must be a positive integer.");
}
config.learningRate = parseFloat(process.env.LEARNING_RATE) || defaultValues.learningRate;
if (isNaN(config.learningRate) || config.learningRate <= 0) {
throw new Error("Invalid learningRate value. Must be a positive number.");
}
config.modelType = process.env.MODEL_TYPE || defaultValues.modelType;
if (!Object.values(defaultValues.modelType).includes(config.modelType)) {
throw new Error("Invalid modelType value. Must be one of: " + Object.values(defaultValues.modelType).join(", "));
}
return config;
}
Add your comment