Skip to content

fix(isISO6346): anchor full pattern and drop stray comma#2773

Open
Noethix55555 wants to merge 1 commit into
validatorjs:masterfrom
Noethix55555:fix/iso6346-anchor
Open

fix(isISO6346): anchor full pattern and drop stray comma#2773
Noethix55555 wants to merge 1 commit into
validatorjs:masterfrom
Noethix55555:fix/iso6346-anchor

Conversation

@Noethix55555

Copy link
Copy Markdown

Fixes #2772.

The regex in src/lib/isISO6346.js did not group the alternation, so ^ anchored only the first branch and $ only the second. The first branch matched any string starting with the owner prefix, U and 7 digits while ignoring trailing characters; the second matched any string ending with J/Z and 6-7 digits while ignoring the leading owner prefix. [J,Z] also accepted a literal comma. Strings whose length is not 11 skip the checksum and return true, so these malformed inputs passed.

isISO6346('ABCU1234567HELLO'); // was true
isISO6346('CSQU3054383XXX');   // was true
isISO6346('hellozZ123456');    // was true
isISO6346('AB,123456');        // was true

The fix groups the alternation inside the anchors and uses [JZ]:

const isISO6346Str = /^[A-Z]{3}(U[0-9]{7}|[JZ][0-9]{6,7})$/;

All existing valid and invalid fixtures still pass. Added the four cases above to the invalid fixtures for both isISO6346 and isFreightContainerID; they fail on the previous regex and pass with the fix.

The regex /^[A-Z]{3}(U[0-9]{7})|([J,Z][0-9]{6,7})$/ did not group the
alternation, so ^ anchored only the first branch and $ only the second.
The first branch matched any string starting with the owner prefix, U and
7 digits while ignoring trailing characters; the second matched any string
ending with J/Z and 6-7 digits while ignoring the leading owner prefix.
[J,Z] also accepted a literal comma. Strings whose length is not 11 skip
the checksum and return true, so these malformed inputs passed.

Group the alternation inside the anchors and use [JZ].
@codecov

codecov Bot commented Jun 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (7fdc788) to head (24c6288).

Additional details and impacted files
@@            Coverage Diff            @@
##            master     #2773   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          114       114           
  Lines         2587      2587           
  Branches       656       656           
=========================================
  Hits          2587      2587           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

isISO6346 and isFreightContainerID accept malformed strings

1 participant