Skip to content

Commit

Permalink
refactor(types): separate valibot generic schema types
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEdoRan committed Jul 19, 2024
1 parent 6e04a2c commit b61c9cd
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions packages/next-safe-action/src/adapters/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,33 @@ import type { z } from "zod";

export type IfInstalled<T> = any extends T ? never : T;

export type Schema = IfInstalled<z.ZodType> | IfInstalled<GenericSchema | GenericSchemaAsync> | IfInstalled<YupSchema>;
export type Schema =
| IfInstalled<z.ZodType>
| IfInstalled<GenericSchema>
| IfInstalled<GenericSchemaAsync>
| IfInstalled<YupSchema>;

export type Infer<S extends Schema> =
S extends IfInstalled<z.ZodType>
? z.infer<S>
: S extends IfInstalled<GenericSchema | GenericSchemaAsync>
: S extends IfInstalled<GenericSchema>
? InferOutput<S>
: S extends IfInstalled<YupSchema>
? InferType<S>
: never;
: S extends IfInstalled<GenericSchemaAsync>
? InferOutput<S>
: S extends IfInstalled<YupSchema>
? InferType<S>
: never;

export type InferIn<S extends Schema> =
S extends IfInstalled<z.ZodType>
? z.input<S>
: S extends IfInstalled<GenericSchema | GenericSchemaAsync>
: S extends IfInstalled<GenericSchema>
? InferInput<S>
: S extends IfInstalled<YupSchema>
? InferType<S>
: never;
: S extends IfInstalled<GenericSchemaAsync>
? InferInput<S>
: S extends IfInstalled<YupSchema>
? InferType<S>
: never;

export type InferArray<BAS extends readonly Schema[]> = {
[K in keyof BAS]: Infer<BAS[K]>;
Expand All @@ -50,7 +58,11 @@ export interface ValidationAdapter {
data: unknown
): Promise<{ success: true; data: Infer<S> } | { success: false; issues: ValidationIssue[] }>;
// valibot
validate<S extends IfInstalled<GenericSchema | GenericSchemaAsync>>(
validate<S extends IfInstalled<GenericSchema>>(
schema: S,
data: unknown
): Promise<{ success: true; data: Infer<S> } | { success: false; issues: ValidationIssue[] }>;
validate<S extends IfInstalled<GenericSchemaAsync>>(
schema: S,
data: unknown
): Promise<{ success: true; data: Infer<S> } | { success: false; issues: ValidationIssue[] }>;
Expand Down

0 comments on commit b61c9cd

Please sign in to comment.