Skip to content

Commit

Permalink
feat: type improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
yamiteru committed Apr 21, 2024
1 parent 048c783 commit 52e6212
Show file tree
Hide file tree
Showing 57 changed files with 125 additions and 123 deletions.
14 changes: 0 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,6 @@ yarn add @the-minimal/validator
- Fully tested with 100% coverage
- Zero runtime dependencies

## Philosophy

Validator doesn't support data transformations or coercions.

We assume that data is already transformed by the sender.

We assume that you control both the sender and receiver.

We assume that Validator is used only for validating JSON.

This allows us to make the library much smaller and faster.

> If you want to transform data during or right after validation then you should make sure that the sender sends already transformed data instead
## Example

```ts
Expand Down
4 changes: 2 additions & 2 deletions src/assertions/and/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import type { Assertion } from "@the-minimal/types";
* ```
*/
export const and =
<const $Schema extends AndSchema, $Infered = InferAndSchema<$Schema>>(
<const $Schema extends AndSchema>(
assertions: $Schema,
): Assertion<$Infered> =>
): Assertion<InferAndSchema<$Schema>> =>
(v) => {
for (let i = 0; i < assertions.length; ++i) {
(assertions[i] as any)(v);
Expand Down
8 changes: 4 additions & 4 deletions src/assertions/and2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import type { Assertion } from "@the-minimal/types";
* ```
*/
export const and2 =
<$Value1, $Value2>(
assertion1: Assertion<$Value1>,
assertion2: Assertion<$Value2>,
): Assertion<$Value1 & $Value2> =>
<$Input1, $Input2>(
assertion1: Assertion<$Input1>,
assertion2: Assertion<$Input2>,
): Assertion<$Input1 & $Input2> =>
(v) => {
assertion1(v);
assertion2(v);
Expand Down
10 changes: 5 additions & 5 deletions src/assertions/and3/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import type { Assertion } from "@the-minimal/types";
* ```
*/
export const and3 =
<$Value1, $Value2, $Value3>(
assertion1: Assertion<$Value1>,
assertion2: Assertion<$Value2>,
assertion3: Assertion<$Value3>,
): Assertion<$Value1 & $Value2 & $Value3> =>
<$Input1, $Input2, $Input3>(
assertion1: Assertion<$Input1>,
assertion2: Assertion<$Input2>,
assertion3: Assertion<$Input3>,
): Assertion<$Input1 & $Input2 & $Input3> =>
(v) => {
assertion1(v);
assertion2(v);
Expand Down
2 changes: 1 addition & 1 deletion src/assertions/array/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type { Assertion } from "@the-minimal/types";
* ```
*/
export const array =
<$Value>(assertion: Assertion<$Value>): Assertion<Array<$Value>> =>
<$Input>(assertion: Assertion<$Input>): Assertion<Array<$Input>> =>
(v) => {
isArray(v);

Expand Down
3 changes: 2 additions & 1 deletion src/assertions/email/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { email } from "@assertions/email";
import type { Email } from "@assertions/email/types";
import type { Assertion } from "@the-minimal/types";
import { assertType } from "vitest";

assertType<Assertion<string>>(email);
assertType<Assertion<Email>>(email);
3 changes: 2 additions & 1 deletion src/assertions/endsWith/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { endsWith } from "@assertions/endsWith";
import type { EndsWith } from "@assertions/endsWith/types";
import type { Assertion } from "@the-minimal/types";
import { assertType } from "vitest";

assertType<Assertion<string>>(endsWith("?"));
assertType<Assertion<EndsWith<"?">>>(endsWith("?"));
2 changes: 1 addition & 1 deletion src/assertions/endsWith/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import type { Brand } from "@the-minimal/types";

export type EndsWith<$Value> = Brand<"EndsWith", $Value>;
export type EndsWith<$Input> = Brand<"EndsWith", $Input>;
2 changes: 1 addition & 1 deletion src/assertions/expect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type { Assertion } from "@the-minimal/types";
* ```
*/
export const expect =
<$Type>(assertion: Assertion<$Type>, message: Message): Assertion<$Type> =>
<$Input>(assertion: Assertion<$Input>, message: Message): Assertion<$Input> =>
(v) => {
try {
assertion(v);
Expand Down
2 changes: 1 addition & 1 deletion src/assertions/expect/types.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export type Message = (error: any, value: unknown) => string;
export type Message = (error: any, input: unknown) => string;
3 changes: 2 additions & 1 deletion src/assertions/includes/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { includes } from "@assertions/includes";
import type { Includes } from "@assertions/includes/types";
import type { Assertion } from "@the-minimal/types";
import { assertType } from "vitest";

assertType<Assertion<string>>(includes("hello"));
assertType<Assertion<Includes<"string">>>(includes("hello"));
6 changes: 3 additions & 3 deletions src/assertions/includes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { Assertion } from "@the-minimal/types";
/**
* Checks if value includes with another value.
*
* @param value - Value used in matching.
* @param input - Value used in matching.
*
* @example
* ```ts
Expand All @@ -16,6 +16,6 @@ import type { Assertion } from "@the-minimal/types";
* ```
*/
export const includes =
<$Type>(value: $Type): Assertion<Includes<$Type>> =>
<$Input>(input: $Input): Assertion<Includes<$Input>> =>
(v: any) =>
v.includes(value) || error(includes);
v.includes(input) || error(includes);
2 changes: 1 addition & 1 deletion src/assertions/includes/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import type { Brand } from "@the-minimal/types";

export type Includes<$Value> = Brand<"Includes", $Value>;
export type Includes<$Input> = Brand<"Includes", $Input>;
3 changes: 2 additions & 1 deletion src/assertions/integer/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Integer } from "@assertions/integer/types";
import type { Assertion } from "@the-minimal/types";
import { assertType } from "vitest";
import { integer } from ".";

assertType<Assertion<number>>(integer);
assertType<Assertion<Integer>>(integer);
4 changes: 2 additions & 2 deletions src/assertions/isArray/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ import type { Assertion } from "@the-minimal/types";
* isArray([]); // passes
* ```
*/
export const isArray: Assertion<Array<unknown>> = (v) =>
Array.isArray(v) || error(isArray);
export const isArray: Assertion<Array<unknown>> = ((v) =>
Array.isArray(v) || error(isArray)) as Assertion<Array<unknown>>;
5 changes: 3 additions & 2 deletions src/assertions/isObject/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ import type { Assertion } from "@the-minimal/types";
* isObject({}); // passes
* ```
*/
export const isObject: Assertion<ObjectUnknown> = (v) =>
(v !== null && typeof v === "object") || error(isObject);
export const isObject: Assertion<ObjectUnknown> = ((v) =>
(v !== null && typeof v === "object") ||
error(isObject)) as Assertion<ObjectUnknown>;
6 changes: 3 additions & 3 deletions src/assertions/lazy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import type { UnknownAssertion } from "@the-minimal/types";
* ```
*/
export const lazy = <$Validation extends UnknownAssertion>(
assertion: (value: unknown) => $Validation,
assertion: (input: unknown) => $Validation,
) =>
((value: unknown) => {
(assertion(value) as any)(value);
((input: unknown) => {
(assertion(input) as any)(input);
}) as $Validation;
4 changes: 3 additions & 1 deletion src/assertions/length/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { length } from "@assertions/length";
import type { Length } from "@assertions/length/types";
import type { Assertion } from "@the-minimal/types";
import { assertType } from "vitest";

assertType<{ length: number }>(length(2));
assertType<Assertion<Length<2>>>(length(2));
6 changes: 3 additions & 3 deletions src/assertions/length/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { Assertion } from "@the-minimal/types";
/**
* Checks if length of value is equal to the provided length.
*
* @param value - Length used in the comparison.
* @param input - Length used in the comparison.
*
* @example
* ```ts
Expand All @@ -16,6 +16,6 @@ import type { Assertion } from "@the-minimal/types";
* ```
*/
export const length =
<$Value extends number>(value: $Value): Assertion<Length<$Value>> =>
<$Input extends number>(input: $Input): Assertion<Length<$Input>> =>
(v: any) =>
v.length === value || error(length);
v.length === input || error(length);
2 changes: 1 addition & 1 deletion src/assertions/length/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import type { Brand } from "@the-minimal/types";

export type Length<$Value extends number> = Brand<"Length", $Value>;
export type Length<$Input extends number> = Brand<"Length", $Input>;
4 changes: 3 additions & 1 deletion src/assertions/maxLength/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { maxLength } from "@assertions/maxLength";
import type { MaxLength } from "@assertions/maxLength/types";
import type { Assertion } from "@the-minimal/types";
import { assertType } from "vitest";

assertType<{ length: number }>(maxLength(2));
assertType<Assertion<MaxLength<2>>>(maxLength(2));
2 changes: 1 addition & 1 deletion src/assertions/maxLength/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ import type { Assertion } from "@the-minimal/types";
* ```
*/
export const maxLength =
<$Value extends number>(length: $Value): Assertion<MaxLength<$Value>> =>
<$Input extends number>(length: $Input): Assertion<MaxLength<$Input>> =>
(v: any) =>
v.length <= length || error(maxLength);
2 changes: 1 addition & 1 deletion src/assertions/maxLength/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import type { Brand } from "@the-minimal/types";

export type MaxLength<$Value extends number> = Brand<"MaxLength", $Value>;
export type MaxLength<$Input extends number> = Brand<"MaxLength", $Input>;
4 changes: 3 additions & 1 deletion src/assertions/maxValue/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { maxValue } from "@assertions/maxValue";
import type { MaxValue } from "@assertions/maxValue/types";
import type { Assertion } from "@the-minimal/types";
import { assertType } from "vitest";

assertType<unknown>(maxValue(17));
assertType<Assertion<MaxValue<17>>>(maxValue(17));
6 changes: 3 additions & 3 deletions src/assertions/maxValue/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { Assertion } from "@the-minimal/types";
/**
* Checks if value is less than or equal to the provided length.
*
* @param value - Value used in the comparison.
* @param input - Value used in the comparison.
*
* @example
* ```ts
Expand All @@ -16,6 +16,6 @@ import type { Assertion } from "@the-minimal/types";
* ```
*/
export const maxValue =
<$Type>(value: $Type): Assertion<MaxValue<$Type>> =>
<const $Input>(input: $Input): Assertion<MaxValue<$Input>> =>
(v: any) =>
v <= value || error(maxValue);
v <= input || error(maxValue);
2 changes: 1 addition & 1 deletion src/assertions/maxValue/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import type { Brand } from "@the-minimal/types";

export type MaxValue<$Value> = Brand<"MaxValue", $Value>;
export type MaxValue<$Input> = Brand<"MaxValue", $Input>;
4 changes: 3 additions & 1 deletion src/assertions/minLength/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { minLength } from "@assertions/minLength";
import type { MinLength } from "@assertions/minLength/types";
import type { Assertion } from "@the-minimal/types";
import { assertType } from "vitest";

assertType<{ length: number }>(minLength(2));
assertType<Assertion<MinLength<2>>>(minLength(2));
2 changes: 1 addition & 1 deletion src/assertions/minLength/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ import type { Assertion } from "@the-minimal/types";
* ```
*/
export const minLength =
<$Value extends number>(length: $Value): Assertion<MinLength<$Value>> =>
<$Input extends number>(length: $Input): Assertion<MinLength<$Input>> =>
(v: any) =>
v.length >= length || error(minLength);
2 changes: 1 addition & 1 deletion src/assertions/minLength/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import type { Brand } from "@the-minimal/types";

export type MinLength<$Value extends number> = Brand<"MinLength", $Value>;
export type MinLength<$Input extends number> = Brand<"MinLength", $Input>;
4 changes: 3 additions & 1 deletion src/assertions/minValue/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { minValue } from "@assertions/minValue";
import type { MinValue } from "@assertions/minValue/types";
import type { Assertion } from "@the-minimal/types";
import { assertType } from "vitest";

assertType<unknown>(minValue(18));
assertType<Assertion<MinValue<18>>>(minValue(18));
6 changes: 3 additions & 3 deletions src/assertions/minValue/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { Assertion } from "@the-minimal/types";
/**
* Checks if value is greater than or equal to the provided length.
*
* @param value - Value used in the comparison.
* @param input - Value used in the comparison.
*
* @example
* ```ts
Expand All @@ -16,6 +16,6 @@ import type { Assertion } from "@the-minimal/types";
* ```
*/
export const minValue =
<$Type>(value: $Type): Assertion<MinValue<$Type>> =>
<const $Input>(input: $Input): Assertion<MinValue<$Input>> =>
(v: any) =>
v >= value || error(minValue);
v >= input || error(minValue);
2 changes: 1 addition & 1 deletion src/assertions/minValue/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import type { Brand } from "@the-minimal/types";

export type MinValue<$Value> = Brand<"MinValue", $Value>;
export type MinValue<$Input> = Brand<"MinValue", $Input>;
4 changes: 3 additions & 1 deletion src/assertions/notLength/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { notLength } from "@assertions/notLength";
import type { NotLength } from "@assertions/notLength/types";
import type { Assertion } from "@the-minimal/types";
import { assertType } from "vitest";

assertType<{ length: number }>(notLength(2));
assertType<Assertion<NotLength<2>>>(notLength(2));
2 changes: 1 addition & 1 deletion src/assertions/notLength/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ import type { Assertion } from "@the-minimal/types";
* ```
*/
export const notLength =
<$Value extends number>(length: $Value): Assertion<NotLength<$Value>> =>
<$Input extends number>(length: $Input): Assertion<NotLength<$Input>> =>
(v: any) =>
v.length !== length || error(notLength);
2 changes: 1 addition & 1 deletion src/assertions/notLength/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import type { Brand } from "@the-minimal/types";

export type NotLength<$Value extends number> = Brand<"NotLength", $Value>;
export type NotLength<$Input extends number> = Brand<"NotLength", $Input>;
3 changes: 2 additions & 1 deletion src/assertions/notValue/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { notValue } from "@assertions/notValue";
import type { NotValue } from "@assertions/notValue/types";
import type { Assertion } from "@the-minimal/types";
import { assertType } from "vitest";

assertType<Assertion<number>>(notValue(2));
assertType<Assertion<NotValue<2>>>(notValue(2));
6 changes: 3 additions & 3 deletions src/assertions/notValue/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { Assertion } from "@the-minimal/types";
/**
* Checks if value is not equal to the provided value.
*
* @param value - Value used in the comparison.
* @param input - Value used in the comparison.
*
* @example
* ```ts
Expand All @@ -16,6 +16,6 @@ import type { Assertion } from "@the-minimal/types";
* ```
*/
export const notValue =
<$Type>(value: $Type): Assertion<NotValue<$Type>> =>
<const $Input>(input: $Input): Assertion<NotValue<$Input>> =>
(v) =>
v !== value || error(notValue);
v !== input || error(notValue);
2 changes: 1 addition & 1 deletion src/assertions/notValue/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import type { Brand } from "@the-minimal/types";

export type NotValue<$Value> = Brand<"NotValue", $Value>;
export type NotValue<$Input> = Brand<"NotValue", $Input>;
2 changes: 1 addition & 1 deletion src/assertions/nullable/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ import type { Assertion, Nullable } from "@the-minimal/types";
* ```
*/
export const nullable =
<$Value>(assertion: Assertion<$Value>): Assertion<Nullable<$Value>> =>
<$Input>(assertion: Assertion<$Input>): Assertion<Nullable<$Input>> =>
(v) =>
v === null || assertion(v);
2 changes: 1 addition & 1 deletion src/assertions/nullish/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ import type { Assertion, Nullish } from "@the-minimal/types";
* ```
*/
export const nullish =
<$Value>(assertion: Assertion<$Value>): Assertion<Nullish<$Value>> =>
<$Input>(assertion: Assertion<$Input>): Assertion<Nullish<$Input>> =>
(v) =>
v === null || v === undefined || assertion(v);
2 changes: 1 addition & 1 deletion src/assertions/optional/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ import type { Assertion, Optional } from "@the-minimal/types";
* ```
*/
export const optional =
<$Value>(assertion: Assertion<$Value>): Assertion<Optional<$Value>> =>
<$Input>(assertion: Assertion<$Input>): Assertion<Optional<$Input>> =>
(v) =>
v === undefined || assertion(v);
Loading

0 comments on commit 52e6212

Please sign in to comment.