Skip to content

Commit

Permalink
Merge pull request #53 from Chat-Your-Way/CHAT-154-bugfix-nickname-fi…
Browse files Browse the repository at this point in the history
…eld-not-accepted-special-symbols

Chat 154 bugfix nickname field not accepted special symbols
  • Loading branch information
Troha7 authored Jan 8, 2024
2 parents 9927f5f + 7ccac22 commit 6353d7c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

public class NicknameValidator implements ConstraintValidator<NicknameValidation, String> {

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) {
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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);
Expand Down

0 comments on commit 6353d7c

Please sign in to comment.