-
-
Notifications
You must be signed in to change notification settings - Fork 896
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: per-version uuid validation #542
Closed
Closed
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,10 @@ For timestamp UUIDs, namespace UUIDs, and other options read on ... | |
| [`uuid.v4()`](#uuidv4options-buffer-offset) | Create a version 4 (random) UUID | | | ||
| [`uuid.v5()`](#uuidv5name-namespace-buffer-offset) | Create a version 5 (namespace w/ SHA-1) UUID | | | ||
| [`uuid.validate()`](#uuidvalidatestr) | Test a string to see if it is a valid UUID | New in `[email protected]` | | ||
| [`uuid.isV1()`](#uuidisv1str) | Test a string to see if it is a valid V1 UUID | | | ||
| [`uuid.isV3()`](#uuidisv3str) | Test a string to see if it is a valid V3 UUID | | | ||
| [`uuid.isV4()`](#uuidisv4str) | Test a string to see if it is a valid V4 UUID | | | ||
| [`uuid.isV5()`](#uuidisv5str) | Test a string to see if it is a valid V5 UUID | | | ||
| [`uuid.version()`](#uuidversionstr) | Detect RFC version of a UUID | New in `[email protected]` | | ||
|
||
## API | ||
|
@@ -296,6 +300,82 @@ uuidValidate('not a UUID'); // ⇨ false | |
uuidValidate('6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b'); // ⇨ true | ||
``` | ||
|
||
### uuid.isV1(str) | ||
|
||
Test a string to see if it is a valid V1 UUID | ||
|
||
| | | | ||
| --------- | ------------------------------------------------------ | | ||
| `str` | `String` to validate | | ||
| _returns_ | `true` if string is a valid V1 UUID, `false` otherwise | | ||
|
||
Example: | ||
|
||
```javascript | ||
import { isV1 as uuidIsV1 } from 'uuid'; | ||
|
||
uuidIsV1('not a UUID'); // ⇨ false | ||
uuidIsV1('d9428888-122b-11e1-b85c-61cd3cbb3210'); // ⇨ true | ||
uuidIsV1('a981a0c2-68b1-35dc-bcfc-296e52ab01ec'); // ⇨ false | ||
``` | ||
|
||
### uuid.isV3(str) | ||
|
||
Test a string to see if it is a valid V3 UUID | ||
|
||
| | | | ||
| --------- | ------------------------------------------------------ | | ||
| `str` | `String` to validate | | ||
| _returns_ | `true` if string is a valid V3 UUID, `false` otherwise | | ||
|
||
Example: | ||
|
||
```javascript | ||
import { isV3 as uuidIsV3 } from 'uuid'; | ||
|
||
uuidIsV3('not a UUID'); // ⇨ false | ||
uuidIsV3('a981a0c2-68b1-35dc-bcfc-296e52ab01ec'); // ⇨ true | ||
uuidIsV3('109156be-c4fb-41ea-b1b4-efe1671c5836'); // ⇨ false | ||
``` | ||
|
||
### uuid.isV4(str) | ||
|
||
Test a string to see if it is a valid V4 UUID | ||
|
||
| | | | ||
| --------- | ------------------------------------------------------ | | ||
| `str` | `String` to validate | | ||
| _returns_ | `true` if string is a valid V4 UUID, `false` otherwise | | ||
|
||
Example: | ||
|
||
```javascript | ||
import { isV4 as uuidIsV4 } from 'uuid'; | ||
|
||
uuidIsV4('not a UUID'); // ⇨ false | ||
uuidIsV4('109156be-c4fb-41ea-b1b4-efe1671c5836'); // ⇨ true | ||
uuidIsV4('90123e1c-7512-523e-bb28-76fab9f2f73d'); // ⇨ false | ||
``` | ||
|
||
### uuid.isV5(str) | ||
|
||
Test a string to see if it is a valid V5 UUID | ||
|
||
| | | | ||
| --------- | ------------------------------------------------------ | | ||
| `str` | `String` to validate | | ||
| _returns_ | `true` if string is a valid V5 UUID, `false` otherwise | | ||
|
||
Example: | ||
|
||
```javascript | ||
import { isV5 as uuidIsV5 } from 'uuid'; | ||
|
||
uuidIsV5('not a UUID'); // ⇨ false | ||
uuidIsV5('90123e1c-7512-523e-bb28-76fab9f2f73d'); // ⇨ true | ||
uuidIsV5('109156be-c4fb-41ea-b1b4-efe1671c5836'); // ⇨ false | ||
``` | ||
|
||
### uuid.version(str) | ||
|
||
Detect RFC version of a UUID | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,6 +69,10 @@ For timestamp UUIDs, namespace UUIDs, and other options read on ... | |
| [`uuid.v4()`](#uuidv4options-buffer-offset) | Create a version 4 (random) UUID | | | ||
| [`uuid.v5()`](#uuidv5name-namespace-buffer-offset) | Create a version 5 (namespace w/ SHA-1) UUID | | | ||
| [`uuid.validate()`](#uuidvalidatestr) | Test a string to see if it is a valid UUID | New in `[email protected]` | | ||
| [`uuid.isV1()`](#uuidisv1str) | Test a string to see if it is a valid V1 UUID | | | ||
| [`uuid.isV3()`](#uuidisv3str) | Test a string to see if it is a valid V3 UUID | | | ||
| [`uuid.isV4()`](#uuidisv4str) | Test a string to see if it is a valid V4 UUID | | | ||
| [`uuid.isV5()`](#uuidisv5str) | Test a string to see if it is a valid V5 UUID | | | ||
| [`uuid.version()`](#uuidversionstr) | Detect RFC version of a UUID | New in `[email protected]` | | ||
|
||
## API | ||
|
@@ -302,6 +306,82 @@ uuidValidate('not a UUID'); // RESULT | |
uuidValidate('6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b'); // RESULT | ||
``` | ||
|
||
### uuid.isV1(str) | ||
|
||
Test a string to see if it is a valid V1 UUID | ||
|
||
| | | | ||
| --------- | ------------------------------------------------------ | | ||
| `str` | `String` to validate | | ||
| _returns_ | `true` if string is a valid V1 UUID, `false` otherwise | | ||
|
||
Example: | ||
|
||
```javascript --run | ||
import { isV1 as uuidIsV1 } from 'uuid'; | ||
|
||
uuidIsV1('not a UUID'); // RESULT | ||
uuidIsV1('d9428888-122b-11e1-b85c-61cd3cbb3210'); // RESULT | ||
uuidIsV1('a981a0c2-68b1-35dc-bcfc-296e52ab01ec'); // RESULT | ||
``` | ||
|
||
### uuid.isV3(str) | ||
|
||
Test a string to see if it is a valid V3 UUID | ||
|
||
| | | | ||
| --------- | ------------------------------------------------------ | | ||
| `str` | `String` to validate | | ||
| _returns_ | `true` if string is a valid V3 UUID, `false` otherwise | | ||
|
||
Example: | ||
|
||
```javascript --run | ||
import { isV3 as uuidIsV3 } from 'uuid'; | ||
|
||
uuidIsV3('not a UUID'); // RESULT | ||
uuidIsV3('a981a0c2-68b1-35dc-bcfc-296e52ab01ec'); // RESULT | ||
uuidIsV3('109156be-c4fb-41ea-b1b4-efe1671c5836'); // RESULT | ||
``` | ||
|
||
### uuid.isV4(str) | ||
|
||
Test a string to see if it is a valid V4 UUID | ||
|
||
| | | | ||
| --------- | ------------------------------------------------------ | | ||
| `str` | `String` to validate | | ||
| _returns_ | `true` if string is a valid V4 UUID, `false` otherwise | | ||
|
||
Example: | ||
|
||
```javascript --run | ||
import { isV4 as uuidIsV4 } from 'uuid'; | ||
|
||
uuidIsV4('not a UUID'); // RESULT | ||
uuidIsV4('109156be-c4fb-41ea-b1b4-efe1671c5836'); // RESULT | ||
uuidIsV4('90123e1c-7512-523e-bb28-76fab9f2f73d'); // RESULT | ||
``` | ||
|
||
### uuid.isV5(str) | ||
|
||
Test a string to see if it is a valid V5 UUID | ||
|
||
| | | | ||
| --------- | ------------------------------------------------------ | | ||
| `str` | `String` to validate | | ||
| _returns_ | `true` if string is a valid V5 UUID, `false` otherwise | | ||
|
||
Example: | ||
|
||
```javascript --run | ||
import { isV5 as uuidIsV5 } from 'uuid'; | ||
|
||
uuidIsV5('not a UUID'); // RESULT | ||
uuidIsV5('90123e1c-7512-523e-bb28-76fab9f2f73d'); // RESULT | ||
uuidIsV5('109156be-c4fb-41ea-b1b4-efe1671c5836'); // RESULT | ||
``` | ||
|
||
### uuid.version(str) | ||
|
||
Detect RFC version of a UUID | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,28 @@ | ||
import REGEX from './regex.js'; | ||
import version from './version.js'; | ||
|
||
function validate(uuid) { | ||
return typeof uuid === 'string' && REGEX.test(uuid); | ||
} | ||
|
||
export default validate; | ||
|
||
function validateByVersion(uuid, uuidVersion) { | ||
return validate(uuid) && version(uuid) === uuidVersion; | ||
} | ||
|
||
export function isV1(uuid) { | ||
return validateByVersion(uuid, 1); | ||
} | ||
|
||
export function isV3(uuid) { | ||
return validateByVersion(uuid, 3); | ||
} | ||
|
||
export function isV4(uuid) { | ||
return validateByVersion(uuid, 4); | ||
} | ||
|
||
export function isV5(uuid) { | ||
return validateByVersion(uuid, 5); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it makes sense to use only named exports in this module.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure what you mean?
Most other things seem to be done as default exports.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They don't have named exports. Validate is not that special to use different export syntax than other methods in this module.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahhh, I understand what you mean now. Good point.
I agree that'd be more consistent with the rest of the source. I'll revise my branch.