Skip to content

Commit c8cbed9

Browse files
Merge changes from v1.9.0-next.1 into main (#103)
2 parents 12d1909 + 6eed6c8 commit c8cbed9

7 files changed

Lines changed: 65 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
44

55
---
66

7+
# [1.9.0-next.1](https://github.com/Nerdware-LLC/ts-string-helpers/compare/v1.8.1...v1.9.0-next.1) (2025-02-24)
8+
9+
10+
### Features
11+
12+
* add `isValidUUID` ([5d77323](https://github.com/Nerdware-LLC/ts-string-helpers/commit/5d773230f72c035da2423856c71bacb8177ac98d))
13+
714
## [1.8.1](https://github.com/Nerdware-LLC/ts-string-helpers/compare/v1.8.0...v1.8.1) (2025-02-24)
815

916

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ app.post("/register", (req, res, next) => {
147147
| [`isValidText`](src/validate/text.ts) | Returns `true` if `value` does not contain potentially harmful characters ([🌎i18n-friendly](#-unicode-support)) |
148148
| [`isValidURL`](src/validate/url.ts) | Returns `true` if `value` is a valid absolute or relative URL (protocol agnostic) |
149149
| [`isValidHttpURL`](src/validate/url.ts) | Returns `true` if `value` is a valid absolute HTTP/S URL |
150+
| [`isValidUUID`](src/validate/uuid.ts) | Returns `true` if `value` is a valid UUID _**of any version**_ |
150151

151152
## 🌎 Unicode Support
152153

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nerdware/ts-string-helpers",
3-
"version": "1.8.1",
3+
"version": "1.9.0-next.1",
44
"description": "TypeScript string utils for validation and sanitization.",
55
"author": {
66
"name": "Trevor Anderson",

src/validate/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ export * from "./phone.js";
1717
export * from "./streetAddress.js";
1818
export * from "./text.js";
1919
export * from "./url.js";
20+
export * from "./uuid.js";

src/validate/uuid.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { isValidUUID } from "./uuid.js";
2+
3+
describe("isValidUUID", () => {
4+
// POSITIVE TEST CASES
5+
test.each([
6+
// The UUID spec defines a total of 8 versions
7+
...["1", "2", "3", "4", "5", "6", "7", "8"].map((uuidVersionNum) => ({
8+
// Note: the hard-coded 8 reflects the UUID variant
9+
input: `aaaaaaaa-aaaa-${uuidVersionNum}aaa-8aaa-aaaaaaaaaaaa`,
10+
description: `a version-${uuidVersionNum} UUID string`,
11+
})),
12+
{ input: "00000000-0000-0000-0000-000000000000", description: "the nil UUID (all zeros)" },
13+
{ input: "ffffffff-ffff-ffff-ffff-ffffffffffff", description: "the max UUID (all f's)" },
14+
])(`returns true when called with $description`, ({ input }) => {
15+
expect(isValidUUID(input)).toBe(true);
16+
});
17+
18+
// NEGATIVE TEST CASES
19+
test.each([
20+
{ input: "", description: "an empty string" },
21+
{ input: "foo", description: "a non-UUID string" },
22+
{ input: 1, description: "1 (a truthy number)" },
23+
{ input: 0, description: "0 (zero)" },
24+
{ input: true, description: "true (boolean)" },
25+
{ input: false, description: "false (boolean)" },
26+
{ input: null, description: "null" },
27+
{ input: undefined, description: "undefined" },
28+
{
29+
input: "aaaaaaaa-aaaa-9aaa-8aaa-aaaaaaaaaaaa",
30+
description: "a UUID with an invalid version number",
31+
},
32+
{
33+
input: "aaaaaaaa-aaaa-1aaa-7aaa-aaaaaaaaaaaa",
34+
description: "a UUID with an invalid variant identifier", // must be 8, 9, a, or b
35+
},
36+
])("returns false when called with $description", ({ input }) => {
37+
expect(isValidUUID(input)).toBe(false);
38+
});
39+
});

src/validate/uuid.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { getValidatorFn } from "../utils/getValidatorFn.js";
2+
3+
/**
4+
* Regex for validating UUIDs _**of any version**_.
5+
*
6+
* Regex source: https://github.com/uuidjs/uuid/blob/6dcb15b86357afdf29ae2dabf5b2a8afab83c2c0/src/regex.js
7+
*/
8+
export const UUID_VALIDATION_REGEX =
9+
/^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$/i;
10+
11+
/**
12+
* Returns `true` if `value` is a valid UUID _**of any version**_.
13+
*/
14+
export const isValidUUID = getValidatorFn(UUID_VALIDATION_REGEX);

0 commit comments

Comments
 (0)