From cc074e639002600c8dbdf73a0f350c4c9a0bdbc8 Mon Sep 17 00:00:00 2001 From: Pram Gurusinga Date: Thu, 9 Jan 2025 17:46:33 +0100 Subject: [PATCH] feat: add validation for same airports (#1591) --- .../vorabcheck/multiFieldsValidation.ts | 7 +++++ .../services/__test__/validation.test.ts | 31 +++++++++++++++++++ .../vorabcheck/services/validation.ts | 21 +++++++++++++ app/domains/multiFieldsFlowValidation.ts | 3 +- 4 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 app/domains/fluggastrechte/vorabcheck/multiFieldsValidation.ts create mode 100644 app/domains/fluggastrechte/vorabcheck/services/__test__/validation.test.ts create mode 100644 app/domains/fluggastrechte/vorabcheck/services/validation.ts diff --git a/app/domains/fluggastrechte/vorabcheck/multiFieldsValidation.ts b/app/domains/fluggastrechte/vorabcheck/multiFieldsValidation.ts new file mode 100644 index 000000000..428b1840c --- /dev/null +++ b/app/domains/fluggastrechte/vorabcheck/multiFieldsValidation.ts @@ -0,0 +1,7 @@ +import type { MultiFieldsStepIdValidation } from "~/domains/multiFieldsFlowValidation"; +import { validateSameDepartureAndArrivalAirports } from "./services/validation"; + +export const fluggastrechtVorabcheckMultiFieldsValidation: MultiFieldsStepIdValidation = + { + "/flughaefen": validateSameDepartureAndArrivalAirports, + }; diff --git a/app/domains/fluggastrechte/vorabcheck/services/__test__/validation.test.ts b/app/domains/fluggastrechte/vorabcheck/services/__test__/validation.test.ts new file mode 100644 index 000000000..ab2589aa5 --- /dev/null +++ b/app/domains/fluggastrechte/vorabcheck/services/__test__/validation.test.ts @@ -0,0 +1,31 @@ +import { z } from "zod"; +import { validateSameDepartureAndArrivalAirports } from "../validation"; + +describe("Vorabcheck - Multi Fields Validation", () => { + describe("validateSameDepartureAndArrivalAirports", () => { + const baseSchema = z.object({ + startAirport: z.string(), + endAirport: z.string(), + }); + + const validator = validateSameDepartureAndArrivalAirports(baseSchema); + + it("should return success false when startAirport and endAirport are the same", () => { + const result = validator.safeParse({ + startAirport: "JFK", + endAirport: "JFK", + }); + + expect(result.success).toBe(false); + }); + + it("should return success true when startAirport and endAirport are different", () => { + const result = validator.safeParse({ + startAirport: "JFK", + endAirport: "LAX", + }); + + expect(result.success).toBe(true); + }); + }); +}); diff --git a/app/domains/fluggastrechte/vorabcheck/services/validation.ts b/app/domains/fluggastrechte/vorabcheck/services/validation.ts new file mode 100644 index 000000000..6ec2081d9 --- /dev/null +++ b/app/domains/fluggastrechte/vorabcheck/services/validation.ts @@ -0,0 +1,21 @@ +import { z } from "zod"; +import type { MultiFieldsValidationBaseSchema } from "~/domains/multiFieldsFlowValidation"; + +export function validateSameDepartureAndArrivalAirports( + baseSchema: MultiFieldsValidationBaseSchema, +) { + const fieldsToValidate = ["startAirport", "endAirport"]; + + return baseSchema.superRefine((data, ctx) => { + if (data.startAirport === data.endAirport) { + fieldsToValidate.forEach((field) => + ctx.addIssue({ + code: z.ZodIssueCode.custom, + message: "sameDepartureAndArrivalAirports", + path: [field], + fatal: true, + }), + ); + } + }); +} diff --git a/app/domains/multiFieldsFlowValidation.ts b/app/domains/multiFieldsFlowValidation.ts index 382c724a4..97c36a91b 100644 --- a/app/domains/multiFieldsFlowValidation.ts +++ b/app/domains/multiFieldsFlowValidation.ts @@ -1,6 +1,7 @@ import type { z } from "zod"; import type { FlowId } from "./flowIds"; import { fluggastrechtMultiFieldsValidation } from "./fluggastrechte/formular/multiFieldsValidation"; +import { fluggastrechtVorabcheckMultiFieldsValidation } from "./fluggastrechte/vorabcheck/multiFieldsValidation"; export type MultiFieldsValidationBaseSchema = z.ZodObject< Record @@ -20,7 +21,7 @@ const multiFieldsFlowValidation = { "/beratungshilfe/vorabcheck": undefined, "/geld-einklagen/vorabcheck": undefined, "/geld-einklagen/formular": undefined, - "/fluggastrechte/vorabcheck": undefined, + "/fluggastrechte/vorabcheck": fluggastrechtVorabcheckMultiFieldsValidation, "/fluggastrechte/formular": fluggastrechtMultiFieldsValidation, "/prozesskostenhilfe/formular": undefined, } as const satisfies Record;