From 24c62889987df40340d74caa7d19f6d21c9458cd Mon Sep 17 00:00:00 2001 From: Noethix55555 <277300782+Noethix55555@users.noreply.github.com> Date: Tue, 16 Jun 2026 23:15:33 -0400 Subject: [PATCH] fix(isISO6346): anchor full pattern and drop stray comma 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]. --- src/lib/isISO6346.js | 2 +- test/validators.test.js | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/lib/isISO6346.js b/src/lib/isISO6346.js index 2c28c1123..cb6ac7b32 100644 --- a/src/lib/isISO6346.js +++ b/src/lib/isISO6346.js @@ -3,7 +3,7 @@ import assertString from './util/assertString'; // https://en.wikipedia.org/wiki/ISO_6346 // according to ISO6346 standard, checksum digit is mandatory for freight container but recommended // for other container types (J and Z) -const isISO6346Str = /^[A-Z]{3}(U[0-9]{7})|([J,Z][0-9]{6,7})$/; +const isISO6346Str = /^[A-Z]{3}(U[0-9]{7}|[JZ][0-9]{6,7})$/; const isDigit = /^[0-9]$/; export function isISO6346(str) { diff --git a/test/validators.test.js b/test/validators.test.js index a4c3d7193..5c0d424d0 100644 --- a/test/validators.test.js +++ b/test/validators.test.js @@ -13537,6 +13537,10 @@ describe('Validators', () => { 'ECMJ4657496', 'TBJA7176445', 'AFFU5962593', + 'ABCU1234567HELLO', + 'CSQU3054383XXX', + 'hellozZ123456', + 'AB,123456', ], }); }); @@ -13562,6 +13566,10 @@ describe('Validators', () => { 'ECMJ4657496', 'TBJA7176445', 'AFFU5962593', + 'ABCU1234567HELLO', + 'CSQU3054383XXX', + 'hellozZ123456', + 'AB,123456', ], }); });