From 82153bdc0cc6d4ee06eeed9617e4f71b7659bd2e Mon Sep 17 00:00:00 2001 From: Johan Ohly Date: Thu, 14 Nov 2024 22:24:16 +0100 Subject: [PATCH 1/6] chore: clearer oauth error message --- src/lib/server/utils/oauth.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/server/utils/oauth.ts b/src/lib/server/utils/oauth.ts index 228a9ea..fdd5e73 100644 --- a/src/lib/server/utils/oauth.ts +++ b/src/lib/server/utils/oauth.ts @@ -54,7 +54,7 @@ export const getOAuthClient = async () => { const { enabled, clientId, clientSecret, issuerUrl } = config.oauth; if (!enabled || !clientId || !clientSecret || !issuerUrl) { - throw new Error('OAuth is not enabled'); + throw new Error('OAuth is not enabled or configured properly'); } try { From c26253ff8bc8d5f62e8c043802e157668f7715ce Mon Sep 17 00:00:00 2001 From: Johan Ohly Date: Thu, 14 Nov 2024 22:55:06 +0100 Subject: [PATCH 2/6] chore: remove empty alternatives from regex patterns --- src/lib/import/jetlog.ts | 30 +++++++++++++++------------- src/lib/zod/flight.ts | 42 ++++++++++++++++++---------------------- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/src/lib/import/jetlog.ts b/src/lib/import/jetlog.ts index 4b01529..6f20fa4 100644 --- a/src/lib/import/jetlog.ts +++ b/src/lib/import/jetlog.ts @@ -19,23 +19,27 @@ const JETLOG_FLIGHT_CLASS_MAP: Record = { }; const nullTransformer = (v: string) => (v === '' ? null : v); +const dateRegex = /^\d{4}-\d{2}-\d{2}$/; +const optionalTimePrimitive = z + .string() + .refine((v) => v === '' || v.match(/^\d{2}:\d{2}$/), { + message: 'Invalid time format', + }) + .transform(nullTransformer); +const optionalDatePrimitive = z + .string() + .refine((v) => v === '' || v.match(dateRegex), { + message: 'Invalid date format', + }) + .transform(nullTransformer); const JetLogFlight = z.object({ - date: z.string().regex(/^\d{4}-\d{2}-\d{2}$/), + date: z.string().regex(dateRegex), origin: z.string(), destination: z.string(), - departure_time: z - .string() - .regex(/^\d{2}:\d{2}$|/) - .transform(nullTransformer), - arrival_time: z - .string() - .regex(/^\d{2}:\d{2}$|/) - .transform(nullTransformer), - arrival_date: z - .string() - .regex(/^\d{4}-\d{2}-\d{2}$|/) - .transform(nullTransformer), + departure_time: optionalTimePrimitive, + arrival_time: optionalTimePrimitive, + arrival_date: optionalDatePrimitive, seat: z.enum(['window', 'middle', 'aisle', '']).transform(nullTransformer), ticket_class: z.string().transform(nullTransformer), duration: z.string().transform(nullTransformer), diff --git a/src/lib/zod/flight.ts b/src/lib/zod/flight.ts index ae3d267..66a8e57 100644 --- a/src/lib/zod/flight.ts +++ b/src/lib/zod/flight.ts @@ -2,12 +2,26 @@ 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 regex24h = - /^([01]?[0-9]|2[0-3])(?::|\.|)[0-5][0-9](?:\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; +const timePrimitive = z + .string() + .refine((value) => { + // Always allow empty string (to allow the user to delete the input) + if (value === '') return true; + + return regex24h.test(value); + }, 'Invalid 24-hour format') + .refine((value) => { + // Skip 12-hour check if empty or not possibly 12-hour format (caught by the previous refine) + if (value === '' || !regex12hLike.test(value)) return true; + + return regex12h.test(value); + }, 'Invalid 12-hour format') + .nullable(); + export const flightAirportsSchema = z.object({ from: z.string().min(1, 'Select an origin'), to: z.string().min(1, 'Select a destination'), @@ -19,30 +33,12 @@ export const flightDateTimeSchema = z.object({ .datetime({ offset: true, message: 'Select a departure date' }) .nullable() .refine((value) => value !== null, 'Select a departure date'), - departureTime: z - .string() - .refine((value) => regex24h.test(value), 'Invalid 24-hour format') - .refine((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(), + departureTime: timePrimitive, arrival: z .string() .datetime({ offset: true, message: 'Select an arrival date' }) .nullable(), - arrivalTime: z - .string() - .refine((value) => regex24h.test(value), 'Invalid 24-hour format') - .refine((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(), + arrivalTime: timePrimitive, }); export const flightSeatInformationSchema = z.object({ From a1ec8f1a5441800e897ab8bdcd94934714c57c2a Mon Sep 17 00:00:00 2001 From: Johan Ohly Date: Thu, 14 Nov 2024 23:04:39 +0100 Subject: [PATCH 3/6] chore: use concise character class syntax in regex --- src/lib/import/airtrail.ts | 2 +- src/lib/import/aita.ts | 2 +- src/lib/zod/flight.ts | 4 ++-- src/lib/zod/user.ts | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/lib/import/airtrail.ts b/src/lib/import/airtrail.ts index 160c19e..3704efe 100644 --- a/src/lib/import/airtrail.ts +++ b/src/lib/import/airtrail.ts @@ -36,7 +36,7 @@ const AirTrailFile = z.object({ .string() .min(3, { message: 'Username must be at least 3 characters long' }) .max(20, { message: 'Username must be at most 20 characters long' }) - .regex(/^[a-zA-Z0-9_]+$/, { + .regex(/^\w+$/, { message: 'Username can only contain letters, numbers, and underscores', }), diff --git a/src/lib/import/aita.ts b/src/lib/import/aita.ts index f049f28..2febb24 100644 --- a/src/lib/import/aita.ts +++ b/src/lib/import/aita.ts @@ -18,7 +18,7 @@ const AITA_SEAT_CLASS_MAP: Record = { export const processAITAFile = (input: string, options: PlatformOptions) => { const tripPattern = - /^Ownership\.([\w]+);[^\r\n]*\sflights:\s([\s\S]+?)\shotels:/gm; + /^Ownership\.(\w+);[^\r\n]*\sflights:\s([\s\S]+?)\shotels:/gm; const flightPattern = /^([^;\n\r]*);(\w*);.*?;(\w{2,4};\d{2,4});(\w*);(\w{3});(\w{3});([\d\-T:]+);([\d\-T:]+);([\d\-T:]+);([\d\-T:]+);(.*)/gm; diff --git a/src/lib/zod/flight.ts b/src/lib/zod/flight.ts index 66a8e57..70f5f8d 100644 --- a/src/lib/zod/flight.ts +++ b/src/lib/zod/flight.ts @@ -2,9 +2,9 @@ import { z } from 'zod'; import { FlightReasons, SeatClasses, SeatTypes } from '$lib/db/types'; -const regex24h = /^([01]?[0-9]|2[0-3])(?::|\.|)[0-5][0-9](?:\s?(?:am|pm))?$/i; +const regex24h = /^([01]?\d|2[0-3])(?::|\.|)[0-5]\d(?:\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; +const regex12h = /^([1-9]|1[0-2])(?::|\.|)[0-5]\d\s?(?:am|pm)$/i; const timePrimitive = z .string() diff --git a/src/lib/zod/user.ts b/src/lib/zod/user.ts index 3178262..4ce146e 100644 --- a/src/lib/zod/user.ts +++ b/src/lib/zod/user.ts @@ -5,7 +5,7 @@ export const userSchema = z.object({ .string() .min(3, { message: 'Username must be at least 3 characters long' }) .max(20, { message: 'Username must be at most 20 characters long' }) - .regex(/^[a-zA-Z0-9_]+$/, { + .regex(/^\w+$/, { message: 'Username can only contain letters, numbers, and underscores', }), password: z.string().min(8), From 9f83421dd8bad8628d88fd88a9efdf2d8d38f8ec Mon Sep 17 00:00:00 2001 From: Johan Ohly Date: Thu, 14 Nov 2024 23:12:45 +0100 Subject: [PATCH 4/6] chore: remove unused import --- src/routes/api/flight/save/+server.ts | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/routes/api/flight/save/+server.ts b/src/routes/api/flight/save/+server.ts index ee09656..d531839 100644 --- a/src/routes/api/flight/save/+server.ts +++ b/src/routes/api/flight/save/+server.ts @@ -1,11 +1,5 @@ import type { TZDate } from '@date-fns/tz'; -import { - differenceInSeconds, - format, - formatISO, - isBefore, - isValid, -} from 'date-fns'; +import { differenceInSeconds, format, formatISO, isBefore } from 'date-fns'; import { actionResult, setError, superValidate } from 'sveltekit-superforms'; import { zod } from 'sveltekit-superforms/adapters'; From 917d393012fe2be7d5c77f393befab4a731f6a33 Mon Sep 17 00:00:00 2001 From: Johan Ohly Date: Thu, 14 Nov 2024 23:27:20 +0100 Subject: [PATCH 5/6] chore: refactor formatSeat function --- src/lib/utils/data/data.ts | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/lib/utils/data/data.ts b/src/lib/utils/data/data.ts index a0ca2b6..750209a 100644 --- a/src/lib/utils/data/data.ts +++ b/src/lib/utils/data/data.ts @@ -217,15 +217,20 @@ export const formatSeat = (f: FlightData) => { const s = f.seats.find((seat) => seat.userId === userId); if (!s) return null; - return s.seat && s.seatNumber && s.seatClass - ? `${t(s.seatClass)} (${s.seat} ${s.seatNumber})` - : s.seat && s.seatNumber - ? `${s.seat} ${s.seatNumber}` - : s.seat && s.seatClass - ? `${t(s.seatClass)} (${s.seat})` - : s.seatClass - ? t(s.seatClass) - : s.seat - ? t(s.seat) - : null; -}; + if (s.seat && s.seatNumber && s.seatClass) { + return `${t(s.seatClass)} (${s.seat} ${s.seatNumber})`; + } + if (s.seat && s.seatNumber) { + return `${s.seat} ${s.seatNumber}`; + } + if (s.seat && s.seatClass) { + return `${t(s.seatClass)} (${s.seat})`; + } + if (s.seatClass) { + return t(s.seatClass); + } + if (s.seat) { + return t(s.seat); + } + return null; +}; \ No newline at end of file From 9ffbc7a911f2df8492f998a1bc923a76024ef66a Mon Sep 17 00:00:00 2001 From: Johan Ohly Date: Thu, 14 Nov 2024 23:29:04 +0100 Subject: [PATCH 6/6] chore: format files --- src/lib/utils/data/data.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/utils/data/data.ts b/src/lib/utils/data/data.ts index 750209a..a07d2ec 100644 --- a/src/lib/utils/data/data.ts +++ b/src/lib/utils/data/data.ts @@ -233,4 +233,4 @@ export const formatSeat = (f: FlightData) => { return t(s.seat); } return null; -}; \ No newline at end of file +};