Skip to content

Commit

Permalink
refactor:アプリケーション
Browse files Browse the repository at this point in the history
Refactor password policy validation logic.

Replaces regex-based validation with an iterative approach for better readability and performance. Adds a null check for password input and introduces early exit once all conditions are satisfied.
  • Loading branch information
k2works committed Dec 28, 2024
1 parent eab7191 commit 510a848
Showing 1 changed file with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
import lombok.NoArgsConstructor;
import lombok.Value;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* パスワード
*/
Expand All @@ -24,14 +21,26 @@ public Password(String value) {
}

private void checkPolicy(String value) {
if (value.length() < 8) {
if (value == null || value.length() < 8) {
throw new PasswordException("パスワードは8文字以上である必要があります");
}

String regex = "(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(value);
if (!matcher.find()) throw new PasswordException("パスワードは小文字、大文字、数字を含む必要があります");
boolean hasDigit = false;
boolean hasLower = false;
boolean hasUpper = false;

for (char c : value.toCharArray()) {
if (Character.isDigit(c)) hasDigit = true;
else if (Character.isLowerCase(c)) hasLower = true;
else if (Character.isUpperCase(c)) hasUpper = true;

// すべての条件を満たしたら早期終了する
if (hasDigit && hasLower && hasUpper) break;
}

if (!hasDigit || !hasLower || !hasUpper) {
throw new PasswordException("パスワードは小文字、大文字、数字を含む必要があります");
}
}

/**
Expand Down

0 comments on commit 510a848

Please sign in to comment.