-
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.
feat(FGR distance): add integer schema with thousand separator
Co-Authored-By: Rafael Falk <rbrtrflflk@gmail.com> Co-Authored-By: Joschka <joschka@users.noreply.github.com>
- Loading branch information
1 parent
add19a4
commit 6207656
Showing
4 changed files
with
55 additions
and
2 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
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, ""))); |
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,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); | ||
}, | ||
); | ||
}); | ||
}); |