searchDateValidation.js 903 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import {
  2. isInvalidIsoDateRange,
  3. isValidIsoDateYmd,
  4. } from "@/lib/frontend/search/dateRange";
  5. /**
  6. * Zentralisierte Validation fuer Search-Datefilter (ISO YYYY-MM-DD).
  7. * Gibt ein kleines Fehlerobjekt zurueck (oder null), ohne UI-Abhaengigkeiten.
  8. *
  9. * @param {string|null} from
  10. * @param {string|null} to
  11. * @returns {{code: string, message: string, details?: any} | null}
  12. */
  13. export function getSearchDateRangeValidation(from, to) {
  14. if (from && !isValidIsoDateYmd(from)) {
  15. return {
  16. code: "VALIDATION_SEARCH_DATE",
  17. message: "Invalid from date",
  18. details: { from },
  19. };
  20. }
  21. if (to && !isValidIsoDateYmd(to)) {
  22. return {
  23. code: "VALIDATION_SEARCH_DATE",
  24. message: "Invalid to date",
  25. details: { to },
  26. };
  27. }
  28. if (isInvalidIsoDateRange(from, to)) {
  29. return {
  30. code: "VALIDATION_SEARCH_RANGE",
  31. message: "Invalid date range",
  32. details: { from, to },
  33. };
  34. }
  35. return null;
  36. }