/**
* Validates the configuration of log streams for exploratory work.
*
* @param {object} config - The configuration object containing log stream settings.
* @returns {object} - An object containing validation errors, or an empty object if valid.
*/
function validateLogStreamConfig(config) {
const errors = {};
// Check if config is provided
if (!config) {
errors.general = "Configuration object is required.";
return errors;
}
// Check if 'streams' property exists and is an array
if (!config.streams || !Array.isArray(config.streams)) {
errors.general = "The 'streams' property must be an array.";
return errors;
}
// Iterate through each log stream configuration
for (let i = 0; i < config.streams.length; i++) {
const stream = config.streams[i];
// Check if stream is an object
if (typeof stream !== 'object' || stream === null) {
errors[`stream_${i}`] = `Stream at index ${i} must be an object.`;
continue; // Skip to the next stream
}
// Check if 'name' property exists and is a string
if (!stream.name || typeof stream.name !== 'string') {
errors[`stream_${i}_name`] = `Stream name at index ${i} must be a string.`;
}
// Check if 'level' property exists and is a valid log level
if (!stream.level || !['error', 'warn', 'info', 'debug', 'trace'].includes(stream.level)) {
errors[`stream_${i}_level`] = `Stream level at index ${i} must be one of: error, warn, info, debug, trace.`;
}
// Check if 'format' property exists and is a string (optional)
if (stream.format && typeof stream.format !== 'string') {
errors[`stream_${i}_format`] = `Stream format at index ${i} must be a string.`;
}
// Check if 'destination' property exists (required)
if (!stream.destination || typeof stream.destination !== 'string') {
errors[`stream_${i}_destination`] = `Stream destination at index ${i} must be a string.`;
}
}
return errors; // Return the errors object
}
Add your comment