source: backend/src/main/java/com/finki/icare/utils/ValidationUtils.java@ 700e2f9

main
Last change on this file since 700e2f9 was 700e2f9, checked in by 186079 <matej.milevski@…>, 5 days ago

Init

  • Property mode set to 100644
File size: 2.1 KB
Line 
1package com.finki.icare.utils;
2
3import com.finki.icare.exceptions.ICareException;
4import lombok.experimental.UtilityClass;
5
6@UtilityClass
7public 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
Note: See TracBrowser for help on using the repository browser.