Skip to content

Commit

Permalink
feat(FGR distance): add integer schema with thousand separator
Browse files Browse the repository at this point in the history
Co-Authored-By: Rafael Falk <rbrtrflflk@gmail.com>
Co-Authored-By: Joschka <joschka@users.noreply.github.com>
  • Loading branch information
3 people committed Jan 18, 2024
1 parent add19a4 commit 6207656
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
3 changes: 2 additions & 1 deletion app/models/flows/fluggastrechteFormular/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
} from "~/services/validation/checkedCheckbox";
import { dateSchema } from "~/services/validation/date";
import { timeSchema } from "~/services/validation/time";
import { integerSchema } from "~/services/validation/integer";

const FileUploadDummySchema = z.string().or(z.object({})).or(z.array(z.any()));

Expand Down Expand Up @@ -57,7 +58,7 @@ export const fluggastrechtContext = {
...namePrivatPerson,
volljaehrig: YesNoAnswer,
gesetzlicheVertretung: YesNoAnswer,
entfernung: z.coerce.number().min(1),
entfernung: integerSchema,
teilentschaedigung: YesNoAnswer,
frist: z.string(), // TODO: validate as German date in the future
nebenforderungen: z.object({
Expand Down
2 changes: 1 addition & 1 deletion app/models/flows/fluggastrechteFormular/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const fluggastrechtFlow = {
stringReplacements: (context: AllContexts) => {
if (!("zwischenstopps" in context)) return {};
return {
entfernung: context.entfernung?.toString(),
entfernung: context.entfernung?.toLocaleString("de"),
startAirport: context.startAirport,
endAirport: context.endAirport,
zwischenstoppAirport: context.zwischenstoppFlughafen,
Expand Down
10 changes: 10 additions & 0 deletions app/services/validation/integer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { z } from "zod";

export const integerSchema = z
.string()
.trim()
.min(1, { message: "required" })
.refine((numString) => /^-?(\d+|\d{1,3}(\.\d{3})+)$/.test(numString), {
message: "invalidInteger",
})
.transform((numString) => Number(numString.replace(/\./g, "")));
42 changes: 42 additions & 0 deletions tests/unit/services/validation/integer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type { SafeParseError } from "zod";
import { integerSchema } from "~/services/validation/integer";

describe("integer", () => {
describe("success cases", () => {
const cases = [
{ input: " 100 ", expected: 100 },
{ input: "1000", expected: 1000 },
{ input: "1.000", expected: 1000 },
{ input: "1.000.000", expected: 1000000 },
];

test.each(cases)(
"given $input, returns $expected",
({ input, expected }) => {
const actual = integerSchema.safeParse(input);
expect(actual).toEqual({ data: expected, success: true });
},
);
});

describe("failing cases", () => {
const cases = [
{ input: "", errorMessage: "required" },
{ input: " ", errorMessage: "required" },
{ input: "1.0", errorMessage: "invalidInteger" },
{ input: "1000.0", errorMessage: "invalidInteger" },
{ input: "1000,0", errorMessage: "invalidInteger" },
];

test.each(cases)(
"given $input, returns $errorMessage",
({ input, errorMessage }) => {
const actual = integerSchema.safeParse(input);
expect(actual.success).toBe(false);
expect(
(actual as SafeParseError<unknown>).error.issues[0].message,
).toBe(errorMessage);
},
);
});
});

0 comments on commit 6207656

Please sign in to comment.