Skip to content

Commit 053832a

Browse files
committed
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.
1 parent f273cfe commit 053832a

File tree

4 files changed

+24
-21
lines changed

4 files changed

+24
-21
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,13 +317,13 @@ Tests if the value is a valid US subdivision or not. By default, codes in the sh
317317

318318
Tests if the value is a valid US zip code.
319319

320-
### Uuid
320+
### UUID
321321

322322
Tests if the value is a valid `UUID`.
323323

324324
#### Arguments
325325

326-
- `version` (optional) - the version to test the `UUID` for. Supported versions are `3`, `4` and `5`. Defaults to test for `all` three if omitted.
326+
- `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.
327327

328328
## Usage
329329

src/asserts/uuid-assert.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ const uuid = {
1414
3: /^[0-9A-F]{8}-[0-9A-F]{4}-3[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i,
1515
4: /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
1616
5: /^[0-9A-F]{8}-[0-9A-F]{4}-5[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
17-
all: /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i
17+
7: /^[0-9A-F]{8}-[0-9A-F]{4}-7[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i,
18+
all: /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i,
19+
max: /^FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF$/i,
20+
nil: /^00000000-0000-0000-0000-000000000000$/i
1821
};
1922

2023
/**
@@ -28,7 +31,7 @@ module.exports = function uuidAssert(version) {
2831

2932
this.__class__ = 'Uuid';
3033

31-
if (version && [3, 4, 5].indexOf(version) === -1) {
34+
if (version && !uuid[version]) {
3235
throw new Error('UUID version specified is not supported.');
3336
}
3437

src/types/index.d.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ export interface ValidatorJSAsserts {
194194
/** Valid US ZIP code. */
195195
usZipCode(): AssertInstance;
196196

197-
/** Valid `UUID` (version 3, 4, or 5). */
198-
uuid(version?: 3 | 4 | 5): AssertInstance;
197+
/**
198+
* Valid `UUID`.
199+
* @param [version] - UUID version 3, 4, 5, 7, 'max' or 'nil'. Defaults to 'all' if omitted.
200+
*/
201+
uuid(version?: '3' | '4' | '5' | '7' | 'max' | 'nil'): AssertInstance;
199202
}

test/asserts/uuid-assert.test.js

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,21 +70,18 @@ describe('UuidAssert', () => {
7070
}
7171
});
7272

73-
it('should accept a v3 uuid', ({ assert }) => {
74-
assert.doesNotThrow(() => {
75-
Assert.uuid(3).validate('6fa459ea-ee8a-3ca4-894e-db77e160355e');
76-
});
77-
});
78-
79-
it('should accept a v4 uuid', ({ assert }) => {
80-
assert.doesNotThrow(() => {
81-
Assert.uuid(4).validate('17dd5a7a-637c-436e-bb8a-5398f7ac0a76');
82-
});
83-
});
84-
85-
it('should accept a v5 uuid', ({ assert }) => {
86-
assert.doesNotThrow(() => {
87-
Assert.uuid(5).validate('74738ff5-5367-5958-9aee-98fffdcd1876');
73+
[
74+
{ name: 'v3', uuid: '6fa459ea-ee8a-3ca4-894e-db77e160355e', version: 3 },
75+
{ name: 'v4', uuid: '17dd5a7a-637c-436e-bb8a-5398f7ac0a76', version: 4 },
76+
{ name: 'v5', uuid: '74738ff5-5367-5958-9aee-98fffdcd1876', version: 5 },
77+
{ name: 'v7', uuid: '01973bbd-2012-7c70-bc1a-59c06fe30326', version: 7 },
78+
{ name: 'nil', uuid: '00000000-0000-0000-0000-000000000000', version: 'nil' },
79+
{ name: 'max', uuid: 'FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF', version: 'max' }
80+
].forEach(({ name, uuid, version }) => {
81+
it(`should accept a ${name} uuid`, ({ assert }) => {
82+
assert.doesNotThrow(() => {
83+
Assert.uuid(version).validate(uuid);
84+
});
8885
});
8986
});
9087
});

0 commit comments

Comments
 (0)