Skip to content
This repository has been archived by the owner on Oct 20, 2021. It is now read-only.

Commit

Permalink
Merge pull request #15 from carlosdaniiel07/feature/uuid
Browse files Browse the repository at this point in the history
Add UUID checks
  • Loading branch information
henriqueleite42 committed Jan 8, 2021
2 parents 6153776 + 131a4d8 commit 8433407
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/checks/uuid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { makeFunction } from "checks/helpers";

const IS_UUID = /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
export const isUUID = makeFunction({
regex: IS_UUID,
});
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { isStrongPassword } from "checks/password";
import { isBrazillianPhone } from "checks/phone";
import { isUrl, hasUrl } from "checks/url";
import { isSimpleUsername } from "checks/username";
import { isUUID } from "checks/uuid";

export const check = {
/**
Expand Down Expand Up @@ -134,4 +135,8 @@ export const check = {
* - NO Special Characters, only `-` and `_`
*/
isSimpleUsername,
/**
* Check if a string is valid UUID (version 4)
*/
isUUID,
};
32 changes: 32 additions & 0 deletions src/tests/uuid/is-uuid.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { isUUID } from "checks/uuid";

/**
*
* True
*
*/

describe("is UUID (return True)", () => {
it("with valid UUID", () => {
const result = isUUID("24bd85a1-4eb7-4f63-829e-75c08ac2b6c0");
expect(result).toBeTruthy();
});
});

/**
*
* False
*
*/

describe("is UUID (return False)", () => {
it("with invalid UUID (1)", () => {
const result = isUUID("24bd85a1-4eb7-4f63-829e-75c08ac2b6t1");
expect(result).toBe(false);
});

it("with invalid UUID (2)", () => {
const result = isUUID("e7f019ea-4d52-11eb-ae93");
expect(result).toBe(false);
});
});

0 comments on commit 8433407

Please sign in to comment.