Skip to content

Commit 13b47c9

Browse files
authored
Merge pull request #596 from companieshouse/lp-1384-invalid-date-error-fix
LP-1384: Improve invalid date error handling
2 parents 801cd57 + a6ede4a commit 13b47c9

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

src/infrastructure/gateway/utils.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,17 @@ const zeroPadNumber = (input: string = ""): string => {
3535
const isDateValid = (date: string): boolean => {
3636
const [year, month, day] = date.split("-");
3737

38-
const isMonthInvalid = isNaN(parseInt(month));
39-
const isYearInvalid = isNaN(parseInt(year)) || year.length !== 4;
38+
const isDayInvalid = day.length > 2 || isNaN(parseInt(day));
39+
const isMonthInvalid = month.length > 2 || isNaN(parseInt(month));
40+
const isYearInvalid = year.length !== 4 || isNaN(parseInt(year));
4041

41-
if (isMonthInvalid || isYearInvalid) {
42+
if (isDayInvalid || isMonthInvalid || isYearInvalid) {
4243
return false;
4344
}
4445

4546
const days = daysInMonth(parseInt(month));
4647

47-
if (!day.length || parseInt(day) > days) {
48+
if (parseInt(day) > days) {
4849
return false;
4950
}
5051

src/presentation/test/unit/gateway/utils.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ describe("Gateway utils test suite", () => {
5858
["space as year", { day: "11", month: "03", year: " " }],
5959
["empty string as day", { day: "", month: "03", year: "1980" }],
6060
["empty string as month", { day: "11", month: "", year: "1980" }],
61-
["empty string as year", { day: "11", month: "03", year: "" }]
61+
["empty string as year", { day: "11", month: "03", year: "" }],
62+
["day with 3 digits", { day: "001", month: "10", year: "2011" }],
63+
["month with 3 digits", { day: "01", month: "012", year: "2011" }]
6264
])("should return false for invalid date - %s", (_desciption, date) => {
6365
let thrownError: UIErrors | null = null;
6466

0 commit comments

Comments
 (0)