Skip to content

Commit

Permalink
feat: Make validators more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkarolyi committed Mar 7, 2022
1 parent 4afe72c commit 8b9b71f
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 70 deletions.
34 changes: 27 additions & 7 deletions __tests__/validators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,13 @@ describe("Validators", () => {

it("returns true if a number was given", () => {
expect(TNumber.isValid(0)).toBe(true);
expect(TNumber.isValid(-12.23)).toBe(true);
expect(TNumber.isValid(12.23)).toBe(true);
});

it("returns false if a non-number value was given", () => {
expect(TNumber.isValid("")).toBe(false);
expect(TNumber.isValid(NaN)).toBe(false);
});
});

Expand All @@ -83,10 +86,12 @@ describe("Validators", () => {

it("returns true if a whole number was given", () => {
expect(TInteger.isValid(1)).toBe(true);
expect(TInteger.isValid(-12)).toBe(true);
});

it("returns false if not a whole number value was given", () => {
expect(TInteger.isValid(1.2)).toBe(false);
expect(TInteger.isValid(NaN)).toBe(false);
});
});

Expand All @@ -103,12 +108,19 @@ describe("Validators", () => {
expect(TNumberAsString.isValid(0)).toBe(false);
});

it("returns false if the string cannot be converted to a number", () => {
it("returns false if the string isn't a valid number", () => {
expect(TNumberAsString.isValid("foo")).toBe(false);
expect(TNumberAsString.isValid("")).toBe(false);
expect(TNumberAsString.isValid("-0")).toBe(false);
expect(TNumberAsString.isValid("- 12")).toBe(false);
});

it("returns true if the string can be converted to a number", () => {
it("returns true if the string is a valid number", () => {
expect(TNumberAsString.isValid("12.235")).toBe(true);
expect(TNumberAsString.isValid("12")).toBe(true);
expect(TNumberAsString.isValid("-12.34")).toBe(true);
expect(TNumberAsString.isValid("-12")).toBe(true);
expect(TNumberAsString.isValid("0")).toBe(true);
});
});

Expand All @@ -127,10 +139,17 @@ describe("Validators", () => {

it("returns false if the string is not an integer", () => {
expect(TIntegerAsString.isValid("1.23")).toBe(false);
expect(TIntegerAsString.isValid("")).toBe(false);
expect(TIntegerAsString.isValid("01")).toBe(false);
expect(TIntegerAsString.isValid("-0")).toBe(false);
expect(TIntegerAsString.isValid("--1")).toBe(false);
expect(TIntegerAsString.isValid("- 0")).toBe(false);
});

it("returns true if the string is a valid integer", () => {
expect(TIntegerAsString.isValid("1")).toBe(true);
expect(TIntegerAsString.isValid("-5")).toBe(true);
expect(TIntegerAsString.isValid("0")).toBe(true);
});
});

Expand All @@ -148,6 +167,7 @@ describe("Validators", () => {
});

it("returns false if a non-boolean value was given", () => {
expect(TBoolean.isValid("true")).toBe(false);
expect(TBoolean.isValid(undefined)).toBe(false);
});
});
Expand Down Expand Up @@ -185,10 +205,8 @@ describe("Validators", () => {

it("returns false if not an object value was given", () => {
expect(TObject.isValid(() => 10)).toBe(false);
});

it("returns true if null value was given", () => {
expect(TObject.isValid(null)).toBe(true);
expect(TObject.isValid(null)).toBe(false);
expect(TObject.isValid(BigInt(100))).toBe(false);
});
});

Expand Down Expand Up @@ -244,6 +262,8 @@ describe("Validators", () => {

it("returns false if a non-null value was given", () => {
expect(TNull.isValid({})).toBe(false);
expect(TNull.isValid(0)).toBe(false);
expect(TNull.isValid(undefined)).toBe(false);
});
});

Expand Down Expand Up @@ -357,7 +377,7 @@ describe("Validators", () => {
expect(validator.isValid({ foo: 10 })).toBe(false);
});

it("returns false, if one of the values is invalid", () => {
it("returns false, if one of the values are invalid", () => {
expect(validator.isValid({ foo: "10" })).toBe(false);
});
});
Expand Down
18 changes: 9 additions & 9 deletions docs/classes/Guard.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ const user = TUser.cast({ posts: ["Who am I?", "I am a user."] });

#### Defined in

