Skip to content

Commit c0eefdc

Browse files
committed
Update UUID assert to include v7, max, and nil
1 parent 0b4e83d commit c0eefdc

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,13 @@ Tests if the value is a valid US subdivision or not. By default, codes in the sh
253253
### UsZipCode
254254
Tests if the value is a valid US zip code.
255255

256-
### Uuid
257-
Tests if the value is a valid uuid.
256+
### UUID
257+
258+
Tests if the value is a valid `UUID`.
258259

259260
#### Arguments
260-
- `version` (optional) - the version to test the uuid for. Supported version are `3`, `4` and `5`. Defaults to test for `all` three if omitted.
261+
262+
- `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.
261263

262264
## Usage
263265
The following is an example for the extra ip assert:

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 && [3, 4, 5, 7, 'max', 'nil'].indexOf(version) === -1) {
3235
throw new Error('UUID version specified is not supported.');
3336
}
3437

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)