Skip to content

Commit 78f25ba

Browse files
authoredFeb 8, 2023
feat(isFreightContainerID): add new validator (#2144)
1 parent 54d330c commit 78f25ba

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed
 

‎README.md

+2
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ Validator | Description
114114
**isEthereumAddress(str)** | check if the string is an [Ethereum][Ethereum] address. Does not validate address checksums.
115115
**isFloat(str [, options])** | check if the string is a float.<br/><br/>`options` is an object which can contain the keys `min`, `max`, `gt`, and/or `lt` to validate the float is within boundaries (e.g. `{ min: 7.22, max: 9.55 }`) it also has `locale` as an option.<br/><br/>`min` and `max` are equivalent to 'greater or equal' and 'less or equal', respectively while `gt` and `lt` are their strict counterparts.<br/><br/>`locale` determines the decimal separator and is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fr-CA', 'fr-FR', 'hu-HU', 'it-IT', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`. Locale list is `validator.isFloatLocales`.
116116
**isFQDN(str [, options])** | check if the string is a fully qualified domain name (e.g. domain.com).<br/><br/>`options` is an object which defaults to `{ require_tld: true, allow_underscores: false, allow_trailing_dot: false, allow_numeric_tld: false, allow_wildcard: false, ignore_max_length: false }`. If `allow_wildcard` is set to true, the validator will allow domain starting with `*.` (e.g. `*.example.com` or `*.shop.example.com`).
117+
**isFreightContainerID(str)** | alias for `isISO6346`, check if the string is a valid [ISO 6346](https://en.wikipedia.org/wiki/ISO_6346) shipping container identification.
117118
**isFullWidth(str)** | check if the string contains any full-width chars.
118119
**isHalfWidth(str)** | check if the string contains any half-width chars.
119120
**isHash(str, algorithm)** | check if the string is a hash of type algorithm.<br/><br/>Algorithm is one of `['crc32', 'crc32b', 'md4', 'md5', 'ripemd128', 'ripemd160', 'sha1', 'sha256', 'sha384', 'sha512', 'tiger128', 'tiger160', 'tiger192']`.
@@ -129,6 +130,7 @@ Validator | Description
129130
**isIPRange(str [, version])** | check if the string is an IP Range (version 4 or 6).
130131
**isISBN(str [, options])** | check if the string is an [ISBN][ISBN].<br/><br/>`options` is an object that has no default.<br/>**Options:**<br/>`version`: ISBN version to compare to. Accepted values are '10' and '13'. If none provided, both will be tested.
131132
**isISIN(str)** | check if the string is an [ISIN][ISIN] (stock/security identifier).
133+
**isISO6346(str)** | check if the string is a valid [ISO 6346](https://en.wikipedia.org/wiki/ISO_6346) shipping container identification.
132134
**isISO6391(str)** | check if the string is a valid [ISO 639-1][ISO 639-1] language code.
133135
**isISO8601(str [, options])** | check if the string is a valid [ISO 8601][ISO 8601] date. <br/>`options` is an object which defaults to `{ strict: false, strictSeparator: false }`. If `strict` is true, date strings with invalid dates like `2009-02-29` will be invalid. If `strictSeparator` is true, date strings with date and time separated by anything other than a T will be invalid.
134136
**isISO31661Alpha2(str)** | check if the string is a valid [ISO 3166-1 alpha-2][ISO 3166-1 alpha-2] officially assigned country code.

‎src/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ import isCurrency from './lib/isCurrency';
8888

8989
import isBtcAddress from './lib/isBtcAddress';
9090

91+
import { isISO6346, isFreightContainerID } from './lib/isISO6346';
9192
import isISO6391 from './lib/isISO6391';
9293
import isISO8601 from './lib/isISO8601';
9394
import isRFC3339 from './lib/isRFC3339';
@@ -199,6 +200,8 @@ const validator = {
199200
isEthereumAddress,
200201
isCurrency,
201202
isBtcAddress,
203+
isISO6346,
204+
isFreightContainerID,
202205
isISO6391,
203206
isISO8601,
204207
isRFC3339,

‎src/lib/isISO6346.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import assertString from './util/assertString';
2+
3+
// https://en.wikipedia.org/wiki/ISO_6346
4+
// according to ISO6346 standard, checksum digit is mandatory for freight container but recommended
5+
// for other container types (J and Z)
6+
const isISO6346Str = /^[A-Z]{3}(U[0-9]{7})|([J,Z][0-9]{6,7})$/;
7+
const isDigit = /^[0-9]$/;
8+
9+
export function isISO6346(str) {
10+
assertString(str);
11+
12+
str = str.toUpperCase();
13+
14+
if (!isISO6346Str.test(str)) return false;
15+
16+
if (str.length === 11) {
17+
let sum = 0;
18+
for (let i = 0; i < str.length - 1; i++) {
19+
if (!isDigit.test(str[i])) {
20+
let convertedCode;
21+
const letterCode = str.charCodeAt(i) - 55;
22+
if (letterCode < 11) convertedCode = letterCode;
23+
else if (letterCode >= 11 && letterCode <= 20) convertedCode = 12 + (letterCode % 11);
24+
else if (letterCode >= 21 && letterCode <= 30) convertedCode = 23 + (letterCode % 21);
25+
else convertedCode = 34 + (letterCode % 31);
26+
sum += convertedCode * (2 ** i);
27+
} else sum += str[i] * (2 ** i);
28+
}
29+
30+
const checkSumDigit = sum % 11;
31+
return Number(str[str.length - 1]) === checkSumDigit;
32+
}
33+
34+
return true;
35+
}
36+
37+
export const isFreightContainerID = isISO6346;

‎test/validators.test.js

+52
Original file line numberDiff line numberDiff line change
@@ -11946,6 +11946,58 @@ describe('Validators', () => {
1194611946
});
1194711947
});
1194811948

11949+
11950+
it('should validate ISO6346 shipping containerID', () => {
11951+
test({
11952+
validator: 'isISO6346',
11953+
valid: [
11954+
'HLXU2008419',
11955+
'TGHU7599330',
11956+
'ECMU4657496',
11957+
'MEDU6246078',
11958+
'YMLU2809976',
11959+
'MRKU0046221',
11960+
'EMCU3811879',
11961+
'OOLU8643084',
11962+
'HJCU1922713',
11963+
'QJRZ123456',
11964+
],
11965+
invalid: [
11966+
'OOLU1922713',
11967+
'HJCU1922413',
11968+
'FCUI985619',
11969+
'ECMJ4657496',
11970+
'TBJA7176445',
11971+
'AFFU5962593',
11972+
],
11973+
});
11974+
});
11975+
it('should validate ISO6346 shipping containerID', () => {
11976+
test({
11977+
validator: 'isFreightContainerID',
11978+
valid: [
11979+
'HLXU2008419',
11980+
'TGHU7599330',
11981+
'ECMU4657496',
11982+
'MEDU6246078',
11983+
'YMLU2809976',
11984+
'MRKU0046221',
11985+
'EMCU3811879',
11986+
'OOLU8643084',
11987+
'HJCU1922713',
11988+
'QJRZ123456',
11989+
],
11990+
invalid: [
11991+
'OOLU1922713',
11992+
'HJCU1922413',
11993+
'FCUI985619',
11994+
'ECMJ4657496',
11995+
'TBJA7176445',
11996+
'AFFU5962593',
11997+
],
11998+
});
11999+
});
12000+
1194912001
// EU-UK valid numbers sourced from https://ec.europa.eu/taxation_customs/tin/specs/FS-TIN%20Algorithms-Public.docx or constructed by @tplessas.
1195012002
it('should validate taxID', () => {
1195112003
test({

0 commit comments

Comments
 (0)
Please sign in to comment.