Skip to content

Commit af2b43c

Browse files
authored
feat(isUUID): add support for validation of version v1 and v2 (#1848)
* fix(isUUID) for null version argument supply * improve(isUUID) validation for version 1 and 2
1 parent 769f6d5 commit af2b43c

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ Validator | Description
163163
**isStrongPassword(str [, options])** | Check if a password is strong or not. Allows for custom requirements or scoring rules. If `returnScore` is true, then the function returns an integer score for the password rather than a boolean.<br/>Default options: <br/>`{ minLength: 8, minLowercase: 1, minUppercase: 1, minNumbers: 1, minSymbols: 1, returnScore: false, pointsPerUnique: 1, pointsPerRepeat: 0.5, pointsForContainingLower: 10, pointsForContainingUpper: 10, pointsForContainingNumber: 10, pointsForContainingSymbol: 10 }`
164164
**isTaxID(str, locale)** | Check if the given value is a valid Tax Identification Number. Default locale is `en-US`.<br/><br/>More info about exact TIN support can be found in `src/lib/isTaxID.js`<br/><br/>Supported locales: `[ 'bg-BG', 'cs-CZ', 'de-AT', 'de-DE', 'dk-DK', 'el-CY', 'el-GR', 'en-GB', 'en-IE', 'en-US', 'es-ES', 'et-EE', 'fi-FI', 'fr-BE', 'fr-FR', 'fr-LU', 'hr-HR', 'hu-HU', 'it-IT', 'lb-LU', 'lt-LT', 'lv-LV' 'mt-MT', 'nl-BE', 'nl-NL', 'pl-PL', 'pt-BR', 'pt-PT', 'ro-RO', 'sk-SK', 'sl-SI', 'sv-SE' ]`
165165
**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_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 }`.<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/>allow_fragments - if set as false isURL will return false if fragments are present.<br/>allow_query_components - if set as false isURL will return false if query components are present.<br/>validate_length - if set as false isURL will skip string length validation (2083 characters is IE max URL length).
166-
**isUUID(str [, version])** | check if the string is a UUID (version 3, 4 or 5).
166+
**isUUID(str [, version])** | check if the string is a UUID (version 1, 2, 3, 4 or 5).
167167
**isVariableWidth(str)** | check if the string contains a mixture of full and half-width chars.
168168
**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','NL' ]`.
169169
**isWhitelisted(str, chars)** | checks characters if they appear in the whitelist.

src/lib/isUUID.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import assertString from './util/assertString';
22

33
const uuid = {
4+
1: /^[0-9A-F]{8}-[0-9A-F]{4}-1[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i,
5+
2: /^[0-9A-F]{8}-[0-9A-F]{4}-2[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i,
46
3: /^[0-9A-F]{8}-[0-9A-F]{4}-3[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i,
57
4: /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
68
5: /^[0-9A-F]{8}-[0-9A-F]{4}-5[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
@@ -10,5 +12,5 @@ const uuid = {
1012
export default function isUUID(str, version) {
1113
assertString(str);
1214
const pattern = uuid[![undefined, null].includes(version) ? version : 'all'];
13-
return pattern && pattern.test(str);
15+
return !!pattern && pattern.test(str);
1416
}

test/validators.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4538,6 +4538,35 @@ describe('Validators', () => {
45384538
'A12AAAAA-1111-1111-AAAG-111111111111',
45394539
],
45404540
});
4541+
test({
4542+
validator: 'isUUID',
4543+
args: [1],
4544+
valid: [
4545+
'E034B584-7D89-11E9-9669-1AECF481A97B',
4546+
],
4547+
invalid: [
4548+
'xxxA987FBC9-4BED-3078-CF07-9141BA07C9F3',
4549+
'AAAAAAAA-1111-2222-AAAG',
4550+
'AAAAAAAA-1111-2222-AAAG-111111111111',
4551+
'A987FBC9-4BED-4078-8F07-9141BA07C9F3',
4552+
'A987FBC9-4BED-5078-AF07-9141BA07C9F3',
4553+
],
4554+
});
4555+
test({
4556+
validator: 'isUUID',
4557+
args: [2],
4558+
valid: [
4559+
'A987FBC9-4BED-2078-CF07-9141BA07C9F3',
4560+
],
4561+
invalid: [
4562+
'',
4563+
'xxxA987FBC9-4BED-3078-CF07-9141BA07C9F3',
4564+
'11111',
4565+
'AAAAAAAA-1111-1111-AAAG-111111111111',
4566+
'A987FBC9-4BED-4078-8F07-9141BA07C9F3',
4567+
'A987FBC9-4BED-5078-AF07-9141BA07C9F3',
4568+
],
4569+
});
45414570
test({
45424571
validator: 'isUUID',
45434572
args: [3],
@@ -4589,6 +4618,18 @@ describe('Validators', () => {
45894618
'A987FBC9-4BED-3078-CF07-9141BA07C9F3',
45904619
],
45914620
});
4621+
test({
4622+
validator: 'isUUID',
4623+
args: [6],
4624+
valid: [],
4625+
invalid: [
4626+
'987FBC97-4BED-1078-AF07-9141BA07C9F3',
4627+
'987FBC97-4BED-2078-AF07-9141BA07C9F3',
4628+
'987FBC97-4BED-3078-AF07-9141BA07C9F3',
4629+
'987FBC97-4BED-4078-AF07-9141BA07C9F3',
4630+
'987FBC97-4BED-5078-AF07-9141BA07C9F3',
4631+
],
4632+
});
45924633
});
45934634

45944635
it('should validate a string that is in another string or array', () => {

0 commit comments

Comments
 (0)