TanStackT
TanStack16mo ago
4 replies
slow-yellow

Search params start and end date validation

Hi there, im implementing search params for filters and i want to make sure the start date is before the end date. in zod i can do something like this
export const FiltersRequest = object({
  startDate: fallback(string().date(), '').default(''),
  endDate: fallback(string().date(), '').default(''),
}).refine((data) => {
  const startDate = parseDate(data.startDate)
  const endDate = parseDate(data.endDate)
  return startDate.compare(endDate) > 0
}, {
  path: ["startDate"],
  message: "Start date must be before end date",
});

But this doesn't work with the fallback since it would fall after the fallback.
Was this page helpful?