-
Notifications
You must be signed in to change notification settings - Fork 570
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-1999 new CNPJ alphanumeric format #1365
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -31,7 +31,7 @@ | |||||
* | ||||||
* @author George Gastaldi | ||||||
*/ | ||||||
@Pattern(regexp = "([0-9]{2}[.]?[0-9]{3}[.]?[0-9]{3}[/]?[0-9]{4}[-]?[0-9]{2})") | ||||||
@Pattern(regexp = "([0-9A-Z]{2}[.]?[0-9A-Z]{3}[.]?[0-9A-Z]{3}[/]?[0-9A-Z]{4}[-]?[0-9]{2})") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this pattern now depends on the CNPJ's version/format, I'd suggest moving it into the CNPJ validator itself and picking the pattern that suits the format. |
||||||
@ReportAsSingleViolation | ||||||
@Documented | ||||||
@Constraint(validatedBy = { }) | ||||||
|
@@ -45,6 +45,8 @@ | |||||
|
||||||
Class<? extends Payload>[] payload() default { }; | ||||||
|
||||||
boolean alphanumeric() default false; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't an enum here be more future-proof option? something like
Suggested change
enum FormatRevision {
// Obviously, the constant names have to be more meaningful
// and connected to the versions of current and new formats.
V1, V2;
} This way, if another version is published, it can be added without any new parameters on the constraint annotation... |
||||||
|
||||||
/** | ||||||
* Defines several {@code @CNPJ} annotations on the same element. | ||||||
*/ | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,8 +29,10 @@ public abstract class ModCheckBase { | |
private static final Log LOG = LoggerFactory.make( MethodHandles.lookup() ); | ||
|
||
private static final Pattern NUMBERS_ONLY_REGEXP = Pattern.compile( "[^0-9]" ); | ||
private static final Pattern NUMBERS_UPPER_LETTERS_ONLY_REGEXP = Pattern.compile( "[^0-9A-Z]" ); | ||
|
||
private static final int DEC_RADIX = 10; | ||
private static final int BASE_CHAR_INDEX = 48; | ||
|
||
/** | ||
* The start index for the checksum calculation | ||
|
@@ -47,7 +49,8 @@ public abstract class ModCheckBase { | |
*/ | ||
private int checkDigitIndex; | ||
|
||
private boolean ignoreNonDigitCharacters; | ||
private boolean ignoreNonValidCharacters; | ||
private boolean alphanumeric; | ||
|
||
public boolean isValid(final CharSequence value, final ConstraintValidatorContext context) { | ||
if ( value == null ) { | ||
|
@@ -79,11 +82,22 @@ public boolean isValid(final CharSequence value, final ConstraintValidatorContex | |
|
||
public abstract boolean isCheckDigitValid(List<Integer> digits, char checkDigit); | ||
|
||
protected void initialize(int startIndex, int endIndex, int checkDigitIndex, boolean ignoreNonDigitCharacters) { | ||
protected void initialize(int startIndex, int endIndex, int checkDigitIndex, boolean ignoreNonValidCharacters) { | ||
this.startIndex = startIndex; | ||
this.endIndex = endIndex; | ||
this.checkDigitIndex = checkDigitIndex; | ||
this.ignoreNonDigitCharacters = ignoreNonDigitCharacters; | ||
this.ignoreNonValidCharacters = ignoreNonValidCharacters; | ||
this.alphanumeric = false; | ||
|
||
this.validateOptions(); | ||
} | ||
Comment on lines
+85
to
+93
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe just call initialize(startIndex, endIndex, checkDigitIndex, ignoreNonValidCharacters, false) and also if you'll end up still needing to modify the |
||
|
||
protected void initialize(int startIndex, int endIndex, int checkDigitIndex, boolean ignoreNonValidCharacters, boolean alphanumeric) { | ||
this.startIndex = startIndex; | ||
this.endIndex = endIndex; | ||
this.checkDigitIndex = checkDigitIndex; | ||
this.ignoreNonValidCharacters = ignoreNonValidCharacters; | ||
this.alphanumeric = alphanumeric; | ||
|
||
this.validateOptions(); | ||
} | ||
|
@@ -98,11 +112,21 @@ protected void initialize(int startIndex, int endIndex, int checkDigitIndex, boo | |
* @throws NumberFormatException in case character is not a digit | ||
*/ | ||
protected int extractDigit(char value) throws NumberFormatException { | ||
if ( Character.isDigit( value ) ) { | ||
return Character.digit( value, DEC_RADIX ); | ||
if ( alphanumeric ) { | ||
if ( (value >= '0' && value <= '9' ) || (value >= 'A' && value <= 'Z') ) { | ||
return value - BASE_CHAR_INDEX; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This way of converting seems somewhat specific to CNPJ, I'd suggest creating an extended version of |
||
} | ||
else { | ||
throw LOG.getCharacterIsNotADigitOrUpperCaseLetterException( value ); | ||
} | ||
} | ||
else { | ||
throw LOG.getCharacterIsNotADigitException( value ); | ||
if ( Character.isDigit( value ) ) { | ||
return Character.digit( value, DEC_RADIX ); | ||
} | ||
else { | ||
throw LOG.getCharacterIsNotADigitException( value ); | ||
} | ||
} | ||
} | ||
|
||
|
@@ -145,8 +169,13 @@ private boolean validateOptions() { | |
} | ||
|
||
private String stripNonDigitsIfRequired(String value) { | ||
if ( ignoreNonDigitCharacters ) { | ||
return NUMBERS_ONLY_REGEXP.matcher( value ).replaceAll( "" ); | ||
if ( ignoreNonValidCharacters ) { | ||
if ( alphanumeric ) { | ||
return NUMBERS_UPPER_LETTERS_ONLY_REGEXP.matcher( value ).replaceAll( "" ); | ||
} | ||
else { | ||
return NUMBERS_ONLY_REGEXP.matcher( value ).replaceAll( "" ); | ||
} | ||
} | ||
else { | ||
return value; | ||
|
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.
I'd suggest limit the changes to CNPJ constraint only