-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-Authored-By: Pram Gurusinga <[email protected]> Co-Authored-By: Joschka <[email protected]>
- Loading branch information
1 parent
cebe985
commit 06ca71f
Showing
7 changed files
with
152 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { type FluggastrechtContext } from "./context"; | ||
|
||
type Guard = (context: FluggastrechtContext) => boolean; | ||
|
||
function yesNoGuards<Field extends keyof FluggastrechtContext>( | ||
field: Field, | ||
): { [field in Field as `${field}Yes`]: Guard } & { | ||
[field in Field as `${field}No`]: Guard; | ||
} { | ||
//@ts-ignore | ||
return { | ||
[`${field}Yes`]: ((context) => context[field] === "yes") as Guard, | ||
[`${field}No`]: ((context) => context[field] === "no") as Guard, | ||
}; | ||
} | ||
|
||
export const fluggastrechteGuards = { | ||
...yesNoGuards("zwischenstopps"), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { z } from "zod"; | ||
|
||
export const airportSchema = z | ||
.string() | ||
.trim() | ||
.regex(/^[a-zA-Z]{3}$/, "wrong_airport_format") | ||
.transform((v) => v.toUpperCase()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { airportSchema } from "~/services/validation/airport"; | ||
import type { SafeParseError } from "zod"; | ||
|
||
describe("airport validation", () => { | ||
describe("success cases", () => { | ||
const cases = [ | ||
{ input: " abc ", expected: "ABC" }, | ||
{ input: "Bar", expected: "BAR" }, | ||
{ input: " fOo ", expected: "FOO" }, | ||
]; | ||
|
||
test.each(cases)( | ||
"given $input, returns $expected", | ||
({ input, expected }) => { | ||
const actual = airportSchema.safeParse(input); | ||
expect(actual).toEqual({ data: expected, success: true }); | ||
}, | ||
); | ||
}); | ||
|
||
describe("failing cases", () => { | ||
const cases = [ | ||
{ input: "", errorMessage: "wrong_airport_format" }, | ||
{ input: "ab", errorMessage: "wrong_airport_format" }, | ||
{ input: "123", errorMessage: "wrong_airport_format" }, | ||
{ input: " :) ", errorMessage: "wrong_airport_format" }, | ||
{ input: "foobar ", errorMessage: "wrong_airport_format" }, | ||
]; | ||
|
||
test.each(cases)( | ||
"given $input, returns $errorMessage", | ||
({ input, errorMessage }) => { | ||
const actual = airportSchema.safeParse(input); | ||
expect(actual.success).toBe(false); | ||
expect( | ||
(actual as SafeParseError<unknown>).error.issues[0].message, | ||
).toBe(errorMessage); | ||
}, | ||
); | ||
}); | ||
}); |