Skip to content

Commit

Permalink
feat: add assert/parse/is
Browse files Browse the repository at this point in the history
  • Loading branch information
yamiteru committed Apr 30, 2024
1 parent a7a4ea8 commit 55de328
Show file tree
Hide file tree
Showing 83 changed files with 209 additions and 348 deletions.
32 changes: 19 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ As a result it has many opinions that might not sit well with some folks.
]);

// endpoint
await validate(filename);
await assert(validate, filename);
```

Do this instead:
Expand All @@ -62,7 +62,7 @@ As a result it has many opinions that might not sit well with some folks.
]);

// endpoint
validate(filename);
assert(validate, filename);

if(!(await File.exists(filename))) {
throw Error("File does not exist");
Expand Down Expand Up @@ -158,9 +158,9 @@ As a result it has many opinions that might not sit well with some folks.
<summary><b>How do I validate types?</b></summary>

```ts
string("Hello, World!");
number(420);
boolean(true);
assert(string, "Hello, World!");
assert(number, 420);
assert(boolean, true);
```

</details>
Expand Down Expand Up @@ -202,14 +202,20 @@ As a result it has many opinions that might not sit well with some folks.
friends: array(string)
});

register("Oh no this is gonna throw");

register({
email: "[email protected]",
password: "Test123456",
role: "ADMIN",
friends: ["Joe"]
});
assert(
register,
"Oh no this is gonna throw"
);

assert(
register
{
email: "[email protected]",
password: "Test123456",
role: "ADMIN",
friends: ["Joe"]
}
);
```

</details>
Expand Down
11 changes: 0 additions & 11 deletions src/assertions/and/index.test-d.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/assertions/and/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { maxValue } from "@assertions/maxValue";
import { minValue } from "@assertions/minValue";
import { number } from "@assertions/number";
import { fc, test } from "@fast-check/vitest";
import { assert } from "@utils";
import { assert } from "@utils/assert";
import { expect } from "vitest";
import { and } from "./index";

Expand Down
3 changes: 1 addition & 2 deletions src/assertions/and/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Assertion } from "@the-minimal/types";
import type { Intersection } from "./types";
import type { Assertion, Intersection } from "@the-minimal/types";

/**
* Checks if all the assertions pass.
Expand Down
9 changes: 0 additions & 9 deletions src/assertions/and/types.ts

This file was deleted.

10 changes: 0 additions & 10 deletions src/assertions/and2/index.test-d.ts

This file was deleted.

11 changes: 0 additions & 11 deletions src/assertions/and3/index.test-d.ts

This file was deleted.

6 changes: 0 additions & 6 deletions src/assertions/array/index.test-d.ts

This file was deleted.

5 changes: 0 additions & 5 deletions src/assertions/boolean/index.test-d.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/assertions/boolean/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { boolean } from "@assertions/boolean";
import { fc, test } from "@fast-check/vitest";
import { assert } from "@utils";
import { assert } from "@utils/assert";
import { expect } from "vitest";

test.prop([fc.boolean()])(
Expand Down
5 changes: 0 additions & 5 deletions src/assertions/email/index.test-d.ts

This file was deleted.

5 changes: 0 additions & 5 deletions src/assertions/endsWith/index.test-d.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/assertions/endsWith/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { error } from "@error";
import type { Assertion } from "@the-minimal/types";
import { ValidationError } from "@utils/error";

/**
* Checks if value ends with `searchString`.
Expand All @@ -19,4 +19,4 @@ import type { Assertion } from "@the-minimal/types";
export const endsWith =
<$Input extends string>(input: $Input): Assertion<unknown> =>
(v: unknown) =>
(v as any).endsWith(input) || error(endsWith);
(v as any).endsWith(input) || ValidationError(endsWith);
6 changes: 0 additions & 6 deletions src/assertions/expect/index.test-d.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/assertions/expect/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { error } from "@error";
import type { UnknownAssertion } from "@the-minimal/types";
import { ValidationError } from "@utils/error";

/**
* Wraps assertion and throws an error with message if assertion fails.
Expand All @@ -23,6 +23,6 @@ export const expect = <$Assertion extends UnknownAssertion>(
try {
assertion(v);
} catch (e) {
error(assertion, message(e, v));
ValidationError(assertion, message(e, v));
}
}) as $Assertion;
5 changes: 0 additions & 5 deletions src/assertions/includes/index.test-d.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/assertions/includes/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { error } from "@error";
import type { Assertion } from "@the-minimal/types";
import { ValidationError } from "@utils/error";

/**
* Checks if value includes with another value.
Expand All @@ -17,4 +17,4 @@ import type { Assertion } from "@the-minimal/types";
export const includes =
(input: unknown): Assertion<unknown> =>
(v: unknown) =>
(v as any).includes(input) || error(includes);
(v as any).includes(input) || ValidationError(includes);
5 changes: 0 additions & 5 deletions src/assertions/integer/index.test-d.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/assertions/integer/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { error } from "@error";
import type { Assertion } from "@the-minimal/types";
import { ValidationError } from "@utils/error";

/**
* Checks if value is integer.
Expand All @@ -11,4 +11,4 @@ import type { Assertion } from "@the-minimal/types";
* ```
*/
export const integer: Assertion<unknown> = (v: unknown) =>
Number.isInteger(v) || error(integer);
Number.isInteger(v) || ValidationError(integer);
5 changes: 0 additions & 5 deletions src/assertions/isArray/index.test-d.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/assertions/isArray/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { error } from "@error";
import type { Assertion } from "@the-minimal/types";
import { ValidationError } from "@utils/error";

