Skip to content

Commit d07eae6

Browse files
authored
feat: add isUUID 'loose' option to validate UUID-like hexadecimal strings (#2553)
1 parent bdb8f9e commit d07eae6

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ Validator | Description
169169
**isTaxID(str, locale)** | check if the string 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-CA', 'en-GB', 'en-IE', 'en-US', 'es-AR', 'es-ES', 'et-EE', 'fi-FI', 'fr-BE', 'fr-CA', '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', 'uk-UA']`.
170170
**isURL(str [, options])** | check if the string is a 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 to 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 to false isURL will not check if host is present in the URL.<br/>`require_port` - if set to true isURL will check if port is present in the URL.<br/>`allow_protocol_relative_urls` - if set to true protocol relative URLs will be allowed.<br/>`allow_fragments` - if set to false isURL will return false if fragments are present.<br/>`allow_query_components` - if set to false isURL will return false if query components are present.<br/>`validate_length` - if set to false isURL will skip string length validation. `max_allowed_length` will be ignored if this is set as `false`.<br/>`max_allowed_length` - if set isURL will not allow URLs longer than the specified value (default is 2084 that IE maximum URL length).
171171
**isULID(str)** | check if the string is a [ULID](https://github.com/ulid/spec).
172-
**isUUID(str [, version])** | check if the string is an RFC9562 UUID.<br/>`version` is one of `'1'`-`'8'`, `'nil'`, `'max'`, or `'all'`.
172+
**isUUID(str [, version])** | check if the string is an RFC9562 UUID.<br/>`version` is one of `'1'`-`'8'`, `'nil'`, `'max'`, `'all'` or `'loose'`. The `'loose'` option checks if the string is a UUID-like string with hexadecimal values, ignoring RFC9565.
173173
**isVariableWidth(str)** | check if the string contains a mixture of full and half-width chars.
174174
**isVAT(str, countryCode)** | check if the string is a [valid VAT number][VAT Number] if validation is available for the given country code matching [ISO 3166-1 alpha-2][ISO 3166-1 alpha-2]. <br/><br/>`countryCode` is one of `['AL', 'AR', 'AT', 'AU', 'BE', 'BG', 'BO', 'BR', 'BY', 'CA', 'CH', 'CL', 'CO', 'CR', 'CY', 'CZ', 'DE', 'DK', 'DO', 'EC', 'EE', 'EL', 'ES', 'FI', 'FR', 'GB', 'GT', 'HN', 'HR', 'HU', 'ID', 'IE', 'IL', 'IN', 'IS', 'IT', 'KZ', 'LT', 'LU', 'LV', 'MK', 'MT', 'MX', 'NG', 'NI', 'NL', 'NO', 'NZ', 'PA', 'PE', 'PH', 'PL', 'PT', 'PY', 'RO', 'RS', 'RU', 'SA', 'SE', 'SI', 'SK', 'SM', 'SV', 'TR', 'UA', 'UY', 'UZ', 'VE']`.
175175
**isWhitelisted(str, chars)** | check if the string consists only of characters that appear in the whitelist `chars`.

src/lib/isUUID.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const uuid = {
1212

1313
nil: /^00000000-0000-0000-0000-000000000000$/i,
1414
max: /^ffffffff-ffff-ffff-ffff-ffffffffffff$/i,
15+
loose: /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i,
1516

1617
// From https://github.com/uuidjs/uuid/blob/main/src/regex.js
1718
all: /^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$/i,

test/validators.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5662,6 +5662,33 @@ describe('Validators', () => {
56625662
'00000000-0000-0000-0000-000000000000',
56635663
],
56645664
});
5665+
test({
5666+
validator: 'isUUID',
5667+
args: ['loose'],
5668+
valid: [
5669+
'9deb20fe-a6e0-355c-81ea-288b009e4f6d',
5670+
'A987FBC9-4BED-3078-CF07-9141BA07C9F3',
5671+
'A987FBC9-4BED-4078-8F07-9141BA07C9F3',
5672+
'A987FBC9-4BED-5078-AF07-9141BA07C9F3',
5673+
'A987FBC9-4BED-6078-AF07-9141BA07C9F3',
5674+
'018C544A-D384-7000-BB74-3B1738ABE43C',
5675+
'A987FBC9-4BED-8078-AF07-9141BA07C9F3',
5676+
'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',
5677+
'AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA',
5678+
'eeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee',
5679+
'EEEEEEEE-EEEE-EEEE-EEEE-EEEEEEEEEEEE',
5680+
'99999999-9999-9999-9999-999999999999',
5681+
],
5682+
invalid: [
5683+
'',
5684+
'xxxA987FBC9-4BED-3078-CF07-9141BA07C9F3',
5685+
'A987FBC9-4BED-3078-CF07-9141BA07C9F3xxx',
5686+
'A987FBC94BED3078CF079141BA07C9F3',
5687+
'987FBC9-4BED-3078-CF07A-9141BA07C9F3',
5688+
'934859',
5689+
'AAAAAAAA-1111-1111-AAAG-111111111111',
5690+
],
5691+
});
56655692
test({
56665693
validator: 'isUUID',
56675694
args: ['all'],

0 commit comments

Comments
 (0)