Skip to content

Commit

Permalink
validation(refactor): remove functions and add new issue
Browse files Browse the repository at this point in the history
  • Loading branch information
aaschlote committed Dec 23, 2024
1 parent a4900ea commit 548c16a
Showing 1 changed file with 8 additions and 47 deletions.
55 changes: 8 additions & 47 deletions app/domains/fluggastrechte/formular/services/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,6 @@ function convertToTimestamp(date: string, time: string): number {
const [hours, minutes] = time.split(":").map(Number);
return new Date(year, month - 1, day, hours, minutes).getTime();
}
function convertToDate(date: string): number {
const [day, month, year] = date.split(".").map(Number);
return new Date(year, month - 1, day).getTime();
}

export function getArrivalTimeDelayValidator(
baseSchema: ValidationMultipleFieldsBaseSchema,
) {
return baseSchema.refine(
(data) => {
const arrivalTimestamp = convertToTimestamp(
data.tatsaechlicherAnkunftsDatum,
data.tatsaechlicherAnkunftsZeit,
);
const departureTimestamp = convertToTimestamp(
data.direktAbflugsDatum,
data.direktAbflugsZeit,
);

const minimumTimeDifferenceInMs = 3 * 60 * 60 * 1000; // 3 hours in milliseconds
const actualTimeDifferenceInMs = arrivalTimestamp - departureTimestamp;
return actualTimeDifferenceInMs >= minimumTimeDifferenceInMs;
},
{
message: "invalidTimeDelay",
path: ["tatsaechlicherAnkunftsZeit"],
},
);
}

export function getArrivalDateValidator(
baseSchema: ValidationMultipleFieldsBaseSchema,
) {
return baseSchema.refine(
(data) => {
const arrivalDate = convertToDate(data.tatsaechlicherAnkunftsDatum);
const departureDate = convertToDate(data.direktAbflugsDatum);

const actualTimeDifferenceInMs = arrivalDate - departureDate;
return actualTimeDifferenceInMs >= 0;
},
{
message: "invalidDate",
path: ["tatsaechlicherAnkunftsDatum"],
},
);
}

export function validateDepartureAfterArrival(
baseSchema: ValidationMultipleFieldsBaseSchema,
Expand All @@ -70,6 +23,14 @@ export function validateDepartureAfterArrival(
);

if (departureDateTime > arrivalDateTime) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "too_late",
path: ["direktAnkunftsZeit"],
fatal: true,
});

// add new issue to invalidate this field as well
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "too_late",
Expand Down

0 comments on commit 548c16a

Please sign in to comment.