Skip to content

Commit

Permalink
refactor(types): rename Exists to IfInstalled
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEdoRan committed Jul 19, 2024
1 parent 9c7d191 commit eb8c7d8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
20 changes: 10 additions & 10 deletions packages/next-safe-action/src/adapters/types.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import type { GenericSchema, GenericSchemaAsync, InferInput, InferOutput } from "valibot";
import type { z } from "zod";

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

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

class ValibotAdapter implements ValidationAdapter {
async validate<S extends GenericSchema | GenericSchemaAsync>(schema: S, data: unknown) {
async validate<S extends IfInstalled<GenericSchema | GenericSchemaAsync>>(schema: S, data: unknown) {
const result = await safeParseAsync(schema, data);

if (result.success) {
Expand Down
4 changes: 2 additions & 2 deletions packages/next-safe-action/src/adapters/zod.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { z } from "zod";
import type { Infer, ValidationAdapter } from "./types";
import type { IfInstalled, Infer, ValidationAdapter } from "./types";

export type ZodSchema = z.ZodType;

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

if (result.success) {
Expand Down

0 comments on commit eb8c7d8

Please sign in to comment.