| [700e2f9] | 1 | package com.finki.icare.utils;
|
|---|
| 2 |
|
|---|
| 3 | import com.finki.icare.exceptions.ICareException;
|
|---|
| 4 | import lombok.experimental.UtilityClass;
|
|---|
| 5 |
|
|---|
| 6 | @UtilityClass
|
|---|
| 7 | public class ValidationUtils {
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 | public static void validatePassword(String password) {
|
|---|
| 11 | if (password == null || password.length() < 8) {
|
|---|
| 12 | throw ICareException.badRequest("Password must be at least 8 characters long");
|
|---|
| 13 | }
|
|---|
| 14 |
|
|---|
| 15 | if (password.length() > 128) {
|
|---|
| 16 | throw ICareException.badRequest("Password must not exceed 128 characters");
|
|---|
| 17 | }
|
|---|
| 18 |
|
|---|
| 19 | boolean hasUppercase = password.chars().anyMatch(Character::isUpperCase);
|
|---|
| 20 | boolean hasLowercase = password.chars().anyMatch(Character::isLowerCase);
|
|---|
| 21 | boolean hasDigit = password.chars().anyMatch(Character::isDigit);
|
|---|
| 22 | boolean hasSpecial = password.chars().anyMatch(ch -> "!@#$%^&*()_+-=[]{}|;:,.<>?".indexOf(ch) >= 0);
|
|---|
| 23 |
|
|---|
| 24 | if (!hasUppercase) {
|
|---|
| 25 | throw ICareException.badRequest("Password must contain at least one uppercase letter");
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | if (!hasLowercase) {
|
|---|
| 29 | throw ICareException.badRequest("Password must contain at least one lowercase letter");
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | if (!hasDigit) {
|
|---|
| 33 | throw ICareException.badRequest("Password must contain at least one number");
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | if (!hasSpecial) {
|
|---|
| 37 | throw ICareException.badRequest("Password must contain at least one special character (!@#$%^&*()_+-=[]{}|;:,.<>?)");
|
|---|
| 38 | }
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | public static void validateUsername(String username) {
|
|---|
| 42 | if (username == null || username.trim().isEmpty()) {
|
|---|
| 43 | throw ICareException.badRequest("Username is required");
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | if (username.length() < 3) {
|
|---|
| 47 | throw ICareException.badRequest("Username must be at least 3 characters long");
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | if (username.length() > 50) {
|
|---|
| 51 | throw ICareException.badRequest("Username must not exceed 50 characters");
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | if (!username.matches("^[a-zA-Z0-9_.-]+$")) {
|
|---|
| 55 | throw ICareException.badRequest("Username can only contain letters, numbers, dots, hyphens, and underscores");
|
|---|
| 56 | }
|
|---|
| 57 | }
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|