Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate multiple fields #1562

Merged
merged 27 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
c233e1f
refactor: fixing typos
aaschlote Dec 23, 2024
dd99627
feat: add option to validate multiple fields
aaschlote Dec 23, 2024
532b530
refactor: move validatorForFieldNames to new path
aaschlote Dec 23, 2024
a4900ea
refactor: move buildStepValidator to new path and add option to valid…
aaschlote Dec 23, 2024
548c16a
validation(refactor): remove functions and add new issue
aaschlote Dec 23, 2024
9e1669f
Merge branch 'main' into validate-multiple-fields
aaschlote Dec 23, 2024
ea7691c
test(getValidationMultipleFieldsByPathname): add unit test
aaschlote Dec 23, 2024
57b10dd
test(buildStepValidator): add unit test for multiple fields validation
aaschlote Dec 23, 2024
45e56ae
refactor: change error message code
aaschlote Dec 23, 2024
c657011
Merge branch 'main' into validate-multiple-fields
aaschlote Dec 23, 2024
d459bb7
Merge branch 'main' into validate-multiple-fields
aaschlote Dec 23, 2024
81290ad
Merge branch 'main' into validate-multiple-fields
aaschlote Dec 30, 2024
84034e1
feat: add validations for other pages
aaschlote Dec 30, 2024
0ca9ced
Merge branch 'main' into validate-multiple-fields
aaschlote Dec 30, 2024
344abff
refactor: rename files, functions, types and variables
aaschlote Dec 30, 2024
186125d
test: fix e2e test
aaschlote Dec 30, 2024
d7c956f
Merge branch 'main' into validate-multiple-fields
aaschlote Dec 30, 2024
1ad17c4
Merge branch 'main' into validate-multiple-fields
aaschlote Jan 6, 2025
7d6a6ff
refactor(getMultipleFieldsByStepIdValidation): improve return functio…
aaschlote Jan 6, 2025
2a583dc
Merge branch 'main' into validate-multiple-fields
aaschlote Jan 6, 2025
2b7dd46
refactor(getMultipleFieldsByStepIdValidation): get multiple validatio…
aaschlote Jan 6, 2025
3abd421
Merge branch 'main' into validate-multiple-fields
aaschlote Jan 6, 2025
b6003df
refactor: rename multiple to multi
aaschlote Jan 6, 2025
de43993
test(validation.test): improve tests
aaschlote Jan 6, 2025
8aa00f7
refactor(buildStepValidator): improve variable name
aaschlote Jan 6, 2025
bd63633
Merge branch 'main' into validate-multiple-fields
aaschlote Jan 7, 2025
659ca61
Merge branch 'main' into validate-multiple-fields
aaschlote Jan 7, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/components/form/ValidatedFlowForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { StrapiFormComponents } from "~/services/cms/components/StrapiFormCompon
import type { StrapiFormComponent } from "~/services/cms/models/StrapiFormComponent";
import { splatFromParams } from "~/services/params";
import { CSRFKey } from "~/services/security/csrf/csrfKey";
import { validatorForFieldnames } from "~/services/validation/buildStepValidator";
import { validatorForFieldNames } from "~/services/validation/stepValidator/validatorForFieldNames";

