diff --git a/src/main/java/com/chat/yourway/annotation/validator/NicknameValidator.java b/src/main/java/com/chat/yourway/annotation/validator/NicknameValidator.java index 9175c943..66409823 100644 --- a/src/main/java/com/chat/yourway/annotation/validator/NicknameValidator.java +++ b/src/main/java/com/chat/yourway/annotation/validator/NicknameValidator.java @@ -10,7 +10,8 @@ public class NicknameValidator implements ConstraintValidator { - private static final Pattern NICKNAME_PATTERN = Pattern.compile("^[a-zA-Z0-9а-яА-ЯІіЇї]{4,20}$"); + private static final Pattern NICKNAME_PATTERN = Pattern.compile( + "^[a-zA-Z0-9а-яА-ЯІіЇї!@#$%^&*_\\-+=~?]{4,20}$"); @Override public boolean isValid(String value, ConstraintValidatorContext context) { @@ -23,13 +24,15 @@ public boolean isValid(String value, ConstraintValidatorContext context) { return false; } else if (!NICKNAME_PATTERN.matcher(value).matches()) { context - .buildConstraintViolationWithTemplate("The nickname is invalid") + .buildConstraintViolationWithTemplate( + "The nickname should be 4-20 characters long and include only letters, numbers or " + + "symbols [! @ # $ % ^ & * _ - + = ~ ?]") .addConstraintViolation(); return false; } else if (PATTERN_SPACE.matcher(value).matches()) { context - .buildConstraintViolationWithTemplate("The nickname should not include space") - .addConstraintViolation(); + .buildConstraintViolationWithTemplate("The nickname should not include space") + .addConstraintViolation(); return false; } diff --git a/src/test/java/com/chat/yourway/unit/annotation/validator/NicknameValidationValidatorTest.java b/src/test/java/com/chat/yourway/unit/annotation/validator/NicknameValidationValidatorTest.java index 059bc3b5..b1dd70f2 100644 --- a/src/test/java/com/chat/yourway/unit/annotation/validator/NicknameValidationValidatorTest.java +++ b/src/test/java/com/chat/yourway/unit/annotation/validator/NicknameValidationValidatorTest.java @@ -38,16 +38,19 @@ public void shouldPassValidator_whenUserInputNickname() { var nicknameFirstExample = "user123"; var nicknameSecondExample = "Username123"; var nicknameThirdExample = "abcd1234"; + var nicknameWithSymbol = "Nickname!"; // When var isValidFirstExample = nicknameValidator.isValid(nicknameFirstExample, context); var isValidSecondExample = nicknameValidator.isValid(nicknameSecondExample, context); var isValidThirdExample = nicknameValidator.isValid(nicknameThirdExample, context); + var isValid4Example = nicknameValidator.isValid(nicknameWithSymbol, context); // Then assertTrue(isValidFirstExample); assertTrue(isValidSecondExample); assertTrue(isValidThirdExample); + assertTrue(isValid4Example); } @DisplayName( @@ -73,7 +76,7 @@ public void shouldFailValidator_whenUserInputNicknameWithInvalidLength() { public void shouldFailValidator_whenUserInputNicknameWithInvalidCharacters() { // Given var nicknameFirstExample = "user 123"; - var nicknameSecondExample = "user@123"; + var nicknameSecondExample = "user)123"; // When var isValidFirstExample = nicknameValidator.isValid(nicknameFirstExample, context);