diff --git a/README.md b/README.md index 49c2bf0d0..d56a3433c 100644 --- a/README.md +++ b/README.md @@ -102,7 +102,6 @@ Validator | Description **isBoolean(str [, options])** | check if a string is a boolean.
`options` is an object which defaults to `{ loose: false }`. If loose is is set to false, the validator will strictly match ['true', 'false', '0', '1']. If loose is set to true, the validator will also match 'yes', 'no', and will match a valid boolean string of any case. (eg: ['true', 'True', 'TRUE']). **isBtcAddress(str)** | check if the string is a valid BTC address. **isByteLength(str [, options])** | check if the string's length (in UTF-8 bytes) falls in a range.

`options` is an object which defaults to `{min:0, max: undefined}`. -**isContainerID(str)** | check if the string is an ISO6346 shipping container identification. **isCreditCard(card, [, options])** | check if the string is a credit card.

options is an optional object that can be supplied with the following key(s): `provider` is an optional key whose value should be a string, and defines the company issuing the credit card. Valid values include `amex` , `dinersclub` , `discover` , `jcb` , `mastercard` , `unionpay` , `visa` or blank will check for any provider. **isCurrency(str [, options])** | check if the string is a valid currency amount.

`options` is an object which defaults to `{symbol: '$', require_symbol: false, allow_space_after_symbol: false, symbol_after_digits: false, allow_negatives: true, parens_for_negatives: false, negative_sign_before_digits: false, negative_sign_after_digits: false, allow_negative_sign_placeholder: false, thousands_separator: ',', decimal_separator: '.', allow_decimal: true, require_decimal: false, digits_after_decimal: [2], allow_space_after_digits: false}`.
**Note:** The array `digits_after_decimal` is filled with the exact number of digits allowed not a range, for example a range 1 to 3 will be given as [1, 2, 3]. **isDataURI(str)** | check if the string is a [data uri format](https://developer.mozilla.org/en-US/docs/Web/HTTP/data_URIs). @@ -130,6 +129,7 @@ Validator | Description **isIPRange(str [, version])** | check if the string is an IP Range (version 4 or 6). **isISBN(str [, version])** | check if the string is an ISBN (version 10 or 13). **isISIN(str)** | check if the string is an [ISIN][ISIN] (stock/security identifier). +**isISO6346(str)** | check if the string is a valid [ISO 6346](https://en.wikipedia.org/wiki/ISO_6346) shipping container identification. **isISO6391(str)** | check if the string is a valid [ISO 639-1](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) language code. **isISO8601(str [, options])** | check if the string is a valid [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) date.
`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. **isISO31661Alpha2(str)** | check if the string is a valid [ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) officially assigned country code. diff --git a/src/index.js b/src/index.js index 2f2e460d5..bebf42239 100644 --- a/src/index.js +++ b/src/index.js @@ -78,7 +78,6 @@ import isISIN from './lib/isISIN'; import isISBN from './lib/isISBN'; import isISSN from './lib/isISSN'; import isTaxID from './lib/isTaxID'; -import isContainerID from './lib/isContainerID'; import isMobilePhone, { locales as isMobilePhoneLocales } from './lib/isMobilePhone'; @@ -88,6 +87,7 @@ import isCurrency from './lib/isCurrency'; import isBtcAddress from './lib/isBtcAddress'; +import isISO6346 from './lib/isISO6346'; import isISO6391 from './lib/isISO6391'; import isISO8601 from './lib/isISO8601'; import isRFC3339 from './lib/isRFC3339'; @@ -196,10 +196,10 @@ const validator = { isMobilePhoneLocales, isPostalCode, isPostalCodeLocales, - isContainerID, isEthereumAddress, isCurrency, isBtcAddress, + isISO6346, isISO6391, isISO8601, isRFC3339, diff --git a/src/lib/isContainerID.js b/src/lib/isContainerID.js deleted file mode 100644 index 98a7ac7a6..000000000 --- a/src/lib/isContainerID.js +++ /dev/null @@ -1,28 +0,0 @@ -import assertString from './util/assertString'; - -// https://en.wikipedia.org/wiki/ISO_6346 -const isContainerIdReg = new RegExp('^[A-Z]{3}U[0-9]{7}$'); -const isDigit = new RegExp('^[0-9]{1}'); - -export default function isContainerID(str) { - assertString(str); - str = str.trim(); - - if (!isContainerIdReg.test(str)) return false; - - let sum = 0; - for (let i = 0; i < str.length - 1; i++) { - if (!isDigit.test(str[i])) { - let convertedCode; - const letterCode = str.charCodeAt(i) - 55; - if (letterCode < 11) convertedCode = letterCode; - else if (letterCode >= 11 && letterCode <= 20) convertedCode = 12 + (letterCode % 11); - else if (letterCode >= 21 && letterCode <= 30) convertedCode = 23 + (letterCode % 21); - else convertedCode = 34 + (letterCode % 31); - sum += convertedCode * (2 ** i); - } else sum += str[i] * (2 ** i); - } - - const checkSumDigit = sum % 11; - return Number(str[str.length - 1]) === checkSumDigit; -} diff --git a/src/lib/isISO6346.js b/src/lib/isISO6346.js new file mode 100644 index 000000000..fe1dd2d55 --- /dev/null +++ b/src/lib/isISO6346.js @@ -0,0 +1,35 @@ +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 isDigit = /^[0-9]$/; + +export default function isISO6346(str) { + assertString(str); + str = str.trim(); + str = str.toUpperCase(); + + if (!isISO6346Str.test(str)) return false; + + if (str.length === 11) { + let sum = 0; + for (let i = 0; i < str.length - 1; i++) { + if (!isDigit.test(str[i])) { + let convertedCode; + const letterCode = str.charCodeAt(i) - 55; + if (letterCode < 11) convertedCode = letterCode; + else if (letterCode >= 11 && letterCode <= 20) convertedCode = 12 + (letterCode % 11); + else if (letterCode >= 21 && letterCode <= 30) convertedCode = 23 + (letterCode % 21); + else convertedCode = 34 + (letterCode % 31); + sum += convertedCode * (2 ** i); + } else sum += str[i] * (2 ** i); + } + + const checkSumDigit = sum % 11; + return Number(str[str.length - 1]) === checkSumDigit; + } + + return true; +} diff --git a/test/validators.js b/test/validators.js index 1d02c476f..c9069a6e0 100644 --- a/test/validators.js +++ b/test/validators.js @@ -11837,9 +11837,9 @@ describe('Validators', () => { }); }); - it('should validate containerID', () => { + it('should validate ISO6346 shipping containerID', () => { test({ - validator: 'isContainerID', + validator: 'isISO6346', valid: [ 'HLXU2008419', 'TGHU7599330', @@ -11850,6 +11850,7 @@ describe('Validators', () => { 'EMCU3811879', 'OOLU8643084', 'HJCU1922713', + 'QJRZ123456', ], invalid: [ 'OOLU1922713',