diff --git a/src/main/java/org/gelecekbilimde/scienceplatform/auth/model/request/RegisterRequest.java b/src/main/java/org/gelecekbilimde/scienceplatform/auth/model/request/RegisterRequest.java index 64390c2..f20fbbd 100644 --- a/src/main/java/org/gelecekbilimde/scienceplatform/auth/model/request/RegisterRequest.java +++ b/src/main/java/org/gelecekbilimde/scienceplatform/auth/model/request/RegisterRequest.java @@ -31,7 +31,6 @@ public class RegisterRequest { @NotBlank @Username - @Size(min = 3, max = 20) private String username; @NotBlank diff --git a/src/main/java/org/gelecekbilimde/scienceplatform/common/util/validation/UsernameValidator.java b/src/main/java/org/gelecekbilimde/scienceplatform/common/util/validation/UsernameValidator.java index 24052fa..dfc76a3 100644 --- a/src/main/java/org/gelecekbilimde/scienceplatform/common/util/validation/UsernameValidator.java +++ b/src/main/java/org/gelecekbilimde/scienceplatform/common/util/validation/UsernameValidator.java @@ -6,7 +6,7 @@ class UsernameValidator implements ConstraintValidator { - private static final String USERNAME_REGEX = "^[a-zA-Z0-9]$"; + private static final String USERNAME_REGEX = "^[a-zA-Z0-9]{3,20}$"; @Override public boolean isValid(String value, ConstraintValidatorContext context) { @@ -23,6 +23,13 @@ public boolean isValid(String value, ConstraintValidatorContext context) { return false; } + if (value.length() <= 3 || value.length() >= 20) { + context.disableDefaultConstraintViolation(); + context.buildConstraintViolationWithTemplate("Username must be between 3 and 20 characters long") + .addConstraintViolation(); + return false; + } + if (!lowerCasedValue.matches(USERNAME_REGEX)) { context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate("Username must be alphanumeric")