Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
@accountable/jsvat
Advanced tools
Readme
@accountable/jsvat
Check the validity of the format of a VAT number. No dependencies
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.
Small library to check validity VAT numbers (European + some others counties). (learn more about VAT)
via npm:
npm i @accountable/jsvat
via yarn:
yarn add @accountable/jsvat
import { checkVAT, belgium, austria } from '@accountable/jsvat';
checkVAT('BE0411905847', [belgium]); // true: accept only Belgium VATs
checkVAT('BE0411905847', [belgium, austria]); // true: accept only Belgium or Austria VATs
checkVAT('BE0411905847', [austria]); // false: accept only Austria VATs
import { checkVAT } from '@accountable/jsvat';
checkVAT('BE0411905847'); // no need to pass countries as all supported countries will be used as default behavior
import { checkVAT, countriesMap } from '@accountable/jsvat';
const { france, germany, ...countriesToValidate } = countriesMap;
checkVAT('BE0411905847', countriesToValidate);
checkVAT()
returns VatCheckResult
object:
export interface VatCheckResult {
value?: string; // 'BE0411905847': your VAT without extra characters (like '-', spaces, etc)
isValid: boolean; // The main result. Indicates if VAT is correct against provided countries or not
isValidFormat: boolean; // Indicates the validation of the format of VAT only. E.g. "BE0411905847" is a valid VAT, and "BE0897221791" is not. But they both has valid format, so "isValidFormat" will return "true"
isSupportedCountry: boolean; // Indicates if "jsvat" could recognize the VAT. Sometimes you want to understand - if it's an invalid VAT from supported country or from an unknown one
country?: {
// VAT's country (null if not found). By "supported" I mean imported.
name: string; // ISO country name of VAT
isoCode: {
// Country ISO codes
short: string;
long: string;
numeric: string;
};
};
}
You can add your own country.
In general Country
should implement following structure:
interface Country {
name: string;
codes: ReadonlyArray<string>;
rules: {
multipliers: {}; // you can leave it empty
regex: ReadonlyArray<RegExp>;
};
} & ({
calcFn: (vat: string, options?: object) => boolean; // use this if you want to check only format of VAT
} | {
calcWithFormatFn: (vat: string, options?: object) => {isValid: boolean, vat: string}; // use this if you want to check format of VAT and re-format it
})
Example:
import { checkVAT } from '@accountable/jsvat';
export const wonderland = {
name: 'Wonderland',
codes: ['WD', 'WDR', '999'], // This codes should follow ISO standards (short, long and numeric), but it's your own business
calcFn: (vat) => {
return vat.length === 10;
},
rules: {
regex: [/^(WD)(\d{8})$/],
},
};
checkVAT('WD12345678', [wonderland]); // true
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:
// Modern Frontend and Node
const { checkVAT, belgium, austria } = require('@accountable/jsvat');
// Node.js
const { checkVAT, belgium, austria } = require('@accountable/jsvat');
// Legacy Frontend
<script src='whatever/jsvat/lib/umd/index.js'></script>;
Alternatively you can specify which module system you do want, e.g.:
// CommonJS (i.g nodejs)
const { checkVAT, belgium, austria } = require('jsvat/lib/commonjs');
// ES6
import { checkVAT, belgium, austria } from 'jsvat/lib/es6';
@accountable/jsvat
checks validity?There is 2-step check:
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.
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.
FAQs
Check the validity of the format of a VAT number
The npm package @accountable/jsvat receives a total of 232 weekly downloads. As such, @accountable/jsvat popularity was classified as not popular.
We found that @accountable/jsvat demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.