1. /**
  2. * Extracts configuration values for hypothesis validation with defensive checks.
  3. * @returns {object} An object containing the extracted configuration values.
  4. * @throws {Error} If a required configuration value is missing or invalid.
  5. */
  6. function extractConfiguration() {
  7. const config = {};
  8. // Default values with validation
  9. const defaultValues = {
  10. threshold: 0.05,
  11. tolerance: 0.01,
  12. maxIterations: 100,
  13. learningRate: 0.01,
  14. modelType: "linear", //Example of a string type
  15. };
  16. // Get configuration values from environment variables or other sources
  17. config.threshold = parseFloat(process.env.THRESHOLD) || defaultValues.threshold;
  18. if (isNaN(config.threshold)) {
  19. throw new Error("Invalid threshold value. Must be a number.");
  20. }
  21. config.tolerance = parseFloat(process.env.TOLERANCE) || defaultValues.tolerance;
  22. if (isNaN(config.tolerance)) {
  23. throw new Error("Invalid tolerance value. Must be a number.");
  24. }
  25. config.maxIterations = parseInt(process.env.MAX_ITERATIONS) || defaultValues.maxIterations;
  26. if (isNaN(config.maxIterations) || config.maxIterations <= 0) {
  27. throw new Error("Invalid maxIterations value. Must be a positive integer.");
  28. }
  29. config.learningRate = parseFloat(process.env.LEARNING_RATE) || defaultValues.learningRate;
  30. if (isNaN(config.learningRate) || config.learningRate <= 0) {
  31. throw new Error("Invalid learningRate value. Must be a positive number.");
  32. }
  33. config.modelType = process.env.MODEL_TYPE || defaultValues.modelType;
  34. if (!Object.values(defaultValues.modelType).includes(config.modelType)) {
  35. throw new Error("Invalid modelType value. Must be one of: " + Object.values(defaultValues.modelType).join(", "));
  36. }
  37. return config;
  38. }

Add your comment