From 83d6ffd7154e0711b340791b3c8d6daca8094c46 Mon Sep 17 00:00:00 2001 From: Matthew Berryman Date: Sun, 28 Apr 2024 00:04:36 +0930 Subject: [PATCH] fix(isVAT): improved ABN (AU VAT) validation (#2343) --- src/lib/isVAT.js | 18 +++++++++++++++++- test/validators.test.js | 18 ++++++++++++++++-- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/lib/isVAT.js b/src/lib/isVAT.js index a8e2611b6..50fcf52e0 100644 --- a/src/lib/isVAT.js +++ b/src/lib/isVAT.js @@ -1,6 +1,22 @@ import assertString from './util/assertString'; import * as algorithms from './util/algorithms'; +const AU = (str) => { + const match = str.match(/^(AU)?(\d{11})$/); + if (!match) { + return false; + } + // @see {@link https://abr.business.gov.au/Help/AbnFormat} + const weights = [10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19]; + str = str.replace(/^AU/, ''); + const ABN = (parseInt(str.slice(0, 1), 10) - 1).toString() + str.slice(1); + let total = 0; + for (let i = 0; i < 11; i++) { + total += weights[i] * ABN.charAt(i); + } + return (total !== 0 && total % 89 === 0); +}; + const CH = (str) => { // @see {@link https://www.ech.ch/de/ech/ech-0097/5.2.0} const hasValidCheckNumber = (digits) => { @@ -68,7 +84,7 @@ export const vatMatchers = { */ AL: str => /^(AL)?\w{9}[A-Z]$/.test(str), MK: str => /^(MK)?\d{13}$/.test(str), - AU: str => /^(AU)?\d{11}$/.test(str), + AU, BY: str => /^(УНП )?\d{9}$/.test(str), CA: str => /^(CA)?\d{9}$/.test(str), IS: str => /^(IS)?\d{5,6}$/.test(str), diff --git a/test/validators.test.js b/test/validators.test.js index 8462c0f1d..3928a1c5c 100644 --- a/test/validators.test.js +++ b/test/validators.test.js @@ -14105,10 +14105,24 @@ describe('Validators', () => { validator: 'isVAT', args: ['AU'], valid: [ + 'AU53004085616', + '53004085616', + 'AU65613309809', + '65613309809', + 'AU34118972998', + '34118972998', + ], + invalid: [ + 'AU65613309808', + '65613309808', + 'AU55613309809', + '55613309809', + 'AU65613319809', + '65613319809', + 'AU34117972998', + '34117972998', 'AU12345678901', '12345678901', - ], - invalid: [ 'AU 12345678901', '1234567890', ],