Skip to content

Commit

Permalink
HV-1955 Use String#isBlank instead of trim in the NotBlankValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
marko-bekhta committed Dec 10, 2024
1 parent 4965992 commit 1dd8478
Showing 1 changed file with 6 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,27 @@
import jakarta.validation.constraints.NotBlank;

/**
* Check that a character sequence is not {@code null} nor empty after removing any leading or trailing whitespace.
* Check that a character sequence is not {@code null} nor {@link String#isBlank() blank}.
*
* @author Guillaume Smet
*/
public class NotBlankValidator implements ConstraintValidator<NotBlank, CharSequence> {

/**
* Checks that the character sequence is not {@code null} nor empty after removing any leading or trailing
* whitespace.
* Checks that the character sequence is not {@code null} nor {@link String#isBlank() blank}.
*
* @param charSequence the character sequence to validate
* @param constraintValidatorContext context in which the constraint is evaluated
* @return returns {@code true} if the string is not {@code null} and the length of the trimmed
* {@code charSequence} is strictly superior to 0, {@code false} otherwise
* @return returns {@code true} if the string is not {@code null} and
* the call to {@link String#isBlank() charSequence.isBlank()} returns {@code false}, {@code false} otherwise
* @see String#isBlank()
*/
@Override
public boolean isValid(CharSequence charSequence, ConstraintValidatorContext constraintValidatorContext) {
if ( charSequence == null ) {
return false;
}

return charSequence.toString().trim().length() > 0;
return !charSequence.toString().isBlank();
}
}

0 comments on commit 1dd8478

Please sign in to comment.