1. /**
  2. * Validates cookie configuration for scheduled runs with fallback logic.
  3. *
  4. * @param {object} config - Configuration object containing cookie settings.
  5. * @param {string} config.cookieName - Name of the cookie.
  6. * @param {string} config.cookieDomain - Domain for the cookie.
  7. * @param {string} config.cookiePath - Path for the cookie.
  8. * @param {number} config.cookieExpires - Cookie expiration time in seconds (optional).
  9. * @param {boolean} config.useFallback - Whether to use fallback logic if cookies are invalid (optional).
  10. * @param {function} config.fallbackFunction - Function to execute if cookies are invalid (optional).
  11. * @returns {boolean} - True if cookie configuration is valid, false otherwise.
  12. */
  13. function validateCookieConfig(config) {
  14. if (!config || typeof config !== 'object') {
  15. console.error("Invalid configuration object.");
  16. return false;
  17. }
  18. const cookieName = config.cookieName;
  19. const cookieDomain = config.cookieDomain;
  20. const cookiePath = config.cookiePath;
  21. const cookieExpires = config.cookieExpires;
  22. const useFallback = config.useFallback || false;
  23. const fallbackFunction = config.fallbackFunction || (() => { console.warn("Cookie configuration invalid, no fallback defined."); });
  24. if (typeof cookieName !== 'string' || cookieName.trim() === '') {
  25. console.error("Cookie name must be a non-empty string.");
  26. return false;
  27. }
  28. if (typeof cookieDomain !== 'string' || cookieDomain.trim() === '') {
  29. console.error("Cookie domain must be a non-empty string.");
  30. return false;
  31. }
  32. if (typeof cookiePath !== 'string' || cookiePath.trim() === '') {
  33. console.error("Cookie path must be a non-empty string.");
  34. return false;
  35. }
  36. if (cookieExpires !== undefined && typeof cookieExpires !== 'number' || cookieExpires < 0) {
  37. console.error("Cookie expires must be a non-negative number.");
  38. return false;
  39. }
  40. // In a real application, you'd check if the cookies exist and are valid.
  41. // This is a placeholder. For demonstration, we assume the config is valid if
  42. // all the above checks pass.
  43. if (useFallback) {
  44. //Placeholder for cookie validation logic.
  45. //In a real implementation, this would check for the existence and validity of the cookie.
  46. try {
  47. //Simulate cookie validation
  48. if(cookieName === "testCookie") {
  49. console.log("Cookies are valid");
  50. return true;
  51. } else {
  52. console.warn("Cookies are invalid, using fallback.");
  53. fallbackFunction();
  54. return false;
  55. }
  56. } catch (error) {
  57. console.error("Error during cookie validation:", error);
  58. fallbackFunction();
  59. return false;
  60. }
  61. }
  62. return true;
  63. }

Add your comment