Compare CAS and EC check digit against validated code, not raw input#398
Merged
Merged
Conversation
REGEX_VALIDATOR trims and reformats the input, so the check digit should be read from the validated string rather than the raw argument; otherwise a trailing space rejects an otherwise valid code while a leading space is accepted.
garydgregory
added a commit
that referenced
this pull request
Jun 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Both
CASNumberCheckDigit.isValidandECNumberCheckDigit.isValidpass the argument throughREGEX_VALIDATOR, which is aCodeValidatorand so trims the input and drops the dash separators before matching. The check digit is then read back fromcode.charAt(code.length() - 1)on the untouched argument. I tripped over this feeding values from a data column that still had trailing spaces:"7732-18-5 "is rejected while" 7732-18-5"passes, so validity ends up depending on which side a stray space happens to sit.The string returned by the validator is already trimmed and de-formatted, and its last character is the check digit, so the comparison belongs on that value rather than the raw argument. Reading both the modulus input and the check digit from the validated string removes the asymmetry and keeps the two routines consistent with the trimming the format validator already does everywhere else. Left as is, valid CAS and EC numbers are quietly rejected whenever the caller has not stripped trailing whitespace first.
Before you push a pull request, review this list:
mvn; that'smvnon the command line by itself.