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

Implemented VAT validation support for english (en-GB) locale #1463

Merged
merged 5 commits into from
Nov 29, 2020
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ Validator | Description
**isURL(str [, options])** | check if the string is an URL.<br/><br/>`options` is an object which defaults to `{ protocols: ['http','https','ftp'], require_tld: true, require_protocol: false, require_host: true, require_valid_protocol: true, allow_underscores: false, host_whitelist: false, host_blacklist: false, allow_trailing_dot: false, allow_protocol_relative_urls: false, disallow_auth: false }`.<br/><br/>require_protocol - if set as true isURL will return false if protocol is not present in the URL.<br/>require_valid_protocol - isURL will check if the URL's protocol is present in the protocols option.<br/>protocols - valid protocols can be modified with this option.<br/>require_host - if set as false isURL will not check if host is present in the URL.<br/>require_port - if set as true isURL will check if port is present in the URL.<br/>allow_protocol_relative_urls - if set as true protocol relative URLs will be allowed.<br/>validate_length - if set as false isURL will skip string length validation (2083 characters is IE max URL length).
**isUUID(str [, version])** | check if the string is a UUID (version 3, 4 or 5).
**isVariableWidth(str)** | check if the string contains a mixture of full and half-width chars.
**isVAT(str, countryCode)** | checks that the string is a [valid VAT number](https://en.wikipedia.org/wiki/VAT_identification_number) if validation is available for the given country code matching [ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2). <br/><br/>Available country codes: `[ 'GB' ]`.
**isWhitelisted(str, chars)** | checks characters if they appear in the whitelist.
**matches(str, pattern [, modifiers])** | check if string matches the pattern.<br/><br/>Either `matches('foo', /foo/i)` or `matches('foo', 'foo', 'i')`.

Expand Down
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ import normalizeEmail from './lib/normalizeEmail';

import isSlug from './lib/isSlug';

import isVAT from './lib/isVAT';

const version = '13.1.17';

const validator = {
Expand Down Expand Up @@ -215,6 +217,7 @@ const validator = {
isSlug,
isTaxID,
isDate,
isVAT,
};

export default validator;
15 changes: 15 additions & 0 deletions src/lib/isVAT.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import assertString from './util/assertString';

export const vatMatchers = {
GB: /^GB((\d{3} \d{4} ([0-8][0-9]|9[0-6]))|(\d{9} \d{3})|(((GD[0-4])|(HA[5-9]))[0-9]{2}))$/,
};

export default function isVAT(str, countryCode) {
assertString(str);
assertString(countryCode);

if (countryCode in vatMatchers) {
return vatMatchers[countryCode].test(str);
}
throw new Error(`Invalid country code: '${countryCode}'`);
}
40 changes: 40 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -9098,4 +9098,44 @@ describe('Validators', () => {
],
});
});

it('should validate english VAT numbers', () => {
test({
validator: 'isVAT',
args: ['GB'],
valid: [
'GB999 9999 00',
'GB999 9999 96',
'GB999999999 999',
'GBGD000',
'GBGD499',
'GBHA500',
'GBHA999',
],
invalid: [
'GB999999900',
'GB999999996',
'GB999 9999 97',
'GB999999999999',
'GB999999999 9999',
'GB9999999999 999',
'GBGD 000',
'GBGD 499',
'GBHA 500',
'GBHA 999',
'GBGD500',
'GBGD999',
'GBHA000',
'GBHA499',
],
});

test({
validator: 'isVAT',
args: ['invalidCountryCode'],
error: [
'GB999 9999 00',
],
});
});
});