Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HV-1955 Use String#isBlank instead of trim in the NotBlankValidator #1512

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
Loading