@accountable/jsvat
Check the validity of the format of a VAT number. No dependencies
Disclaimer
This library is a fork of jsvat which in no longer maintained and all new pull requests to it are ignored for long time.
So in Accountable, we thought it would be great to add support for the new VAT number format and keep the library up to date and easily maintained as it is part of our day to day work.
What is it?
Small library to check validity VAT numbers (European + some others counties). (learn more about VAT)
- Rewritten into Typescript :new:
- No dependencies
- No http calls
- 2-step checks: math + regexp
- Tree-shakeable
- Extendable
- Separate checks for valid VAT and valid VAT format
- Dynamically add/remove countries with which you want to check the VAT
- Detecting possible country before you finish
- Typescript
Installation
via npm:
npm i @accountable/jsvat
via yarn:
yarn add @accountable/jsvat
Getting Started
- check against specific countries
import { checkVAT, belgium, austria } from '@accountable/jsvat';
checkVAT('BE0411905847', [belgium]);
checkVAT('BE0411905847', [belgium, austria]);
checkVAT('BE0411905847', [austria]);
- check against all supported countries
import { checkVAT } from '@accountable/jsvat';
checkVAT('BE0411905847');
- check against all supported countries except specific ones
import { checkVAT, countriesMap } from '@accountable/jsvat';
const { france, germany, ...countriesToValidate } = countriesMap;
checkVAT('BE0411905847', countriesToValidate);
Return value
checkVAT()
returns VatCheckResult
object:
export interface VatCheckResult {
value?: string;
isValid: boolean;
isValidFormat: boolean;
isSupportedCountry: boolean;
country?: {
name: string;
isoCode: {
short: string;
long: string;
numeric: string;
};
};
}
List of supported Countries:
- 🇦🇩 Andorra
- 🇦🇹 Austria
- 🇧🇪 Belgium
- 🇧🇷 Brazil
- 🇧🇬 Bulgaria
- 🇭🇷 Croatia
- 🇨🇾 Cyprus
- 🇨🇿 Czech republic
- 🇩🇰 Denmark
- 🇪🇪 Estonia
- 🇪🇺 Europe
- 🇫🇮 Finland
- 🇫🇷 France
- 🇩🇪 Germany
- 🇬🇷 Greece
- 🇭🇺 Hungary
- 🇮🇪 Ireland
- 🇮🇹 Italy
- 🇱🇻 Latvia
- 🇱🇹 Lithuania
- 🇱🇺 Luxembourg
- 🇲🇹 Malta
- 🇳🇱 Netherlands
- 🇳🇴 Norway
- 🇵🇱 Poland
- 🇵🇹 Portugal
- 🇷🇴 Romania
- 🇷🇺 Russia
- 🇷🇸 Serbia
- 🇸🇰 Slovakia Republic
- 🇸🇮 Slovenia
- 🇪🇸 Spain
- 🇸🇪 Sweden
- 🇬🇧 United Kingdom
- 🇨🇭 Switzerland
Extend countries list - add your own country
You can add your own country.
In general Country
should implement following structure:
interface Country {
name: string;
codes: ReadonlyArray<string>;
rules: {
multipliers: {};
regex: ReadonlyArray<RegExp>;
};
} & ({
calcFn: (vat: string, options?: object) => boolean;
} | {
calcWithFormatFn: (vat: string, options?: object) => {isValid: boolean, vat: string};
})
Example:
import { checkVAT } from '@accountable/jsvat';
export const wonderland = {
name: 'Wonderland',
codes: ['WD', 'WDR', '999'],
calcFn: (vat) => {
return vat.length === 10;
},
rules: {
regex: [/^(WD)(\d{8})$/],
},
};
checkVAT('WD12345678', [wonderland]);
About modules... ES6 / CommonJS
jsvat build includes es6
, commonjs
, amd
, umd
and system
builds at the same time.
By default you will stick to es6
version for browsers and build tools (webpack, etc):
which expects you to import it as
import { checkVAT, belgium, austria } from '@accountable/jsvat';
Node.js automatically will pick up CommonJS
version by default.
Means you could import it like:
const { checkVAT, belgium, austria } = require('@accountable/jsvat');
const { checkVAT, belgium, austria } = require('@accountable/jsvat');
<script src='whatever/jsvat/lib/umd/index.js'></script>;
Alternatively you can specify which module system you do want, e.g.:
const { checkVAT, belgium, austria } = require('jsvat/lib/commonjs');
import { checkVAT, belgium, austria } from 'jsvat/lib/es6';
How @accountable/jsvat
checks validity?
There is 2-step check:
- Compare with list of Regexps;
For example regexp for austria is /^(AT)U(\d{8})$/
.
Looks like ATU99999999
is valid (it's satisfy the regexp), but actually it's should be invalid.
- Some magic mathematical counting;
Here we make some mathematical calculation (different for each country).
After that we may be sure that ATU99999999
and for example ATV66889218
isn't valid, but ATU12011204
is valid.
Note: VAT numbers of some countries should ends up with special characters. Like '01' for Sweden or "L" for Cyprus. If 100% real VAT doesn't fit, try to add proper appendix.