Skip to content

Commit

Permalink
feat(isVAT): add IT locale (#1536)
Browse files Browse the repository at this point in the history
* feat(isVAT): add Italian regex

* test(isVAT): add Italian regex testing
  • Loading branch information
fedeci committed Dec 2, 2020
1 parent 9dc4ed3 commit 5810e36
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,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' ]`.
**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', 'IT' ]`.
**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
1 change: 1 addition & 0 deletions src/lib/isVAT.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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}))$/,
IT: /^(IT)?[0-9]{11}$/,
};

export default function isVAT(str, countryCode) {
Expand Down
16 changes: 16 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -10216,6 +10216,22 @@ describe('Validators', () => {
],
});

test({
validator: 'isVAT',
args: ['IT'],
valid: [
'IT12345678910',
'12345678910',
],
invalid: [
'IT12345678 910',
'IT 123456789101',
'IT123456789101',
'GB12345678910',
'IT123456789',
],
});

test({
validator: 'isVAT',
args: ['invalidCountryCode'],
Expand Down
3 changes: 2 additions & 1 deletion validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4607,7 +4607,8 @@ function isStrongPassword(str) {
}

var 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}))$/
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}))$/,
IT: /^(IT)?[0-9]{11}$/
};
function isVAT(str, countryCode) {
assertString(str);
Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.

0 comments on commit 5810e36

Please sign in to comment.