Skip to content
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
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
80 changes: 80 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
80 changes: 80 additions & 0 deletions README_js.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export { default as v3 } from './v3.js';
export { default as v4 } from './v4.js';
export { default as v5 } from './v5.js';
export { default as NIL } from './nil.js';
export { default as validate, isV1, isV3, isV4, isV5 } from './validate.js';
export { default as version } from './version.js';
export { default as validate } from './validate.js';
export { default as stringify } from './stringify.js';
export { default as parse } from './parse.js';
21 changes: 21 additions & 0 deletions src/validate.js
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;
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

@TrySound TrySound Dec 6, 2020

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.

Copy link
Contributor Author

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.


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);
}
45 changes: 40 additions & 5 deletions test/unit/validate.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import assert from 'assert';
import validate from '../../src/validate.js';
import validate, { isV1, isV3, isV4, isV5 } from '../../src/validate.js';
import NIL from '../../src/nil.js';

const validUuids = {
v1: 'd9428888-122b-11e1-b85c-61cd3cbb3210',
v3: 'a981a0c2-68b1-35dc-bcfc-296e52ab01ec',
v4: '109156be-c4fb-41ea-b1b4-efe1671c5836',
v5: '90123e1c-7512-523e-bb28-76fab9f2f73d',
};

describe('validate', () => {
test('validate uuid', () => {
assert.strictEqual(validate(NIL), true);

assert.strictEqual(validate('d9428888-122b-11e1-b85c-61cd3cbb3210'), true);
assert.strictEqual(validate(validUuids.v1), true);

assert.strictEqual(validate('109156be-c4fb-41ea-b1b4-efe1671c5836'), true);
assert.strictEqual(validate(validUuids.v3), true);

assert.strictEqual(validate('a981a0c2-68b1-35dc-bcfc-296e52ab01ec'), true);
assert.strictEqual(validate(validUuids.v4), true);

assert.strictEqual(validate('90123e1c-7512-523e-bb28-76fab9f2f73d'), true);
assert.strictEqual(validate(validUuids.v5), true);

assert.strictEqual(validate(), false);

Expand All @@ -29,4 +36,32 @@ describe('validate', () => {
false
);
});

test('validate v1 uuid', () => {
assert.strictEqual(isV1(validUuids.v1), true);
assert.strictEqual(isV1(validUuids.v3), false);
assert.strictEqual(isV1(validUuids.v4), false);
assert.strictEqual(isV1(validUuids.v5), false);
});

test('validate v3 uuid', () => {
assert.strictEqual(isV3(validUuids.v1), false);
assert.strictEqual(isV3(validUuids.v3), true);
assert.strictEqual(isV3(validUuids.v4), false);
assert.strictEqual(isV3(validUuids.v5), false);
});

test('validate v4 uuid', () => {
assert.strictEqual(isV4(validUuids.v1), false);
assert.strictEqual(isV4(validUuids.v3), false);
assert.strictEqual(isV4(validUuids.v4), true);
assert.strictEqual(isV4(validUuids.v5), false);
});

test('validate v5 uuid', () => {
assert.strictEqual(isV5(validUuids.v1), false);
assert.strictEqual(isV5(validUuids.v3), false);
assert.strictEqual(isV5(validUuids.v4), false);
assert.strictEqual(isV5(validUuids.v5), true);
});
});