Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(isVAT): improved ABN (AU GST/VAT ID) validation #2343

Merged
merged 1 commit into from
Apr 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -13936,10 +13936,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
Loading