1. function validateConfig(config) {
  2. // Check if config is an object
  3. if (typeof config !== 'object' || config === null) {
  4. return { error: 'Config must be an object.' };
  5. }
  6. // Validate required properties
  7. if (!config.appName) {
  8. return { error: 'appName is required.' };
  9. }
  10. if (!config.port) {
  11. return { error: 'port is required.' };
  12. }
  13. if (typeof config.port !== 'number' || isNaN(config.port)) {
  14. return { error: 'port must be a number.' };
  15. }
  16. if (!config.debug) {
  17. return { error: 'debug is required.' };
  18. }
  19. if (typeof config.debug !== 'boolean') {
  20. return { error: 'debug must be a boolean.' };
  21. }
  22. // Validate port range
  23. if (config.port < 1024 || config.port > 65535) {
  24. return { error: 'port must be between 1024 and 65535.' };
  25. }
  26. // Validate appName length
  27. if (typeof config.appName === 'string' && config.appName.length < 3) {
  28. return { error: 'appName must be at least 3 characters long.'};
  29. }
  30. //Validate config.timeout
  31. if (config.timeout && typeof config.timeout !== 'number' || isNaN(config.timeout) || config.timeout <= 0) {
  32. return { error: 'timeout must be a positive number.'};
  33. }
  34. // If all validations pass
  35. return { success: true };
  36. }

Add your comment