Skip to content

Compare CAS and EC check digit against validated code, not raw input#398

Merged
garydgregory merged 1 commit into
apache:masterfrom
sahvx655-wq:cas-ec-validated-code
Jun 15, 2026
Merged

Compare CAS and EC check digit against validated code, not raw input#398
garydgregory merged 1 commit into
apache:masterfrom
sahvx655-wq:cas-ec-validated-code

Conversation

@sahvx655-wq

Copy link
Copy Markdown
Contributor

Both CASNumberCheckDigit.isValid and ECNumberCheckDigit.isValid pass the argument through REGEX_VALIDATOR, which is a CodeValidator and so trims the input and drops the dash separators before matching. The check digit is then read back from code.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:

  • Read the contribution guidelines for this project.
  • Read the ASF Generative Tooling Guidance if you use Artificial Intelligence (AI).
  • I used AI to create any part of, or all of, this pull request. Which AI tool was used to create this pull request, and to what extent did it contribute?
  • Run a successful build using the default Maven goal with mvn; that's mvn on the command line by itself.
  • Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied. This may not always be possible, but it is a best practice.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Each commit in the pull request should have a meaningful subject line and body. Note that a maintainer may squash commits during the merge process.

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 garydgregory changed the title compare CAS and EC check digit against validated code, not raw input Compare CAS and EC check digit against validated code, not raw input Jun 15, 2026
@garydgregory garydgregory merged commit 3dfbb3c into apache:master Jun 15, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants