(since: string | undefined, until: string | undefined)
| 108 | * @returns Error message if invalid, null if valid |
| 109 | */ |
| 110 | export function validateDateRange(since: string | undefined, until: string | undefined): string | null { |
| 111 | if (!since || !until) { |
| 112 | return null; // No validation needed if either is missing |
| 113 | } |
| 114 | |
| 115 | const parsedSince = parseTemporalDate(since); |
| 116 | const parsedUntil = parseTemporalDate(until); |
| 117 | |
| 118 | if (!parsedSince || !parsedUntil) { |
| 119 | return null; // Let individual date parsing handle invalid formats |
| 120 | } |
| 121 | |
| 122 | const sinceDate = new Date(parsedSince); |
| 123 | const untilDate = new Date(parsedUntil); |
| 124 | |
| 125 | if (isNaN(sinceDate.getTime()) || isNaN(untilDate.getTime())) { |
| 126 | return null; |
| 127 | } |
| 128 | |
| 129 | if (sinceDate > untilDate) { |
| 130 | return `Invalid date range: 'since' (${since}) must be before 'until' (${until})`; |
| 131 | } |
| 132 | |
| 133 | return null; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Convert a date to a format suitable for Prisma database queries. |
no test coverage detected