Skip to content

Commit 2565f17

Browse files
po4tiontjrdnjs1534
authored andcommitted
fix(@toss/validators): Increase the accuracy of verification for isBirthDate6 (toss#420)
* fix(@toss/validators): Increase the accuracy of verification * test(@toss/validators): add test case * fix: catch 2000y bug * refactor: change from magic number to constant variable * test: split test case * refactor: change constant variable name * refactor(@toss/validators): use proper library(date-fns)
1 parent fdf9605 commit 2565f17

File tree

5 files changed

+36
-13
lines changed

5 files changed

+36
-13
lines changed

.pnp.cjs

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/common/validators/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"typecheck": "tsc --noEmit"
2727
},
2828
"dependencies": {
29-
"@toss/utils": "workspace:^1.4.6"
29+
"@toss/utils": "workspace:^1.4.6",
30+
"date-fns": "^2.25.0"
3031
},
3132
"devDependencies": {
3233
"@babel/core": "^7",
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
11
import { isBirthDate6 } from './is-birth-date-6';
22

33
describe('isBirthDate6', () => {
4-
it('should return `true` if given value is valid', () => {
5-
expect(isBirthDate6('960729')).toEqual(true);
6-
expect(isBirthDate6('961231')).toEqual(true);
7-
expect(isBirthDate6('000101')).toEqual(true);
4+
describe('when given value is a valid string of 6 digits', () => {
5+
it('should return true for valid value', () => {
6+
expect(isBirthDate6('960729')).toEqual(true);
7+
expect(isBirthDate6('961231')).toEqual(true);
8+
expect(isBirthDate6('000101')).toEqual(true);
9+
expect(isBirthDate6('000229')).toEqual(true);
10+
});
811
});
9-
it('should return `false` if given value is not valid', () => {
10-
expect(isBirthDate6('19960729')).toEqual(false);
11-
expect(isBirthDate6('foobar')).toEqual(false);
12-
expect(isBirthDate6('000000')).toEqual(false);
13-
expect(isBirthDate6('960732')).toEqual(false);
14-
expect(isBirthDate6('')).toEqual(false);
12+
13+
describe('when given value is not a valid string of 6 digits', () => {
14+
it('should return false for invalid formats', () => {
15+
expect(isBirthDate6('19960729')).toEqual(false);
16+
expect(isBirthDate6('foobar')).toEqual(false);
17+
expect(isBirthDate6('000000')).toEqual(false);
18+
expect(isBirthDate6('')).toEqual(false);
19+
});
20+
21+
it('should return false for out of range values', () => {
22+
expect(isBirthDate6('960732')).toEqual(false);
23+
expect(isBirthDate6('951301')).toEqual(false);
24+
expect(isBirthDate6('970000')).toEqual(false);
25+
expect(isBirthDate6('950431')).toEqual(false);
26+
});
27+
28+
it('should return false for leap year edge cases', () => {
29+
expect(isBirthDate6('990229')).toEqual(false);
30+
expect(isBirthDate6('210029')).toEqual(false);
31+
});
1532
});
1633
});
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
/** @tossdocs-ignore */
2+
import { isValid, parse } from 'date-fns';
3+
24
export function isBirthDate6(birthDate: string) {
3-
const re = /^[0-9]{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[1,2][0-9]|3[0,1])$/;
4-
return re.test(birthDate);
5+
const parsed = parse(birthDate, 'yyMMdd', new Date());
6+
7+
return isValid(parsed);
58
}

yarn.lock

+1
Original file line numberDiff line numberDiff line change
@@ -6275,6 +6275,7 @@ __metadata:
62756275
"@types/node": ^14.14.41
62766276
babel-jest: ^29
62776277
concurrently: ^4.1.0
6278+
date-fns: ^2.25.0
62786279
jest: ^29.0.1
62796280
jest-environment-jsdom: ^29
62806281
rollup: ^2.77.0

0 commit comments

Comments
 (0)