Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,17 @@ var bytes = uuid.parse('797ff043-11eb-11e1-80d6-510998755d10'); // -> <Buffer 79
var string = uuid.unparse(bytes); // -> '797ff043-11eb-11e1-80d6-510998755d10'
```

### uuid.validate(str)

Check whether a given string is a valid UUID

* `str` - (String) UUID(-like) string

```javascript
uuid.validate('797ff043-11eb-11e1-80d6-510998755d10'); // -> true
uuid.validate('some-invalid-uuid-1234567890-1234567'); // -> false
```

### uuid.noConflict()

(Browsers only) Set `uuid` property back to it's previous value.
Expand Down
7 changes: 7 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ var before = u0.split('-')[0], after = u1.split('-')[0];
var dt = parseInt(after, 16) - parseInt(before, 16);
assert(dt === 1, 'Ids spanning 1ms boundary are 100ns apart');

//
// Test validate
//

assert(uuid.validate('d9428888-122b-11e1-b85c-61cd3cbb3210') === true, 'Valid uuid');
assert(uuid.validate('d9428888-11e1-b85c-61cd3cbb3210') === false, 'Invalid uuid');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests for more rigor:

assert(uuid.validate('d9428888-122b-11e1-b85c-61cd3cbb3210') === true, 'Valid ID');
assert(uuid.validate('D9428888-122B-11E1-B85C-61CD3CBB3210') === true, 'Valid upper-case ID');
assert(uuid.validate('d9428888-122b-11e1-b85c-61cd3cbb321') === false, 'Too-long');
assert(uuid.validate('d9428888-122b-11e1-b85c-61cd3cbb32100') === false, 'Too-short');
assert(uuid.validate('d942888-8122b-11e1-b85c-61cd3cbb3210') === false, 'Bad hyphen-placement');
assert(uuid.validate('x9428888-122b-11e1-b85c-61cd3cbb3210') === false, 'Invalid hex digit');
assert(uuid.validate('d9428888-122b-01e1-b85c-61cd3cbb3210') === false, 'Bad version (0)');
assert(uuid.validate('d9428888-122b-61e1-b85c-61cd3cbb3210') === false, 'Bad version (6)');
assert(uuid.validate('d9428888-122b-11e1-785c-61cd3cbb3210') === false, 'Bad variant (0b01)');
assert(uuid.validate('d9428888-122b-11e1-c85c-61cd3cbb3210') === false, 'Bad variant (0b11)');


//
// Test parse/unparse
//
Expand Down
6 changes: 6 additions & 0 deletions uuid.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@
bth[buf[i++]] + bth[buf[i++]];
}

// **`validate(str)` - Test whether given string a valid UUID**
function validate(str) {
return /[0-9a-f]{22}|[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/i.test(str);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey zeke, I know you pulled his from #41, where I offered this as a one-liner solution, but if we're going to add a regex-based validate() method, it should conform as closely as possible to what RFC4122 does/doesn't allow. Specifically:

Thus, this regex:

/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89ABab][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/

(edited: to add ^ & $ to regex)

}

// **`v1()` - Generate time-based UUID**
//
// Inspired by https://github.com/LiosK/UUID.js
Expand Down Expand Up @@ -222,6 +227,7 @@
uuid.v4 = v4;
uuid.parse = parse;
uuid.unparse = unparse;
uuid.validate = validate;
uuid.BufferClass = BufferClass;

if (typeof define === 'function' && define.amd) {
Expand Down