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

Add option to strict VAT ID validation check #133

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 5 additions & 4 deletions lib/es6/lib/jsvat.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,12 @@ function isVatValid(vat, country) {
return false;
return country.calcFn(regexResult[2]);
}
export function checkVAT(vat, countriesList = []) {
export function checkVAT(vat, countriesList = [], shouldRemoveExtraChars = true) {
if (!vat)
return makeResult(vat, false);
const cleanVAT = removeExtraChars(vat);
const country = getCountry(cleanVAT, countriesList);
const isValid = country ? isVatValid(cleanVAT, country) : false;
return makeResult(cleanVAT, isValid, country);
const vatId = shouldRemoveExtraChars ? cleanVAT : vat;
const country = getCountry(vatId, countriesList);
const isValid = country ? isVatValid(vatId, country) : false;
return makeResult(vatId, isValid, country);
}
13 changes: 9 additions & 4 deletions src/lib/jsvat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,15 @@ function isVatValid(vat: string, country: Country): boolean {
return country.calcFn(regexResult[2]);
}

export function checkVAT(vat: string, countriesList: ReadonlyArray<Country> = []): VatCheckResult {
export function checkVAT(
vat: string,
countriesList: ReadonlyArray<Country> = [],
shouldRemoveExtraChars: boolean = true
): VatCheckResult {
if (!vat) return makeResult(vat, false);
const cleanVAT = removeExtraChars(vat);
const country = getCountry(cleanVAT, countriesList);
const isValid = country ? isVatValid(cleanVAT, country) : false;
return makeResult(cleanVAT, isValid, country);
const vatId = shouldRemoveExtraChars ? cleanVAT : vat;
const country = getCountry(vatId, countriesList);
const isValid = country ? isVatValid(vatId, country) : false;
return makeResult(vatId, isValid, country);
}
20 changes: 19 additions & 1 deletion test/germany.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { germany } from '../index';
import { codes, invalid, name, valid, validOnlyByFormat } from './countries_vat_lists/germany.vat';
import { addCharsToString, checkInvalidVat, checkOnlyValidFormatVat, checkValidVat } from './utils';
import {
addCharsToString,
checkInvalidVat,
checkOnlyValidFormatVat,
checkValidVat,
checkValidVatWithoutRemovingExtraChars
} from './utils';

describe('Germany', () => {
it('should return "true" result for valid VATs', () => {
Expand All @@ -15,10 +21,22 @@ describe('Germany', () => {
valid.map((vat) => addCharsToString(vat, '-')).forEach((vat) => checkValidVat(vat, [germany], codes, name));
});

it('should return "false" result for valid VATs with extra dash characters when shouldRemoveExtraChars is set to false', () => {
valid
.map((vat) => addCharsToString(vat, '-'))
.forEach((vat) => checkValidVatWithoutRemovingExtraChars(vat, [germany], codes, name));
});

it('should return "true" result for valid VATs with extra space characters', () => {
valid.map((vat) => addCharsToString(vat, ' ')).forEach((vat) => checkValidVat(vat, [germany], codes, name));
});

it('should return "false" result for valid VATs with extra space characters when shouldRemoveExtraChars is set to false', () => {
valid
.map((vat) => addCharsToString(vat, ' '))
.forEach((vat) => checkValidVatWithoutRemovingExtraChars(vat, [germany], codes, name));
});

it('should return "false" result for invalid VATs', () => {
invalid.forEach((vat) => checkInvalidVat(vat, [germany]));
});
Expand Down
7 changes: 7 additions & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ export function checkValidVat(vat, countriesList, codes, name) {
expect(result.country.isoCode.numeric).toBe(codes[2]);
}

export function checkValidVatWithoutRemovingExtraChars(vat, countriesList, codes, name) {
const result = checkVAT(vat, countriesList, false);

if (result.isValid) console.info('Following VAT should be invalid:', vat);
expect(result.isValid).toBe(false);
}

export function checkInvalidVat(vat, countriesList) {
const result = checkVAT(vat, countriesList);
if (result.isValid) console.info('Following VAT should be invalid:', vat);
Expand Down