Skip to content

Commit

Permalink
feat: take normal input and infer assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
yamiteru committed Apr 21, 2024
1 parent 52e6212 commit 2e54bd8
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 33 deletions.
12 changes: 8 additions & 4 deletions src/assertions/and/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type { AndSchema, InferAndSchema } from "@assertions/and/types";
import type {
AndSchema,
InferAndOutput,
InferAndSchema,
} from "@assertions/and/types";
import type { Assertion } from "@the-minimal/types";

/**
Expand All @@ -21,10 +25,10 @@ import type { Assertion } from "@the-minimal/types";
*/
export const and =
<const $Schema extends AndSchema>(
assertions: $Schema,
): Assertion<InferAndSchema<$Schema>> =>
assertions: InferAndSchema<$Schema>,
): Assertion<InferAndOutput<$Schema>> =>
(v) => {
for (let i = 0; i < assertions.length; ++i) {
(assertions[i] as any)(v);
((assertions as any)[i] as any)(v);
}
};
23 changes: 14 additions & 9 deletions src/assertions/and/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import type { UnknownAssertion } from "@the-minimal/types";
import type { Infer } from "@types";
import type { Assertion } from "@the-minimal/types";

export type AndSchema = Array<UnknownAssertion>;
export type AndSchema = Array<unknown>;

export type InferAndSchema<$Validations extends AndSchema> =
$Validations extends [infer $Head, ...infer $Tail]
? $Tail extends [infer $1, ...infer $2]
? Infer<$Head> & InferAndSchema<$Tail>
: Infer<$Head>
: never;
export type InferAndSchema<$Schema extends AndSchema> = {
[$Key in keyof $Schema]: Assertion<$Schema[$Key]>;
};

export type InferAndOutput<$Schema extends AndSchema> = $Schema extends [
infer $Head,
...infer $Tail,
]
? $Tail extends [infer $1, ...infer $2]
? $Head & InferAndOutput<$Tail>
: $Head
: never;
5 changes: 3 additions & 2 deletions src/assertions/object/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { isObject } from "@assertions/isObject";
import type { InferObjectSchema, ObjectSchema } from "@assertions/object/types";
import { string } from "@assertions/string";
import type { Assertion } from "@the-minimal/types";

/**
Expand Down Expand Up @@ -27,8 +28,8 @@ import type { Assertion } from "@the-minimal/types";
* ```
*/
export const object = <$Schema extends ObjectSchema>(
schema: $Schema,
): Assertion<InferObjectSchema<$Schema>> => {
schema: InferObjectSchema<$Schema>,
): Assertion<$Schema> => {
const keys = Object.keys(schema);

return (v) => {
Expand Down
11 changes: 4 additions & 7 deletions src/assertions/object/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import type {
InferAssertion,
Pretty,
UnknownAssertion,
} from "@the-minimal/types";
import type { ObjectUnknown } from "@assertions/isObject/types";
import type { Assertion, Pretty } from "@the-minimal/types";

export type ObjectSchema = Record<string, UnknownAssertion>;
export type ObjectSchema = ObjectUnknown;

export type InferObjectSchema<$Schema extends ObjectSchema> = Pretty<{
[$Key in keyof $Schema]: InferAssertion<$Schema[$Key]>;
[$Key in keyof $Schema]: Assertion<$Schema[$Key]>;
}>;
4 changes: 2 additions & 2 deletions src/assertions/or/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import type { Assertion } from "@the-minimal/types";
*/
export const or =
<const $Validations extends OrSchema>(
validations: $Validations,
): Assertion<InferOrSchema<$Validations>> =>
validations: InferOrSchema<$Validations>,
): Assertion<$Validations[number]> =>
(v) => {
for (let i = 0; i < validations.length; ++i) {
try {
Expand Down
12 changes: 8 additions & 4 deletions src/assertions/or/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import type { InferAssertion, UnknownAssertion } from "@the-minimal/types";
import type {
Assertion,
InferAssertion,
UnknownAssertion,
} from "@the-minimal/types";

export type OrSchema = Array<UnknownAssertion>;
export type OrSchema = Array<unknown>;

export type InferOrSchema<$Schema extends OrSchema> = {
[$Key in keyof $Schema]: InferAssertion<$Schema[$Key]>;
}[number];
[$Key in keyof $Schema]: Assertion<$Schema[$Key]>;
};
4 changes: 2 additions & 2 deletions src/assertions/tuple/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import type { Assertion } from "@the-minimal/types";
*/
export const tuple =
<const $Schema extends TupleSchema>(
assertions: $Schema,
): Assertion<InferTupleSchema<$Schema>> =>
assertions: InferTupleSchema<$Schema>,
): Assertion<$Schema> =>
(v) => {
isArray(v);

Expand Down
6 changes: 3 additions & 3 deletions src/assertions/tuple/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { InferAssertion, UnknownAssertion } from "@the-minimal/types";
import type { Assertion } from "@the-minimal/types";

export type TupleSchema = Array<UnknownAssertion>;
export type TupleSchema = Array<unknown>;

export type InferTupleSchema<$Schema extends TupleSchema> = {
[$Key in keyof $Schema]: InferAssertion<$Schema[$Key]>;
[$Key in keyof $Schema]: Assertion<$Schema[$Key]>;
};

0 comments on commit 2e54bd8

Please sign in to comment.