Skip to content

Commit 7d67816

Browse files
authored
Merge pull request #28 from klmhyeonwoo/feature/util-workspace
feat: Add checkPassword util function
2 parents fcdcb18 + e207546 commit 7d67816

4 files changed

Lines changed: 74 additions & 0 deletions

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ const isValid = validationUtil.checkEmail("user@example.com"); // true
5050
const isHttpUrl = validationUtil.checkHttpUrl("https://example.com"); // true
5151
const isDomain = validationUtil.checkDomain("example.com"); // true
5252
const isBase64 = validationUtil.checkBase64("U29tZSB2YWxpZCBiYXNlNjQgc3RyaW5n"); // true
53+
const isPasswordValid = validationUtil.checkPassword("Abc123!@#", {
54+
minLength: 8,
55+
requireUppercase: true,
56+
requireLowercase: true,
57+
requireNumber: true,
58+
requireSpecialChar: true,
59+
}); // true
5360

5461
// Common utilities
5562
const empty = commonUtil.isEmpty(""); // true
@@ -87,6 +94,7 @@ const theme = cookieUtil.getCookie("theme");
8794
- `checkHttpUrl(url: string): boolean` - Validates HTTP/HTTPS URL format
8895
- `checkDomain(domain: string): boolean` - Validates domain name format
8996
- `checkBase64(value: string): boolean` - Validates whether a string is a valid base64 encoded value
97+
- `checkPassword(password: string, options?: { minLength?: number; maxLength?: number; requireUppercase?: boolean; requireLowercase?: boolean; requireNumber?: boolean; requireSpecialChar?: boolean }): boolean` - Validates password strength and requirements
9098

9199
### CommonUtil
92100

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { describe, expect, test } from "vitest";
2+
import checkPassword from ".";
3+
4+
describe("checkPassword 유틸 함수 테스트", () => {
5+
describe("유효한 비밀번호에 대해 true를 반환해야 합니다.", () => {
6+
test("기본 상태는 아무 옵션도 설정되어있지 않기 때문에 true를 반환해야 합니다.", () => {
7+
expect(checkPassword("Valid123!", {})).toBe(true);
8+
});
9+
test("특수 문자가 필요한 경우 true를 반환해야 합니다.", () => {
10+
expect(checkPassword("vsdvdsvsdvs1!", { requireSpecialChar: true })).toBe(
11+
true
12+
);
13+
});
14+
test("대문자가 필요한 경우 true를 반환해야 합니다.", () => {
15+
expect(checkPassword("Vsdvd", { requireUppercase: true })).toBe(true);
16+
});
17+
test("최소 3자, 최대 5자의 길이를 가져야 하는 경우 true를 반환해야 합니다.", () => {
18+
expect(checkPassword("Vsd", { minLength: 3, maxLength: 5 })).toBe(true);
19+
});
20+
});
21+
22+
describe("유효하지 않은 비밀번호에 대해 false를 반환해야 합니다.", () => {
23+
test("길이가 유효하지 않은 경우 false를 반환해야 합니다.", () => {
24+
expect(checkPassword("invalid", { minLength: 3, maxLength: 5 })).toBe(
25+
false
26+
);
27+
});
28+
test("특수 문자가 없는 경우 false를 반환해야 합니다.", () => {
29+
expect(checkPassword("dffadsfas", { requireSpecialChar: true })).toBe(
30+
false
31+
);
32+
});
33+
});
34+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export default function checkPassword(
2+
password: string,
3+
options?: {
4+
minLength?: number;
5+
maxLength?: number;
6+
requireUppercase?: boolean;
7+
requireLowercase?: boolean;
8+
requireNumber?: boolean;
9+
requireSpecialChar?: boolean;
10+
}
11+
): boolean {
12+
const {
13+
minLength,
14+
maxLength,
15+
requireUppercase = false,
16+
requireLowercase = false,
17+
requireNumber = false,
18+
requireSpecialChar = false,
19+
} = options || {};
20+
21+
if (minLength !== undefined && password.length < minLength) return false;
22+
if (maxLength !== undefined && password.length > maxLength) return false;
23+
const hasUpperCase = requireUppercase ? /[A-Z]/.test(password) : true;
24+
const hasLowerCase = requireLowercase ? /[a-z]/.test(password) : true;
25+
const hasNumber = requireNumber ? /\d/.test(password) : true;
26+
const hasSpecialChar = requireSpecialChar
27+
? /[!@#$%^&*(),.?":{}|<>]/.test(password)
28+
: true;
29+
30+
return hasUpperCase && hasLowerCase && hasNumber && hasSpecialChar;
31+
}

package/validationUtil/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export { default as checkEmail } from "./checkEmail";
22
export { default as checkHttpUrl } from "./checkHttpUrl";
33
export { default as checkDomain } from "./checkDomain";
44
export { default as checkBase64 } from "./checkBase64";
5+
export { default as checkPassword } from "./checkPassword";

0 commit comments

Comments
 (0)