Gunnar wrote:
Hi,
Thanks for the detailed reports. Scenario 1 looks like a bug indeed, I've created https://hibernate.atlassian.net/browse/HV-810 for this. Maybe you would be interested in providing a fix for this issue? Btw. I'm not sure why you couldn't create in issue in JIRA; Do you have a user account?
Regarding scenario 2, I don't think that's a bug. As per the BV spec, null is considered by all validators as valid (except for the @NotNull validator of course). But you have to deal with all other values in the validator. That's what is happening here, an empty string (as well as Strings with non-digit characters) doesn't represent a number within the specified range and thus is invalid. You may add @NotEmpty or @Size(min = 1) to your attribute to make sure the string is not empty. Or you could create a composed constraint comprising @Range and @NotEmpty.
--Gunnar
Hi,
thanks for the answer. You are right, I don't have an user account on JIRA.
A possible solution for Scenario 1 is to check that there aren't trailing 'separators'. Due to String.split method specification, they will ignore all of them. In this case "@" is the pattern for split, so we can use the String.endsWith method to ensure that. This is a simple solution and it will work for the EmailValidator:
Code:
...
if ( value == null || value.length() == 0 ) {
return true;
}
if (value.toString().endsWith( "@" )) {
return false;
}
// split email at '@' and consider local and domain part separately
String[] emailParts = value.toString().split( "@" );
...
But, as "@" is a regular expression that will be compiled by Pattern class, we could do it for any regular expression.
Code:
/**
* Regular expression for email.
*/
private Pattern separator = java.util.regex.Pattern.compile( "@" );
private boolean isValid(final CharSequence value) {
if ( value == null || value.length() == 0 ) {
return true;
}
// Count the separator occurrences
final Matcher matcher = separator.matcher(value);
int matchCount = 0;
while (matcher.find()) {
matchCount++;
}
if (matchCount != 1) {
return false;
}
// split email at '@' and consider local and domain part separately
String[] emailParts = value.toString().split(separator.pattern());
...
The second solution works for every regular expression but maybe is not necessary in this case.
Regarding Scenario 2, I agree with you about an empty string is not a valid number, I will work around with a composed or a custom constraint.
Thanks for the explanation.
-- Francisco