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-1971 fix CNPJ validation when all digits are the same #1339
base: main
Are you sure you want to change the base?
Conversation
Thanks for your pull request! This pull request appears to follow the contribution rules. › This message was automatically generated. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, thanks for reporting the problem and submitting a PR. Please see an inline comment.
@@ -55,6 +56,13 @@ public boolean isValid(CharSequence value, ConstraintValidatorContext context) { | |||
return true; | |||
} | |||
|
|||
// Check for repeated digits | |||
if ( value.toString().chars() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe let's replace this with a simple for loop? Something along the lines:
// the number is too short, hence it's invalid anyway:
if ( value.length() < 2 ) {
return false;
}
char firstDigit = value.charAt( 0 );
char otherDigit = value.charAt( 1 );
for ( int i = 2; i < value.length(); i++ ) {
char c = value.charAt( i );
if ( Character.isDigit( c ) && firstDigit != c ) {
otherDigit = c;
}
}
if ( firstDigit == otherDigit ) {
return false;
}
From what I see distinct
may not be optimized:
public final IntStream distinct() {
// While functional and quick to implement, this approach is not very efficient.
// An efficient version requires an int-specific map/set implementation.
return boxed().distinct().mapToInt(i -> i);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perfect, I'll change it. Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, thanks!
https://hibernate.atlassian.net/browse/HV-1971