-
Notifications
You must be signed in to change notification settings - Fork 25
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
[#168453950] Adult check at login if dateOfBirth is provided #575
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { isAdult } from "../date"; | ||
|
||
const today = new Date(); | ||
|
||
describe("Check if a birthdate is for an adult user", () => { | ||
it("should return true if the user has more than 18 years old", () => { | ||
const validAdultBirthdate = `${today.getFullYear() - 19}-01-01`; | ||
expect(isAdult(validAdultBirthdate)).toBeTruthy(); | ||
}); | ||
|
||
it("should return true if the user has exactly 18 years old", () => { | ||
const currentMount = | ||
today.getMonth() + 1 < 10 | ||
? `0${today.getMonth() + 1}` | ||
: today.getMonth() + 1; | ||
const validAdultBirthdate = `${today.getFullYear() - | ||
18}-${currentMount}-${today.getDate()}`; | ||
expect(isAdult(validAdultBirthdate)).toBeTruthy(); | ||
}); | ||
|
||
it("should return false if the the user has less than 18 years old", () => { | ||
const currentMount = | ||
today.getMonth() + 1 < 10 | ||
? `0${today.getMonth() + 1}` | ||
: today.getMonth() + 1; | ||
const validNotAdultBirthdate = `${today.getFullYear() - | ||
18}-${currentMount}-${today.getDate() - 1}`; | ||
expect(isAdult(validNotAdultBirthdate)).toBeFalsy(); | ||
}); | ||
|
||
it("should return false if the birthdate is invalid", () => { | ||
expect(isAdult("2000-13-32")).toBeFalsy(); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { isRight } from "fp-ts/lib/Either"; | ||
import * as t from "io-ts"; | ||
|
||
export const dateRegex = /^(?<year>[12]\d{3})-(?<month>0[1-9]|1[0-2])-(?<day>0[1-9]|[12]\d|3[01])$/; | ||
const dateRegexGroups = t.interface({ | ||
day: t.string, | ||
month: t.string, | ||
year: t.string | ||
}); | ||
|
||
export const isAdult = (dateOfBirth: string): boolean => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd refactor this method to take currentDate as input. This will simplify tests of isAdult() that should be deterministic and not use something like --> moreover I'd move the date parsing into the caller and, if we already include something like date-fns (or momentjs) use a library rather than a regex to parse the input date There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I'll use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 34282a4 |
||
const result = dateRegex.exec(dateOfBirth); | ||
const date = dateRegexGroups.decode(result?.groups); | ||
if (isRight(date)) { | ||
const year = Number(date.value.year); | ||
const currentDate = new Date(); | ||
const currentYear = currentDate.getFullYear(); | ||
const currentMonth = currentDate.getMonth() + 1; | ||
const month = Number(date.value.month); | ||
const dayNumber = Number(date.value.day); | ||
const currentDayNumber = currentDate.getDate(); | ||
if ( | ||
year + 18 > currentYear || | ||
(year + 18 === currentYear && | ||
(currentMonth > month || | ||
(currentMonth === month && currentDayNumber > dayNumber))) | ||
) { | ||
return false; | ||
} else { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can use
exists(isAdult)
see https://github.com/gcanti/fp-ts/blob/1.12.0/src/Option.ts#L303There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 34282a4