Skip to content

Commit

Permalink
fix(isVAT): improved ABN (AU VAT) validation (#2343)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewberryman authored Apr 27, 2024
1 parent 43a0f09 commit 83d6ffd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
18 changes: 17 additions & 1 deletion src/lib/isVAT.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down Expand Up @@ -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),
Expand Down
18 changes: 16 additions & 2 deletions test/validators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
Expand Down

0 comments on commit 83d6ffd

Please sign in to comment.