Skip to content

Commit

Permalink
fixed guid validation (#11)
Browse files Browse the repository at this point in the history
* enhanced test expectation for guid validation

Strings shall be able to be validated too, at least the validation function expects multiple value types!

* fixed test

(removed an accidentally duplicated var declaration)

* limited parameter tolerance for guid validation

& implemented validation support for hyphenated guid strings

* fixed guid validation syntax

* Update guid.ts

* Update guid.ts

* fixed guid validation

* adjusted tests to fully cover string validation

* incremented lib version

* incremented lib version

* corrected typos in comments

* enhanced validation to cover undefined & null

* covered unexpected runtime values in validation

* Update lib/guid.ts

Co-authored-by: Destinate <[email protected]>

* Update lib/guid.ts

Co-authored-by: Destinate <[email protected]>

Co-authored-by: Destinate <[email protected]>
  • Loading branch information
gekkedev and Destinate authored Jun 25, 2020
1 parent 7b6766e commit 8943cb8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 18 deletions.
21 changes: 13 additions & 8 deletions lib/guid.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
export class Guid {
/** Empty GUID string (hyphenated). */
public static EMPTY = "00000000-0000-0000-0000-000000000000";
public static EMPTY: string = "00000000-0000-0000-0000-000000000000";
private static validator: RegExp = new RegExp("^[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}$", "i");

public static isValid(guid: any) {
const value: string = guid.toString();
return guid && (guid instanceof Guid || this.validator.test(value));

public static isValid(guid: string | Guid): boolean {
if (typeof (guid) == "string") {
try {
let value: string = (guid.indexOf("-") < 0) ? (new Guid(guid)).toString() : guid; //if hyphenated, parse it first
return this.validator.test(value)
} catch { return false }
} else return guid instanceof Guid
}

/** Static alias for `new Guid()`. Refer to the constructor for further information. */
public static create(guid?: string): Guid {
if (!guid) guid = this.random();
return new Guid(guid);
}

Expand All @@ -36,8 +40,9 @@ export class Guid {
/** Container for the GUID itself (in the hyphenated format). */
private value: string = Guid.EMPTY;

/** Creates a guid object from a given Guid or, (if no parameter is given) creates one. Supported formats are: hyphenated and non-hyphenated. */
constructor(guid?: string) {
if (guid && Guid.isValid(guid)) { //hyphenated, no conversion necessary
if (guid && Guid.validator.test(guid)) { //hyphenated, no conversion necessary
this.value = guid.toLowerCase();
} else if (guid && !(guid.indexOf("-") >= 0) && guid.length == 32) { //non-hyphenated
let tempGuid: string = "";
Expand All @@ -51,7 +56,7 @@ export class Guid {
this.value = tempGuid;
} else if (!guid) {
this.value = Guid.random();
} else throw new TypeError("No valid GUID string given; cannot instantiate!")
} else throw new TypeError("Invalid GUID string given; cannot instantiate!")
}

/** Compares one Guid instance with another */
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ez-guid",
"version": "1.2.3",
"version": "1.2.4",
"description": "Easy GUID type implementation for Typescript",
"scripts": {
"verify": ".\\node_modules\\.bin\\tslint .\\lib\\**",
Expand Down
23 changes: 15 additions & 8 deletions tests/guid.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ describe("Guid", () => {
const example_no_hyphen: string = "0315642ca0699f3e18529adf2d075b93";

it("should create & validate a random GUID", () => {
const wrong: string = "wrongguid";
expect(Guid.isValid(wrong)).equal(false);

//generated guid using the static construction method
const static_guid: Guid = Guid.create();
expect(Guid.isValid(static_guid)).equal(true); //valid?
expect(static_guid.toString()).not.equal(Guid.EMPTY); //not null?

//generated guid using the constructor; same expectation here
const dynamic_guid: Guid = new Guid();
expect(Guid.isValid(dynamic_guid)).equal(true); //valid?
expect(dynamic_guid.toString()).not.equal(Guid.EMPTY); //not null?
Expand All @@ -29,11 +28,19 @@ describe("Guid", () => {
expect(() => new Guid(text)).to.throw(TypeError);
});

it("should parse & validate a GUID", () => {
it("should parse & validate GUIDs", () => {
const wrong: string = "wrongguid";
expect(Guid.isValid(wrong)).equal(false);

expect(Guid.isValid(example_hyphen)).equal(true);
expect(Guid.isValid(wrong)).equal(false); //must return false, no exception
expect(Guid.isValid(example_hyphen)).equal(true); //valid?
expect(Guid.isValid(example_no_hyphen)).equal(true); //non-hyphenated guid. also valid?
expect(Guid.isValid(example_no_hyphen + wrong)).equal(false); //valid guid plus one char. invalid?
//@ts-ignore
expect(Guid.isValid(undefined)).equal(false);
//@ts-ignore
expect(Guid.isValid(null)).equal(false);
//@ts-ignore
expect(Guid.isValid(123456789)).equal(false);
});

it("should create nulled GUIDs & return them as a string", () => {
Expand All @@ -52,7 +59,7 @@ describe("Guid", () => {
it("should compare GUID instances to another", () => {
const wrong_guid: Guid = Guid.create();
expect(wrong_guid.equals(Guid.create())).equal(false);

const correct_guid: Guid = Guid.create(example_hyphen);
const duplicate_guid: Guid = Guid.create(example_hyphen);
expect(correct_guid.equals(duplicate_guid)).equal(true);
Expand All @@ -64,7 +71,7 @@ describe("Guid", () => {
guids.push(Guid.create());
}
expect(guids.indexOf(guids[0]) < 0).equal(false);

expect(guids.indexOf(Guid.create()) < 0).equal(true);
});

Expand Down

0 comments on commit 8943cb8

Please sign in to comment.