Skip to content

Commit 5687e07

Browse files
committed
メールアドレスの検証
1 parent 5da8451 commit 5687e07

7 files changed

Lines changed: 65 additions & 6 deletions

File tree

jest.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@ module.exports = {
33
preset: 'ts-jest',
44
testEnvironment: 'node',
55
testMatch: ['**/test/**/*.ts', '**/test/**/?(*.)+(spec|test).ts'],
6-
testPathIgnorePatterns: ['/node_modules/', '/dist/']
6+
testPathIgnorePatterns: ['/node_modules/', '/dist/'],
7+
moduleNameMapper: {
8+
'^@/(.*)$': '<rootDir>/src/$1',
9+
},
710
};

package-lock.json

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,9 @@
3131
"volta": {
3232
"node": "18.18.0",
3333
"npm": "10.2.0"
34+
},
35+
"dependencies": {
36+
"@types/validator": "^13.9.0",
37+
"validator": "^13.9.0"
3438
}
3539
}

src/index.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/util/isValidEmail.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import isEmail from 'validator/lib/isEmail';
2+
3+
export default function isValidEmail(mail: string) {
4+
// 150文字制限
5+
if (mail.length > 150) {
6+
return false;
7+
}
8+
return isEmail(mail);
9+
}

test/util/isValidEmail.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import isValidEmail from '@/util/isValidEmail';
2+
3+
describe('isValidEmail', () => {
4+
it('150文字以下', () => {
5+
expect(isValidEmail('cuculus@example.com')).toBe(true);
6+
});
7+
8+
it('150文字以上', () => {
9+
let prefix = '';
10+
for (let i = 0; i < 150; i++) {
11+
prefix += 'A';
12+
}
13+
expect(isValidEmail(`@example.com`)).toBe(false);
14+
});
15+
16+
it('エイリアスが使用可能', () => {
17+
expect(isValidEmail(`cuculus+test@example.com`)).toBe(true);
18+
});
19+
20+
it('メールアドレスではない', () => {
21+
expect(isValidEmail(`example.com`)).toBe(false);
22+
});
23+
});

tsconfig.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@
1414
"skipLibCheck": true,
1515
"forceConsistentCasingInFileNames": true,
1616
"esModuleInterop": true,
17-
"outDir": "./dist"
17+
"outDir": "./dist",
18+
"paths": {
19+
"@/*": [
20+
"./src/*"
21+
]
22+
}
1823
},
19-
"exclude": ["node_modules", "test"]
24+
"exclude": ["node_modules", "test"],
25+
2026
}

0 commit comments

Comments
 (0)