From 57738693d97e087200c929237c1d57cf0a69218b Mon Sep 17 00:00:00 2001 From: Dave Borghuis Date: Sat, 30 Oct 2021 12:01:48 +0200 Subject: [PATCH] feat(isVAT): add dutch NL locale (#1825) for hacktober #1742 if you want to extend it more check : https://www.oreilly.com/library/view/regular-expressions-cookbook/9781449327453/ch04s21.html --- README.md | 2 +- src/lib/isVAT.js | 1 + test/validators.js | 19 ++++++++++++++++--- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index cf913a202..93b5fd18f 100644 --- a/README.md +++ b/README.md @@ -165,7 +165,7 @@ Validator | Description **isURL(str [, options])** | check if the string is an URL.

`options` is an object which defaults to `{ protocols: ['http','https','ftp'], require_tld: true, require_protocol: false, require_host: true, require_port: false, require_valid_protocol: true, allow_underscores: false, host_whitelist: false, host_blacklist: false, allow_trailing_dot: false, allow_protocol_relative_urls: false, allow_fragments: true, allow_query_components: true, disallow_auth: false, validate_length: true }`.

require_protocol - if set as true isURL will return false if protocol is not present in the URL.
require_valid_protocol - isURL will check if the URL's protocol is present in the protocols option.
protocols - valid protocols can be modified with this option.
require_host - if set as false isURL will not check if host is present in the URL.
require_port - if set as true isURL will check if port is present in the URL.
allow_protocol_relative_urls - if set as true protocol relative URLs will be allowed.
allow_fragments - if set as false isURL will return false if fragments are present.
allow_query_components - if set as false isURL will return false if query components are present.
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).

Available country codes: `[ 'GB', 'IT' ]`. +**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).

Available country codes: `[ 'GB', 'IT','NL' ]`. **isWhitelisted(str, chars)** | checks characters if they appear in the whitelist. **matches(str, pattern [, modifiers])** | check if string matches the pattern.

Either `matches('foo', /foo/i)` or `matches('foo', 'foo', 'i')`. diff --git a/src/lib/isVAT.js b/src/lib/isVAT.js index 549bf336f..884b066ff 100644 --- a/src/lib/isVAT.js +++ b/src/lib/isVAT.js @@ -3,6 +3,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}$/, + NL: /^(NL)?[0-9]{9}B[0-9]{2}$/, }; export default function isVAT(str, countryCode) { diff --git a/test/validators.js b/test/validators.js index 5edb735d6..1b92f3a64 100644 --- a/test/validators.js +++ b/test/validators.js @@ -11310,7 +11310,7 @@ describe('Validators', () => { ], }); }); - it('should validate english VAT numbers', () => { + it('should validate VAT numbers', () => { test({ validator: 'isVAT', args: ['GB'], @@ -11340,7 +11340,6 @@ describe('Validators', () => { 'GBHA499', ], }); - test({ validator: 'isVAT', args: ['IT'], @@ -11356,7 +11355,21 @@ describe('Validators', () => { 'IT123456789', ], }); - + test({ + validator: 'isVAT', + args: ['NL'], + valid: [ + 'NL123456789B10', + '123456789B10', + ], + invalid: [ + 'NL12345678 910', + 'NL 123456789101', + 'NL123456789B1', + 'GB12345678910', + 'NL123456789', + ], + }); test({ validator: 'isVAT', args: ['invalidCountryCode'],