1. /**
  2. * Validates configuration for binary files for short-lived tasks with a dry-run mode.
  3. *
  4. * @param {object} config - Configuration object.
  5. * @param {boolean} dryRun - Whether to perform a dry run. Defaults to false.
  6. * @returns {object} - Validation results.
  7. */
  8. function validateBinaryConfig(config, dryRun = false) {
  9. const validationResults = {
  10. errors: [],
  11. warnings: [],
  12. };
  13. // Validate required fields
  14. if (!config.inputFile || !config.outputFile || !config.task) {
  15. validationResults.errors.push("Missing required configuration fields (inputFile, outputFile, task).");
  16. return validationResults;
  17. }
  18. // Validate input file
  19. if (!/^[a-zA-Z0-9._-]+$/.test(config.inputFile)) {
  20. validationResults.errors.push("Invalid input file name. Only alphanumeric characters, dots, underscores, and hyphens are allowed.");
  21. }
  22. // Validate output file
  23. if (!/^[a-zA-Z0-9._-]+$/.test(config.outputFile)) {
  24. validationResults.errors.push("Invalid output file name. Only alphanumeric characters, dots, underscores, and hyphens are allowed.");
  25. }
  26. // Validate task
  27. if (typeof config.task !== 'function') {
  28. validationResults.errors.push("Task must be a function.");
  29. }
  30. // Validate task arguments
  31. if (config.task.length > 3) {
  32. validationResults.warnings.push("Task accepts more than 3 arguments. Consider refactoring.");
  33. }
  34. // Validate file size limits (example)
  35. if (config.maxFileSize && config.inputFile.length > config.maxFileSize) {
  36. validationResults.warnings.push(`Input file exceeds maximum size of ${config.maxFileSize} bytes.`);
  37. }
  38. // Dry-run mode
  39. if (dryRun) {
  40. validationResults.message = "Dry run mode enabled. No actual file operations performed.";
  41. }
  42. return validationResults;
  43. }

Add your comment