Skip to content

Commit

Permalink
fix(types): Schema adapter detection
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEdoRan committed Jul 19, 2024
1 parent 52d09b7 commit 9c7d191
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
37 changes: 18 additions & 19 deletions packages/next-safe-action/src/adapters/types.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import type { GenericSchema, GenericSchemaAsync, InferInput, InferOutput } from "valibot";
import type { z } from "zod";

export type Schema = z.ZodType | GenericSchema | GenericSchemaAsync;
export type Infer<S extends Schema> = S extends z.ZodType
? z.infer<S>
: S extends GenericSchema
? InferOutput<S>
: S extends GenericSchemaAsync
export type Exists<T> = any extends T ? never : T;

export type Schema = Exists<z.ZodType> | Exists<GenericSchema> | Exists<GenericSchemaAsync>;
export type Infer<S extends Schema> =
S extends Exists<z.ZodType>
? z.infer<S>
: S extends Exists<GenericSchema>
? InferOutput<S>
: never;
export type InferIn<S extends Schema> = S extends z.ZodType
? z.input<S>
: S extends GenericSchema
? InferInput<S>
: S extends GenericSchemaAsync
: S extends Exists<GenericSchemaAsync>
? InferOutput<S>
: never;
export type InferIn<S extends Schema> =
S extends Exists<z.ZodType>
? z.input<S>
: S extends Exists<GenericSchema>
? InferInput<S>
: never;
: S extends Exists<GenericSchemaAsync>
? InferInput<S>
: never;
export type InferArray<BAS extends readonly Schema[]> = {
[K in keyof BAS]: Infer<BAS[K]>;
};
Expand All @@ -40,12 +44,7 @@ export interface ValidationAdapter {
data: unknown
): Promise<{ success: true; data: Infer<S> } | { success: false; issues: ValidationIssue[] }>;
// valibot
validate<S extends GenericSchema>(
schema: S,
data: unknown
): Promise<{ success: true; data: Infer<S> } | { success: false; issues: ValidationIssue[] }>;
// valibot
validate<S extends GenericSchemaAsync>(
validate<S extends GenericSchema | GenericSchemaAsync>(
schema: S,
data: unknown
): Promise<{ success: true; data: Infer<S> } | { success: false; issues: ValidationIssue[] }>;
Expand Down
2 changes: 2 additions & 0 deletions packages/next-safe-action/src/adapters/zod.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { z } from "zod";
import type { Infer, ValidationAdapter } from "./types";

export type ZodSchema = z.ZodType;

class ZodAdapter implements ValidationAdapter {
async validate<S extends z.ZodType>(schema: S, data: unknown) {
const result = await schema.safeParseAsync(data);
Expand Down

0 comments on commit 9c7d191

Please sign in to comment.