searchDateValidation.js 970 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import {
  2. isInvalidIsoDateRange,
  3. isValidIsoDateYmd,
  4. } from "@/lib/frontend/search/dateRange";
  5. /**
  6. * Centralized validation for search date filters (ISO YYYY-MM-DD).
  7. *
  8. * Returns a small, UI-agnostic error descriptor (or null).
  9. *
  10. * Notes:
  11. * - from === to is valid (single-day search)
  12. * - from > to is invalid
  13. *
  14. * @param {string|null} from
  15. * @param {string|null} to
  16. * @returns {{code: string, message: string, details?: any} | null}
  17. */
  18. export function getSearchDateRangeValidation(from, to) {
  19. if (from && !isValidIsoDateYmd(from)) {
  20. return {
  21. code: "VALIDATION_SEARCH_DATE",
  22. message: "Invalid from date",
  23. details: { from },
  24. };
  25. }
  26. if (to && !isValidIsoDateYmd(to)) {
  27. return {
  28. code: "VALIDATION_SEARCH_DATE",
  29. message: "Invalid to date",
  30. details: { to },
  31. };
  32. }
  33. if (isInvalidIsoDateRange(from, to)) {
  34. return {
  35. code: "VALIDATION_SEARCH_RANGE",
  36. message: "Invalid date range",
  37. details: { from, to },
  38. };
  39. }
  40. return null;
  41. }