From 31c367347bf994f8230e14921468c653299f5f0d Mon Sep 17 00:00:00 2001 From: Rafael Santos Date: Wed, 4 Jun 2025 17:35:09 +0100 Subject: [PATCH] Update UUID assert to include `v7`, `max`, and `nil` - Update its type signature to include new UUID versions, replacing the previous versions' integers with strings, for accuracy. --- README.md | 4 ++-- src/asserts/uuid-assert.js | 7 +++++-- src/types/index.d.ts | 7 +++++-- test/asserts/uuid-assert.test.js | 27 ++++++++++++--------------- 4 files changed, 24 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 944f704..fe5d81d 100644 --- a/README.md +++ b/README.md @@ -317,13 +317,13 @@ Tests if the value is a valid US subdivision or not. By default, codes in the sh Tests if the value is a valid US zip code. -### Uuid +### UUID Tests if the value is a valid `UUID`. #### Arguments -- `version` (optional) - the version to test the `UUID` for. Supported versions are `3`, `4` and `5`. Defaults to test for `all` three if omitted. +- `version` (optional) - the version to test the `UUID` for. Supported versions are `3`, `4`, `5`, `7`, `max`, and `nil`. Defaults to test for `all` if omitted. ## Usage diff --git a/src/asserts/uuid-assert.js b/src/asserts/uuid-assert.js index 21a007b..fb6b66a 100644 --- a/src/asserts/uuid-assert.js +++ b/src/asserts/uuid-assert.js @@ -14,7 +14,10 @@ const uuid = { 3: /^[0-9A-F]{8}-[0-9A-F]{4}-3[0-9A-F]{3}-[0-9A-F]{4}-[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, - all: /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[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, + max: /^FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF$/i, + nil: /^00000000-0000-0000-0000-000000000000$/i }; /** @@ -28,7 +31,7 @@ module.exports = function uuidAssert(version) { this.__class__ = 'Uuid'; - if (version && [3, 4, 5].indexOf(version) === -1) { + if (version && !uuid[version]) { throw new Error('UUID version specified is not supported.'); } diff --git a/src/types/index.d.ts b/src/types/index.d.ts index 73d73a2..68d90e1 100644 --- a/src/types/index.d.ts +++ b/src/types/index.d.ts @@ -194,6 +194,9 @@ export interface ValidatorJSAsserts { /** Valid US ZIP code. */ usZipCode(): AssertInstance; - /** Valid `UUID` (version 3, 4, or 5). */ - uuid(version?: 3 | 4 | 5): AssertInstance; + /** + * Valid `UUID`. + * @param [version] - UUID version `3`, `4`, `5`, `7`, `max` or `nil`. Defaults to `all` if omitted. + */ + uuid(version?: '3' | '4' | '5' | '7' | 'max' | 'nil'): AssertInstance; } diff --git a/test/asserts/uuid-assert.test.js b/test/asserts/uuid-assert.test.js index ff77f6d..8e11290 100644 --- a/test/asserts/uuid-assert.test.js +++ b/test/asserts/uuid-assert.test.js @@ -70,21 +70,18 @@ describe('UuidAssert', () => { } }); - it('should accept a v3 uuid', ({ assert }) => { - assert.doesNotThrow(() => { - Assert.uuid(3).validate('6fa459ea-ee8a-3ca4-894e-db77e160355e'); - }); - }); - - it('should accept a v4 uuid', ({ assert }) => { - assert.doesNotThrow(() => { - Assert.uuid(4).validate('17dd5a7a-637c-436e-bb8a-5398f7ac0a76'); - }); - }); - - it('should accept a v5 uuid', ({ assert }) => { - assert.doesNotThrow(() => { - Assert.uuid(5).validate('74738ff5-5367-5958-9aee-98fffdcd1876'); + [ + { name: 'v3', uuid: '6fa459ea-ee8a-3ca4-894e-db77e160355e', version: 3 }, + { name: 'v4', uuid: '17dd5a7a-637c-436e-bb8a-5398f7ac0a76', version: 4 }, + { name: 'v5', uuid: '74738ff5-5367-5958-9aee-98fffdcd1876', version: 5 }, + { name: 'v7', uuid: '01973bbd-2012-7c70-bc1a-59c06fe30326', version: 7 }, + { name: 'nil', uuid: '00000000-0000-0000-0000-000000000000', version: 'nil' }, + { name: 'max', uuid: 'FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF', version: 'max' } + ].forEach(({ name, uuid, version }) => { + it(`should accept a ${name} uuid`, ({ assert }) => { + assert.doesNotThrow(() => { + Assert.uuid(version).validate(uuid); + }); }); }); });