[src/guard.ts:67](https://github.com/davidkarolyi/tguard/blob/483baa7/src/guard.ts#L67)
[src/guard.ts:67](https://github.com/davidkarolyi/tguard/blob/4afe72c/src/guard.ts#L67)

## Properties

Expand All @@ -117,7 +117,7 @@ const user = TUser.cast({ posts: ["Who am I?", "I am a user."] });

#### Defined in

[src/guard.ts:60](https://github.com/davidkarolyi/tguard/blob/483baa7/src/guard.ts#L60)
[src/guard.ts:60](https://github.com/davidkarolyi/tguard/blob/4afe72c/src/guard.ts#L60)

___

Expand All @@ -127,7 +127,7 @@ ___

#### Defined in

[src/guard.ts:61](https://github.com/davidkarolyi/tguard/blob/483baa7/src/guard.ts#L61)
[src/guard.ts:61](https://github.com/davidkarolyi/tguard/blob/4afe72c/src/guard.ts#L61)

## Accessors

Expand All @@ -141,7 +141,7 @@ ___

#### Defined in

[src/guard.ts:76](https://github.com/davidkarolyi/tguard/blob/483baa7/src/guard.ts#L76)
[src/guard.ts:76](https://github.com/davidkarolyi/tguard/blob/4afe72c/src/guard.ts#L76)

## Methods

Expand Down Expand Up @@ -186,7 +186,7 @@ const user = TUser.cast({ posts: ["Who am I?", "I am a user."] });

#### Defined in

[src/guard.ts:121](https://github.com/davidkarolyi/tguard/blob/483baa7/src/guard.ts#L121)
[src/guard.ts:121](https://github.com/davidkarolyi/tguard/blob/4afe72c/src/guard.ts#L121)

___

Expand All @@ -206,7 +206,7 @@ ___

#### Defined in

[src/guard.ts:145](https://github.com/davidkarolyi/tguard/blob/483baa7/src/guard.ts#L145)
[src/guard.ts:145](https://github.com/davidkarolyi/tguard/blob/4afe72c/src/guard.ts#L145)

___

Expand All @@ -226,7 +226,7 @@ ___

#### Defined in

[src/guard.ts:157](https://github.com/davidkarolyi/tguard/blob/483baa7/src/guard.ts#L157)
[src/guard.ts:157](https://github.com/davidkarolyi/tguard/blob/4afe72c/src/guard.ts#L157)

___

Expand Down Expand Up @@ -255,7 +255,7 @@ If the the given `value` is matching the `schema`.

#### Defined in

[src/guard.ts:86](https://github.com/davidkarolyi/tguard/blob/483baa7/src/guard.ts#L86)
[src/guard.ts:86](https://github.com/davidkarolyi/tguard/blob/4afe72c/src/guard.ts#L86)

___

Expand All @@ -275,4 +275,4 @@ ___

#### Defined in

[src/guard.ts:140](https://github.com/davidkarolyi/tguard/blob/483baa7/src/guard.ts#L140)
[src/guard.ts:140](https://github.com/davidkarolyi/tguard/blob/4afe72c/src/guard.ts#L140)
6 changes: 3 additions & 3 deletions docs/classes/ValidationError.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Error.constructor

#### Defined in

[src/errors.ts:5](https://github.com/davidkarolyi/tguard/blob/483baa7/src/errors.ts#L5)
[src/errors.ts:5](https://github.com/davidkarolyi/tguard/blob/4afe72c/src/errors.ts#L5)

## Properties

Expand All @@ -58,7 +58,7 @@ Error.constructor

#### Defined in

[src/errors.ts:3](https://github.com/davidkarolyi/tguard/blob/483baa7/src/errors.ts#L3)
[src/errors.ts:3](https://github.com/davidkarolyi/tguard/blob/4afe72c/src/errors.ts#L3)

___

Expand Down Expand Up @@ -96,7 +96,7 @@ ___

#### Defined in

[src/errors.ts:2](https://github.com/davidkarolyi/tguard/blob/483baa7/src/errors.ts#L2)
[src/errors.ts:2](https://github.com/davidkarolyi/tguard/blob/4afe72c/src/errors.ts#L2)

___

Expand Down
4 changes: 2 additions & 2 deletions docs/classes/Validator.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ An abstract class, which has an `isValid` method, and a `name` property, which r

#### Defined in

[src/types.ts:9](https://github.com/davidkarolyi/tguard/blob/483baa7/src/types.ts#L9)
[src/types.ts:9](https://github.com/davidkarolyi/tguard/blob/4afe72c/src/types.ts#L9)

## Methods

Expand All @@ -72,4 +72,4 @@ value is T

#### Defined in

[src/types.ts:10](https://github.com/davidkarolyi/tguard/blob/483baa7/src/types.ts#L10)
[src/types.ts:10](https://github.com/davidkarolyi/tguard/blob/4afe72c/src/types.ts#L10)
Loading

0 comments on commit 8b9b71f

Please sign in to comment.