Skip to content

Commit

Permalink
fix: allow more time seperators
Browse files Browse the repository at this point in the history
* for example "10.40 PM" and "1040 PM" are allowed
  • Loading branch information
johanohly committed Sep 17, 2024
1 parent 7185cc1 commit d888419
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
18 changes: 10 additions & 8 deletions src/lib/zod/flight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { z } from 'zod';
import { FlightReasons, SeatClasses, SeatTypes } from '$lib/db/types';

// |^$ is for empty string in the case where the user deletes the input
const timeRegex24 = /^([01]?[0-9]|2[0-3]):[0-5][0-9](?:\s?(?:am|pm))?$|^$/i;
const timeRegex12 = /^\d{1,2}:\d{2}\s?(?:am|pm)$/i;
const regex24h =
/^([01]?[0-9]|2[0-3])(?::|\.|)[0-5][0-9](?:\s?(?:am|pm))?$|^$/i;
const regex12hLike = /^\d{1,2}(?::|\.|)\d{2}\s?(?:am|pm)$/i;
const regex12h = /^([1-9]|1[0-2])(?::|\.|)[0-5][0-9]\s?(?:am|pm)$/i;

export const flightAirportsSchema = z.object({
from: z.string().min(1, 'Select an origin'),
Expand All @@ -18,21 +20,21 @@ export const flightDateTimeSchema = z.object({
.refine((value) => value !== null, 'Select a departure date'),
departureTime: z
.string()
.refine((value) => timeRegex24.test(value), 'Invalid 24-hour format')
.refine((value) => regex24h.test(value), 'Invalid 24-hour format')
.refine((value) => {
if (timeRegex12.test(value)) {
return /^([1-9]|1[0-2]):[0-5][0-9]\s?(?:am|pm)$/i.test(value);
if (regex12hLike.test(value)) {
return regex12h.test(value);
}
return true; // If it's not in 12-hour format, just return true (it'll be caught by the previous refine)
}, 'Invalid 12-hour format')
.nullable(),
arrival: z.string().datetime('Select an arrival date').nullable(),
arrivalTime: z
.string()
.refine((value) => timeRegex24.test(value), 'Invalid 24-hour format')
.refine((value) => regex24h.test(value), 'Invalid 24-hour format')
.refine((value) => {
if (timeRegex12.test(value)) {
return /^([1-9]|1[0-2]):[0-5][0-9]\s?(?:am|pm)$/i.test(value);
if (regex12hLike.test(value)) {
return regex12h.test(value);
}
return true; // If it's not in 12-hour format, just return true (it'll be caught by the previous refine)
}, 'Invalid 12-hour format')
Expand Down
2 changes: 1 addition & 1 deletion src/routes/api/flight/save/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export const POST: RequestHandler = async ({ locals, request }) => {
return actionResult('success', { form });
};

const timePartsRegex = /^(\d{1,2}):(\d{2})(?:\s?(am|pm))?$/i;
const timePartsRegex = /^(\d{1,2})(?::|\.|)(\d{2})(?:\s?(am|pm))?$/i;
const mergeTimeWithDate = (
dateOnly: dayjs.Dayjs,
timeString: string,
Expand Down

0 comments on commit d888419

Please sign in to comment.