Skip to content

Commit

Permalink
feat: Add TConstant compound validator
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkarolyi committed Mar 7, 2022
1 parent 483baa7 commit bb14c51
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 56 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ A function that returns a [Validator](#validators), called a compound validator.

- [TAnd](/docs/modules.md#tand)
- [TArray](/docs/modules.md#tarray)
- [TConstant](/docs/modules.md#tconstant)
- [TNot](/docs/modules.md#tnot)
- [TObjectOfShape](/docs/modules.md#tobjectofshape)
- [TOr](/docs/modules.md#tor)
Expand Down
32 changes: 30 additions & 2 deletions __tests__/validators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
TStringURL,
TStringUUID,
TValidate,
TConstant,
} from "../src/validators";

describe("Validators", () => {
Expand Down Expand Up @@ -671,13 +672,40 @@ describe("Validators", () => {
expect(TValidate("custom", () => true).name).toBe("custom");
});

it("returns false, if the given function return false", () => {
it("returns false, if the given function returns false", () => {
expect(TValidate("custom", () => false).isValid("foobar")).toBe(false);
});

it("returns true, if the input is UUID", () => {
it("returns true, if the given function returns true", () => {
expect(TValidate("custom", () => true).isValid("foobar")).toBe(true);
});
});

describe("TConstant", () => {
it("is an instance of Validator", () => {
expect(TConstant(5)).toBeInstanceOf(Validator);
});

it("it's name is 'constant(<constant>)'", () => {
expect(TConstant(5).name).toBe("constant(5)");
expect(TConstant("foo").name).toBe('constant("foo")');
expect(TConstant(BigInt(100)).name).toBe("constant(100)");
expect(TConstant(true).name).toBe("constant(true)");
});

it("returns false, if the given value is not equal to the constant", () => {
expect(TConstant("foo").isValid("foobar")).toBe(false);
expect(TConstant(2).isValid(2.1)).toBe(false);
expect(TConstant(true).isValid(false)).toBe(false);
expect(TConstant(2).isValid("2")).toBe(false);
});

it("returns true, if the input is UUID", () => {
expect(TConstant("foo").isValid("foo")).toBe(true);
expect(TConstant(2).isValid(2)).toBe(true);
expect(TConstant(false).isValid(false)).toBe(true);
expect(TConstant("2").isValid("2")).toBe(true);
});
});
});
});
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ A function that returns a [Validator](#validators), called a compound validator.

- [TAnd](/docs/modules.md#tand)
- [TArray](/docs/modules.md#tarray)
- [TConstant](/docs/modules.md#tconstant)
- [TNot](/docs/modules.md#tnot)
- [TObjectOfShape](/docs/modules.md#tobjectofshape)
- [TOr](/docs/modules.md#tor)
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/f6f4bca/src/guard.ts#L67)
[src/guard.ts:67](https://github.com/davidkarolyi/tguard/blob/483baa7/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/f6f4bca/src/guard.ts#L60)
[src/guard.ts:60](https://github.com/davidkarolyi/tguard/blob/483baa7/src/guard.ts#L60)

___

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

#### Defined in

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

## Accessors

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

#### Defined in

[src/guard.ts:76](https://github.com/davidkarolyi/tguard/blob/f6f4bca/src/guard.ts#L76)
[src/guard.ts:76](https://github.com/davidkarolyi/tguard/blob/483baa7/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/f6f4bca/src/guard.ts#L121)
[src/guard.ts:121](https://github.com/davidkarolyi/tguard/blob/483baa7/src/guard.ts#L121)

___

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

#### Defined in

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

___

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

#### Defined in

[src/guard.ts:157](https://github.com/davidkarolyi/tguard/blob/f6f4bca/src/guard.ts#L157)
[src/guard.ts:157](https://github.com/davidkarolyi/tguard/blob/483baa7/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/f6f4bca/src/guard.ts#L86)
[src/guard.ts:86](https://github.com/davidkarolyi/tguard/blob/483baa7/src/guard.ts#L86)

___

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

#### Defined in

[src/guard.ts:140](https://github.com/davidkarolyi/tguard/blob/f6f4bca/src/guard.ts#L140)
[src/guard.ts:140](https://github.com/davidkarolyi/tguard/blob/483baa7/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/f6f4bca/src/errors.ts#L5)
[src/errors.ts:5](https://github.com/davidkarolyi/tguard/blob/483baa7/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/f6f4bca/src/errors.ts#L3)
[src/errors.ts:3](https://github.com/davidkarolyi/tguard/blob/483baa7/src/errors.ts#L3)

___

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

#### Defined in

[src/errors.ts:2](https://github.com/davidkarolyi/tguard/blob/f6f4bca/src/errors.ts#L2)
[src/errors.ts:2](https://github.com/davidkarolyi/tguard/blob/483baa7/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/f6f4bca/src/types.ts#L9)
[src/types.ts:9](https://github.com/davidkarolyi/tguard/blob/483baa7/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/f6f4bca/src/types.ts#L10)
[src/types.ts:10](https://github.com/davidkarolyi/tguard/blob/483baa7/src/types.ts#L10)
Loading

0 comments on commit bb14c51

Please sign in to comment.