/**
* Checks that the value is an array.
Expand All @@ -11,4 +11,4 @@ import type { Assertion } from "@the-minimal/types";
* ```
*/
export const isArray: Assertion<unknown[]> = (v: unknown) =>
Array.isArray(v) || error(isArray);
Array.isArray(v) || ValidationError(isArray);
5 changes: 0 additions & 5 deletions src/assertions/isObject/index.test-d.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/assertions/isObject/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { error } from "@error";
import type { Assertion } from "@the-minimal/types";
import { ValidationError } from "@utils/error";

/**
* Checks that the value is of type object and is not null.
Expand All @@ -12,4 +12,4 @@ import type { Assertion } from "@the-minimal/types";
* ```
*/
export const isObject: Assertion<Record<string, unknown>> = (v: unknown) =>
(v !== null && typeof v === "object") || error(isObject);
(v !== null && typeof v === "object") || ValidationError(isObject);
18 changes: 0 additions & 18 deletions src/assertions/lazy/index.test-d.ts

This file was deleted.

5 changes: 0 additions & 5 deletions src/assertions/length/index.test-d.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/assertions/length/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { error } from "@error";
import type { Assertion } from "@the-minimal/types";
import { ValidationError } from "@utils/error";

/**
* Checks if length of value is equal to the provided length.
Expand All @@ -17,4 +17,4 @@ import type { Assertion } from "@the-minimal/types";
export const length =
(input: number): Assertion<unknown> =>
(v: unknown) =>
(v as any).length === input || error(length);
(v as any).length === input || ValidationError(length);
5 changes: 0 additions & 5 deletions src/assertions/maxLength/index.test-d.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/assertions/maxLength/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { error } from "@error";
import type { Assertion } from "@the-minimal/types";
import { ValidationError } from "@utils/error";

/**
* Checks if length of value is less than or equal to the provided length.
Expand All @@ -17,4 +17,4 @@ import type { Assertion } from "@the-minimal/types";
export const maxLength =
(length: number): Assertion<unknown> =>
(v: unknown) =>
(v as any).length <= length || error(maxLength);
(v as any).length <= length || ValidationError(maxLength);
5 changes: 0 additions & 5 deletions src/assertions/maxValue/index.test-d.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/assertions/maxValue/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { error } from "@error";
import { ValidationError } from "@utils/error";

/**
* Checks if value is less than or equal to the provided length.
Expand All @@ -14,4 +14,4 @@ import { error } from "@error";
* ```
*/
export const maxValue = (input: unknown) => (v: unknown) =>
<any>v <= <any>input || error(maxValue);
<any>v <= <any>input || ValidationError(maxValue);
5 changes: 0 additions & 5 deletions src/assertions/minLength/index.test-d.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/assertions/minLength/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { error } from "@error";
import type { Assertion } from "@the-minimal/types";
import { ValidationError } from "@utils/error";

/**
* Checks if length of value is greater than or equal to the provided length.
Expand All @@ -17,4 +17,4 @@ import type { Assertion } from "@the-minimal/types";
export const minLength =
(length: number): Assertion<unknown> =>
(v: unknown) =>
(v as any).length >= length || error(minLength);
(v as any).length >= length || ValidationError(minLength);
5 changes: 0 additions & 5 deletions src/assertions/minValue/index.test-d.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/assertions/minValue/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { error } from "@error";
import type { Assertion } from "@the-minimal/types";
import { ValidationError } from "@utils/error";

/**
* Checks if value is greater than or equal to the provided length.
Expand All @@ -17,4 +17,4 @@ import type { Assertion } from "@the-minimal/types";
export const minValue =
(input: unknown): Assertion<unknown> =>
(v: unknown) =>
<any>v >= <any>input || error(minValue);
<any>v >= <any>input || ValidationError(minValue);
5 changes: 0 additions & 5 deletions src/assertions/modulo/index.test-d.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/assertions/modulo/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { error } from "@error";
import type { Assertion } from "@the-minimal/types";
import { ValidationError } from "@utils/error";

/**
* Checks if the remainer is equal to the specified value when the input is divided by the divider.
Expand All @@ -18,4 +18,4 @@ import type { Assertion } from "@the-minimal/types";
export const modulo =
(divider: number, remainder: number): Assertion<unknown> =>
(v: unknown) =>
<number>v % divider === remainder || error(modulo);
<number>v % divider === remainder || ValidationError(modulo);
Loading

0 comments on commit 55de328

Please sign in to comment.