Skip to content

Commit

Permalink
Assign check digit to 0 if remainder is 10 (validatorjs#2492)
Browse files Browse the repository at this point in the history
  • Loading branch information
joelcuy authored Nov 12, 2024
1 parent fc31e6e commit a066cd2
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/lib/isISO6346.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export function isISO6346(str) {
} else sum += str[i] * (2 ** i);
}

const checkSumDigit = sum % 11;
let checkSumDigit = sum % 11;
if (checkSumDigit === 10) checkSumDigit = 0;
return Number(str[str.length - 1]) === checkSumDigit;
}

Expand Down
49 changes: 49 additions & 0 deletions test/validators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13066,6 +13066,55 @@ describe('Validators', () => {
});
});

it('should validate ISO6346 shipping container IDs with checksum digit 10 represented as 0', () => {
test({
validator: 'isISO6346',
valid: [
'APZU3789870',
'TEMU1002030',
'DFSU1704420',
'CMAU2221480',
'SEGU5060260',
'FCIU8939320',
'TRHU3495670',
'MEDU3871410',
'CMAU2184010',
'TCLU2265970',
],
invalid: [
'APZU3789871', // Incorrect check digit
'TEMU1002031',
'DFSU1704421',
'CMAU2221481',
'SEGU5060261',
],
});
});
it('should validate ISO6346 shipping container IDs with checksum digit 10 represented as 0', () => {
test({
validator: 'isFreightContainerID',
valid: [
'APZU3789870',
'TEMU1002030',
'DFSU1704420',
'CMAU2221480',
'SEGU5060260',
'FCIU8939320',
'TRHU3495670',
'MEDU3871410',
'CMAU2184010',
'TCLU2265970',
],
invalid: [
'APZU3789871', // Incorrect check digit
'TEMU1002031',
'DFSU1704421',
'CMAU2221481',
'SEGU5060261',
],
});
});

// EU-UK valid numbers sourced from https://ec.europa.eu/taxation_customs/tin/specs/FS-TIN%20Algorithms-Public.docx or constructed by @tplessas.
it('should validate taxID', () => {
test({
Expand Down

0 comments on commit a066cd2

Please sign in to comment.