Skip to content

fix(isISO6346): anchor the pattern and reject a literal comma - #2877

Open
yfwmaniish wants to merge 1 commit into
validatorjs:masterfrom
yfwmaniish:fix-iso6346-anchoring
Open

fix(isISO6346): anchor the pattern and reject a literal comma#2877
yfwmaniish wants to merge 1 commit into
validatorjs:masterfrom
yfwmaniish:fix-iso6346-anchoring

Conversation

@yfwmaniish

Copy link
Copy Markdown

Closes #2772.

Bug

isISO6346 (and its alias isFreightContainerID) accepts malformed strings because its regex has two flaws:

const isISO6346Str = /^[A-Z]{3}(U[0-9]{7})|([J,Z][0-9]{6,7})$/;
  1. Unanchored alternation. The | splits the whole pattern, so ^ anchors only the first branch and $ only the second. Anything starting with [A-Z]{3}U + 7 digits matches regardless of trailing junk, and anything ending with [JZ] + 6-7 digits matches regardless of leading junk.
  2. Literal comma in the class. [J,Z] matches J, Z, or ,.

Inputs whose length isn't 11 skip the checksum and return true, so malformed strings pass:

isISO6346('HLXU2008419HELLO'); // true  ❌ (trailing junk)
isISO6346('QJR,123456');       // true  ❌ (literal comma)

Fix

Group the alternation under the shared ^[A-Z]{3} prefix and end anchor, and drop the stray comma:

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

Tests

Added trailing-junk, leading-junk, and literal-comma cases to the invalid list. Verified against the full existing suite: all 10 valid IDs (incl. the QJRZ123456 J/Z 6-digit case) stay valid and all invalid IDs (incl. the 3 new ones) are rejected.

Copilot AI lite review requested due to automatic review settings September 6, 2026 17:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@codecov

codecov Bot commented Sep 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (a79ff98) to head (a972b6a).

Additional details and impacted files
@@            Coverage Diff            @@
##            master     #2877   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          114       114           
  Lines         2599      2599           
  Branches       658       658           
=========================================
  Hits          2599      2599           

☔ 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

2 participants