function validateConfig(config) {
// Check if config is an object
if (typeof config !== 'object' || config === null) {
return { error: 'Config must be an object.' };
}
// Validate required properties
if (!config.appName) {
return { error: 'appName is required.' };
}
if (!config.port) {
return { error: 'port is required.' };
}
if (typeof config.port !== 'number' || isNaN(config.port)) {
return { error: 'port must be a number.' };
}
if (!config.debug) {
return { error: 'debug is required.' };
}
if (typeof config.debug !== 'boolean') {
return { error: 'debug must be a boolean.' };
}
// Validate port range
if (config.port < 1024 || config.port > 65535) {
return { error: 'port must be between 1024 and 65535.' };
}
// Validate appName length
if (typeof config.appName === 'string' && config.appName.length < 3) {
return { error: 'appName must be at least 3 characters long.'};
}
//Validate config.timeout
if (config.timeout && typeof config.timeout !== 'number' || isNaN(config.timeout) || config.timeout <= 0) {
return { error: 'timeout must be a positive number.'};
}
// If all validations pass
return { success: true };
}
Add your comment