type ValidatedFlowFormProps = {
stepData: Context;
Expand All @@ -25,7 +25,7 @@ function ValidatedFlowForm({
const stepId = splatFromParams(useParams());
const { pathname } = useLocation();
const fieldNames = formElements.map((entry) => entry.name);
const validator = validatorForFieldnames(fieldNames, pathname);
const validator = validatorForFieldNames(fieldNames, pathname);
return (
<ValidatedForm
id={`${stepId}_form`}
Expand Down
6 changes: 3 additions & 3 deletions app/components/form/__test__/ValidatedFlowForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import { getStrapiTextareaComponent } from "tests/factories/cmsModels/strapiText
import { getStrapiTileGroupComponent } from "tests/factories/cmsModels/strapiTileGroupComponent";
import ValidatedFlowForm from "~/components/form/ValidatedFlowForm";
import type { StrapiFormComponent } from "~/services/cms/models/StrapiFormComponent";
import * as buildStepValidator from "~/services/validation/buildStepValidator";
import { checkedRequired } from "~/services/validation/checkedCheckbox";
import { createDateSchema } from "~/services/validation/date";
import { integerSchema } from "~/services/validation/integer";
import * as validatorForFieldNames from "~/services/validation/stepValidator/validatorForFieldNames";
aaschlote marked this conversation as resolved.
Show resolved Hide resolved
import { stringRequiredSchema } from "~/services/validation/stringRequired";
import { timeSchema } from "~/services/validation/time";
import {
Expand All @@ -36,8 +36,8 @@ vi.mock("~/services/params", () => ({
}));

const fieldNameValidatorSpy = vi.spyOn(
buildStepValidator,
"validatorForFieldnames",
validatorForFieldNames,
"validatorForFieldNames",
);

describe("ValidatedFlowForm", () => {
Expand Down
14 changes: 14 additions & 0 deletions app/domains/fluggastrechte/formular/multiFieldsValidation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { MultiFieldsStepIdValidation } from "~/domains/multiFieldsFlowValidation";
import {
validateAnotherFlightPage,
validateDepartureAfterArrival,
validateReplacementConnectionPage,
validateSameFlightPage,
} from "./services/validation";

export const fluggastrechtMultiFieldsValidation: MultiFieldsStepIdValidation = {
"/flugdaten/geplanter-flug": validateDepartureAfterArrival,
"/flugdaten/tatsaechlicher-flug-ankunft": validateSameFlightPage,
"/flugdaten/anderer-flug-ankunft": validateAnotherFlightPage,
"/flugdaten/ersatzverbindung-beschreibung": validateReplacementConnectionPage,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,334 @@
import { z } from "zod";
import {
validateAnotherFlightPage,
validateDepartureAfterArrival,
validateReplacementConnectionPage,
validateSameFlightPage,
} from "../validation";

describe("validation", () => {
describe("validateDepartureAfterArrival", () => {
const baseSchema = z.object({
direktAbflugsDatum: z.string(),
direktAbflugsZeit: z.string(),
direktAnkunftsDatum: z.string(),
direktAnkunftsZeit: z.string(),
});

const validator = validateDepartureAfterArrival(baseSchema);

it("should return success false given undefined values", () => {
const result = validator.safeParse({
direktAbflugsDatum: undefined,
direktAbflugsZeit: undefined,
direktAnkunftsDatum: undefined,
direktAnkunftsZeit: undefined,
});

expect(result.success).toBe(false);
});

it("should return success false given a departure time after the arrival", () => {
const result = validator.safeParse({
direktAbflugsDatum: "01.01.2024",
direktAbflugsZeit: "14:00",
direktAnkunftsDatum: "01.01.2024",
direktAnkunftsZeit: "11:00",
});

expect(result.success).toBe(false);
});

it("should return success false given a departure date after the arrival", () => {
const result = validator.safeParse({
direktAbflugsDatum: "02.01.2024",
direktAbflugsZeit: "14:00",
direktAnkunftsDatum: "01.01.2024",
direktAnkunftsZeit: "15:00",
});

expect(result.success).toBe(false);
});

it("should return success true given a departure date before the arrival", () => {
const result = validator.safeParse({
direktAbflugsDatum: "01.01.2024",
direktAbflugsZeit: "14:00",
aaschlote marked this conversation as resolved.
Show resolved Hide resolved
direktAnkunftsDatum: "02.01.2024",
direktAnkunftsZeit: "10:00",
});

expect(result.success).toBe(true);
});

it("should return success true given a departure time before the arrival", () => {
const result = validator.safeParse({
direktAbflugsDatum: "01.01.2024",
direktAbflugsZeit: "14:00",
direktAnkunftsDatum: "01.01.2024",
direktAnkunftsZeit: "15:00",
});

expect(result.success).toBe(true);
});
});

describe("validateSameFlightPage", () => {
const baseSchema = z.object({
direktAbflugsDatum: z.string(),
direktAbflugsZeit: z.string(),
tatsaechlicherAnkunftsDatum: z.string(),
tatsaechlicherAnkunftsZeit: z.string(),
});

const validatorSameFlightPage = validateSameFlightPage(baseSchema);

it("should return success false given undefined values", () => {
const result = validatorSameFlightPage.safeParse({
direktAbflugsDatum: undefined,
direktAbflugsZeit: undefined,
tatsaechlicherAnkunftsDatum: undefined,
tatsaechlicherAnkunftsZeit: undefined,
});

expect(result.success).toBe(false);
});

it("should return success false given a departure time after the arrival", () => {
const result = validatorSameFlightPage.safeParse({
direktAbflugsDatum: "01.01.2024",
direktAbflugsZeit: "14:00",
tatsaechlicherAnkunftsDatum: "01.01.2024",
tatsaechlicherAnkunftsZeit: "11:00",
});

expect(result.success).toBe(false);
});

it("should return success false given a departure date after the arrival", () => {
const result = validatorSameFlightPage.safeParse({
direktAbflugsDatum: "02.01.2024",
direktAbflugsZeit: "14:00",
tatsaechlicherAnkunftsDatum: "01.01.2024",
tatsaechlicherAnkunftsZeit: "15:00",
aaschlote marked this conversation as resolved.
Show resolved Hide resolved
});

expect(result.success).toBe(false);
});

it("should return success false given a departure date before three hours after the arrival", () => {
const result = validatorSameFlightPage.safeParse({
direktAbflugsDatum: "02.01.2024",
direktAbflugsZeit: "14:00",
tatsaechlicherAnkunftsDatum: "02.01.2024",
tatsaechlicherAnkunftsZeit: "15:00",
});

expect(result.success).toBe(false);
});

it("should return success true given a departure date after three hours after the arrival", () => {
const result = validatorSameFlightPage.safeParse({
direktAbflugsDatum: "02.01.2024",
direktAbflugsZeit: "14:00",
tatsaechlicherAnkunftsDatum: "02.01.2024",
tatsaechlicherAnkunftsZeit: "19:01",
});

expect(result.success).toBe(true);
});
});

describe("validateAnotherFlightPage", () => {
const baseSchema = z.object({
direktAbflugsDatum: z.string(),
direktAbflugsZeit: z.string(),
ersatzFlugAnkunftsDatum: z.string(),
ersatzFlugAnkunftsZeit: z.string(),
bereich: z.string(),
});

const validatorAnotherPage = validateAnotherFlightPage(baseSchema);

it("should return success false given undefined values", () => {
const result = validatorAnotherPage.safeParse({
direktAbflugsDatum: undefined,
direktAbflugsZeit: undefined,
ersatzFlugAnkunftsDatum: undefined,
ersatzFlugAnkunftsZeit: undefined,
bereich: "verspaetet",
});

expect(result.success).toBe(false);
});

it("should return success false given a departure time after the arrival", () => {
const result = validatorAnotherPage.safeParse({
direktAbflugsDatum: "01.01.2024",
direktAbflugsZeit: "14:00",
ersatzFlugAnkunftsDatum: "01.01.2024",
ersatzFlugAnkunftsZeit: "11:00",
bereich: "verspaetet",
});

expect(result.success).toBe(false);
});

it("should return success false given a departure date after the arrival", () => {
const result = validatorAnotherPage.safeParse({
direktAbflugsDatum: "02.01.2024",
direktAbflugsZeit: "14:00",
ersatzFlugAnkunftsDatum: "01.01.2024",
ersatzFlugAnkunftsZeit: "15:00",
bereich: "verspaetet",
});

expect(result.success).toBe(false);
});

it("should return success false given a departure date before three hours after the arrival and bereich verspaetet", () => {
const result = validatorAnotherPage.safeParse({
direktAbflugsDatum: "02.01.2024",
direktAbflugsZeit: "14:00",
ersatzFlugAnkunftsDatum: "02.01.2024",
ersatzFlugAnkunftsZeit: "15:00",
bereich: "verspaetet",
});

expect(result.success).toBe(false);
});

it("should return success true given a departure date after three hours after the arrival and bereich verspaetet", () => {
const result = validatorAnotherPage.safeParse({
direktAbflugsDatum: "02.01.2024",
direktAbflugsZeit: "14:00",
ersatzFlugAnkunftsDatum: "02.01.2024",
ersatzFlugAnkunftsZeit: "19:01",
bereich: "verspaetet",
});

expect(result.success).toBe(true);
});

it("should return success true given a departure date before three hours after the arrival and bereich annullierung", () => {
aaschlote marked this conversation as resolved.
Show resolved Hide resolved
const result = validatorAnotherPage.safeParse({
direktAbflugsDatum: "02.01.2024",
direktAbflugsZeit: "14:00",
ersatzFlugAnkunftsDatum: "02.01.2024",
ersatzFlugAnkunftsZeit: "15:00",
bereich: "annullierung",
});

expect(result.success).toBe(true);
});

it("should return success true given a departure date before three hours after the arrival and bereich nichtbefoerderung", () => {
const result = validatorAnotherPage.safeParse({
direktAbflugsDatum: "02.01.2024",
direktAbflugsZeit: "14:00",
ersatzFlugAnkunftsDatum: "02.01.2024",
ersatzFlugAnkunftsZeit: "15:00",
bereich: "nichtbefoerderung",
});

expect(result.success).toBe(true);
});
});

describe("validateReplacementConnectionPage", () => {
const baseSchema = z.object({
direktAbflugsDatum: z.string(),
direktAbflugsZeit: z.string(),
andereErsatzverbindungAnkunftsDatum: z.string(),
andereErsatzverbindungAnkunftsZeit: z.string(),
bereich: z.string(),
});

const validatorReplacementConnectionPage =
validateReplacementConnectionPage(baseSchema);

it("should return success false given undefined values", () => {
const result = validatorReplacementConnectionPage.safeParse({
direktAbflugsDatum: undefined,
direktAbflugsZeit: undefined,
andereErsatzverbindungAnkunftsDatum: undefined,
andereErsatzverbindungAnkunftsZeit: undefined,
bereich: "verspaetet",
});

expect(result.success).toBe(false);
});

it("should return success false given a departure time after the arrival", () => {
const result = validatorReplacementConnectionPage.safeParse({
direktAbflugsDatum: "01.01.2024",
direktAbflugsZeit: "14:00",
andereErsatzverbindungAnkunftsDatum: "01.01.2024",
andereErsatzverbindungAnkunftsZeit: "11:00",
bereich: "verspaetet",
});

expect(result.success).toBe(false);
});

it("should return success false given a departure date after the arrival", () => {
const result = validatorReplacementConnectionPage.safeParse({
direktAbflugsDatum: "02.01.2024",
direktAbflugsZeit: "14:00",
aaschlote marked this conversation as resolved.
Show resolved Hide resolved
andereErsatzverbindungAnkunftsDatum: "01.01.2024",
andereErsatzverbindungAnkunftsZeit: "15:00",
bereich: "verspaetet",
});

expect(result.success).toBe(false);
});

it("should return success false given a departure date before three hours after the arrival and bereich verspaetet", () => {
const result = validatorReplacementConnectionPage.safeParse({
direktAbflugsDatum: "02.01.2024",
direktAbflugsZeit: "14:00",
andereErsatzverbindungAnkunftsDatum: "02.01.2024",
andereErsatzverbindungAnkunftsZeit: "15:00",
bereich: "verspaetet",
});

expect(result.success).toBe(false);
});

it("should return success true given a departure date after three hours after the arrival and bereich verspaetet", () => {
const result = validatorReplacementConnectionPage.safeParse({
direktAbflugsDatum: "02.01.2024",
direktAbflugsZeit: "14:00",
andereErsatzverbindungAnkunftsDatum: "02.01.2024",
andereErsatzverbindungAnkunftsZeit: "19:01",
bereich: "verspaetet",
});

expect(result.success).toBe(true);
});

it("should return success true given a departure date before three hours after the arrival and bereich annullierung", () => {
aaschlote marked this conversation as resolved.
Show resolved Hide resolved
const result = validatorReplacementConnectionPage.safeParse({
direktAbflugsDatum: "02.01.2024",
direktAbflugsZeit: "14:00",
andereErsatzverbindungAnkunftsDatum: "02.01.2024",
andereErsatzverbindungAnkunftsZeit: "15:00",
bereich: "annullierung",
});

expect(result.success).toBe(true);
});

it("should return success true given a departure date before three hours after the arrival and bereich nichtbefoerderung", () => {
const result = validatorReplacementConnectionPage.safeParse({
direktAbflugsDatum: "02.01.2024",
direktAbflugsZeit: "14:00",
andereErsatzverbindungAnkunftsDatum: "02.01.2024",
andereErsatzverbindungAnkunftsZeit: "15:00",
bereich: "nichtbefoerderung",
});

expect(result.success).toBe(true);
});
});
});
Loading
Loading