Skip to content

Commit

Permalink
fix(isUUID): fully support rfc9562 (#2421)
Browse files Browse the repository at this point in the history
  • Loading branch information
broofa committed Jun 22, 2024
1 parent f81d857 commit 89e856c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ Validator | Description
**isTime(str [, options])** | check if the string is a valid time e.g. [`23:01:59`, new Date().toLocaleTimeString()].<br/><br/> `options` is an object which can contain the keys `hourFormat` or `mode`.<br/><br/>`hourFormat` is a key and defaults to `'hour24'`.<br/><br/>`mode` is a key and defaults to `'default'`. <br/><br/>`hourFormat` can contain the values `'hour12'` or `'hour24'`, `'hour24'` will validate hours in 24 format and `'hour12'` will validate hours in 12 format. <br/><br/>`mode` can contain the values `'default'` or `'withSeconds'`, `'default'` will validate `HH:MM` format, `'withSeconds'` will validate the `HH:MM:SS` format.
**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']`.
**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 (2083 characters is IE max URL length).
**isUUID(str [, version])** | check if the string is a UUID (version 1, 2, 3, 4, 5 or 7).
**isUUID(str [, version])** | check if the string is an RFC9562 UUID.<br/>`version` is one of `'1'`-`'8'`, `'nil'`, `'max'`, or `'all'`.
**isVariableWidth(str)** | check if the string contains a mixture of full and half-width chars.
**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']`.
**isWhitelisted(str, chars)** | check if the string consists only of characters that appear in the whitelist `chars`.
Expand Down
23 changes: 17 additions & 6 deletions src/lib/isUUID.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
import assertString from './util/assertString';

const uuid = {
1: /^[0-9A-F]{8}-[0-9A-F]{4}-1[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i,
2: /^[0-9A-F]{8}-[0-9A-F]{4}-2[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i,
3: /^[0-9A-F]{8}-[0-9A-F]{4}-3[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i,
1: /^[0-9A-F]{8}-[0-9A-F]{4}-1[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
2: /^[0-9A-F]{8}-[0-9A-F]{4}-2[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
3: /^[0-9A-F]{8}-[0-9A-F]{4}-3[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
4: /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
5: /^[0-9A-F]{8}-[0-9A-F]{4}-5[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
6: /^[0-9A-F]{8}-[0-9A-F]{4}-6[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
7: /^[0-9A-F]{8}-[0-9A-F]{4}-7[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
all: /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i,
8: /^[0-9A-F]{8}-[0-9A-F]{4}-8[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,

nil: /^00000000-0000-0000-0000-000000000000$/i,
max: /^ffffffff-ffff-ffff-ffff-ffffffffffff$/i,

// From https://github.com/uuidjs/uuid/blob/main/src/regex.js
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,
};

export default function isUUID(str, version) {
assertString(str);
const pattern = uuid[![undefined, null].includes(version) ? version : 'all'];
return !!pattern && pattern.test(str);

if (version === undefined || version === null) {
version = 'all';
}

return version in uuid ? uuid[version].test(str) : false;
}
45 changes: 39 additions & 6 deletions test/validators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5424,14 +5424,17 @@ describe('Validators', () => {
test({
validator: 'isUUID',
valid: [
'A987FBC9-4BED-3078-CF07-9141BA07C9F3',
'9deb20fe-a6e0-355c-81ea-288b009e4f6d',
'A987FBC9-4BED-4078-8F07-9141BA07C9F3',
'A987FBC9-4BED-5078-AF07-9141BA07C9F3',
'A987FBC9-4BED-6078-AF07-9141BA07C9F3',
'018C544A-D384-7000-BB74-3B1738ABE43C',
'A987FBC9-4BED-8078-AF07-9141BA07C9F3',
],
invalid: [
'',
'xxxA987FBC9-4BED-3078-CF07-9141BA07C9F3',
'A987FBC9-4BED-3078-CF07-9141BA07C9F3',
'A987FBC9-4BED-3078-CF07-9141BA07C9F3xxx',
'A987FBC94BED3078CF079141BA07C9F3',
'934859',
Expand All @@ -5443,12 +5446,13 @@ describe('Validators', () => {
validator: 'isUUID',
args: [undefined],
valid: [
'A117FBC9-4BED-3078-CF07-9141BA07C9F3',
'9deb20fe-a6e0-355c-81ea-288b009e4f6d',
'A117FBC9-4BED-5078-AF07-9141BA07C9F3',
'018C544A-D384-7000-BB74-3B1738ABE43C',
],
invalid: [
'',
'A117FBC9-4BED-3078-CF07-9141BA07C9F3',
'xxxA987FBC9-4BED-3078-CF07-9141BA07C9F3',
'A987FBC94BED3078CF079141BA07C9F3',
'A11AAAAA-1111-1111-AAAG-111111111111',
Expand All @@ -5458,12 +5462,13 @@ describe('Validators', () => {
validator: 'isUUID',
args: [null],
valid: [
'A127FBC9-4BED-3078-CF07-9141BA07C9F3',
'A127FBC9-4BED-3078-AF07-9141BA07C9F3',
'018C544A-D384-7000-BB74-3B1738ABE43C',
],
invalid: [
'',
'xxxA987FBC9-4BED-3078-CF07-9141BA07C9F3',
'A127FBC9-4BED-3078-CF07-9141BA07C9F3',
'A127FBC9-4BED-3078-CF07-9141BA07C9F3xxx',
'912859',
'A12AAAAA-1111-1111-AAAG-111111111111',
Expand All @@ -5488,13 +5493,14 @@ describe('Validators', () => {
validator: 'isUUID',
args: [2],
valid: [
'A987FBC9-4BED-2078-CF07-9141BA07C9F3',
'A987FBC9-4BED-2078-AF07-9141BA07C9F3',
],
invalid: [
'',
'xxxA987FBC9-4BED-3078-CF07-9141BA07C9F3',
'11111',
'AAAAAAAA-1111-1111-AAAG-111111111111',
'A987FBC9-4BED-2078-CF07-9141BA07C9F3',
'A987FBC9-4BED-4078-8F07-9141BA07C9F3',
'A987FBC9-4BED-5078-AF07-9141BA07C9F3',
'018C544A-D384-7000-BB74-3B1738ABE43C',
Expand All @@ -5504,10 +5510,11 @@ describe('Validators', () => {
validator: 'isUUID',
args: [3],
valid: [
'A987FBC9-4BED-3078-CF07-9141BA07C9F3',
'9deb20fe-a6e0-355c-81ea-288b009e4f6d',
],
invalid: [
'',
'A987FBC9-4BED-3078-CF07-9141BA07C9F3',
'xxxA987FBC9-4BED-3078-CF07-9141BA07C9F3',
'934859',
'AAAAAAAA-1111-1111-AAAG-111111111111',
Expand Down Expand Up @@ -5557,14 +5564,17 @@ describe('Validators', () => {
test({
validator: 'isUUID',
args: [6],
valid: [],
valid: [
'1ef29908-cde1-69d0-be16-bfc8518a95f0',
],
invalid: [
'987FBC97-4BED-1078-AF07-9141BA07C9F3',
'987FBC97-4BED-2078-AF07-9141BA07C9F3',
'987FBC97-4BED-3078-AF07-9141BA07C9F3',
'987FBC97-4BED-4078-AF07-9141BA07C9F3',
'987FBC97-4BED-5078-AF07-9141BA07C9F3',
'018C544A-D384-7000-BB74-3B1738ABE43C',
'987FBC97-4BED-8078-AF07-9141BA07C9F3',
],
});
test({
Expand All @@ -5580,6 +5590,29 @@ describe('Validators', () => {
'AAAAAAAA-1111-1111-AAAG-111111111111',
'A987FBC9-4BED-5078-AF07-9141BA07C9F3',
'A987FBC9-4BED-3078-CF07-9141BA07C9F3',
'A987FBC9-4BED-6078-AF07-9141BA07C9F3',
'A987FBC9-4BED-8078-AF07-9141BA07C9F3',
'713ae7e3-cb32-45f9-adcb-7c4fa86b90c1',
'625e63f3-58f5-40b7-83a1-a72ad31acffb',
'57b73598-8764-4ad0-a76a-679bb6640eb1',
'9c858901-8a57-4791-81fe-4c455b099bc9',
],
});
test({
validator: 'isUUID',
args: [8],
valid: [
'018C544A-D384-8000-BB74-3B1738ABE43C',
],
invalid: [
'',
'xxxA987FBC9-4BED-3078-CF07-9141BA07C9F3',
'934859',
'AAAAAAAA-1111-1111-AAAG-111111111111',
'A987FBC9-4BED-5078-AF07-9141BA07C9F3',
'A987FBC9-4BED-3078-CF07-9141BA07C9F3',
'A987FBC9-4BED-6078-AF07-9141BA07C9F3',
'A987FBC9-4BED-7078-AF07-9141BA07C9F3',
'713ae7e3-cb32-45f9-adcb-7c4fa86b90c1',
'625e63f3-58f5-40b7-83a1-a72ad31acffb',
'57b73598-8764-4ad0-a76a-679bb6640eb1',
Expand Down

0 comments on commit 89e856c

Please sign in to comment.