1. /**
  2. * Validates the configuration of log streams for exploratory work.
  3. *
  4. * @param {object} config - The configuration object containing log stream settings.
  5. * @returns {object} - An object containing validation errors, or an empty object if valid.
  6. */
  7. function validateLogStreamConfig(config) {
  8. const errors = {};
  9. // Check if config is provided
  10. if (!config) {
  11. errors.general = "Configuration object is required.";
  12. return errors;
  13. }
  14. // Check if 'streams' property exists and is an array
  15. if (!config.streams || !Array.isArray(config.streams)) {
  16. errors.general = "The 'streams' property must be an array.";
  17. return errors;
  18. }
  19. // Iterate through each log stream configuration
  20. for (let i = 0; i < config.streams.length; i++) {
  21. const stream = config.streams[i];
  22. // Check if stream is an object
  23. if (typeof stream !== 'object' || stream === null) {
  24. errors[`stream_${i}`] = `Stream at index ${i} must be an object.`;
  25. continue; // Skip to the next stream
  26. }
  27. // Check if 'name' property exists and is a string
  28. if (!stream.name || typeof stream.name !== 'string') {
  29. errors[`stream_${i}_name`] = `Stream name at index ${i} must be a string.`;
  30. }
  31. // Check if 'level' property exists and is a valid log level
  32. if (!stream.level || !['error', 'warn', 'info', 'debug', 'trace'].includes(stream.level)) {
  33. errors[`stream_${i}_level`] = `Stream level at index ${i} must be one of: error, warn, info, debug, trace.`;
  34. }
  35. // Check if 'format' property exists and is a string (optional)
  36. if (stream.format && typeof stream.format !== 'string') {
  37. errors[`stream_${i}_format`] = `Stream format at index ${i} must be a string.`;
  38. }
  39. // Check if 'destination' property exists (required)
  40. if (!stream.destination || typeof stream.destination !== 'string') {
  41. errors[`stream_${i}_destination`] = `Stream destination at index ${i} must be a string.`;
  42. }
  43. }
  44. return errors; // Return the errors object
  45. }

Add your comment