Skip to content

Commit

Permalink
feat: wip Valibot adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEdoRan committed Jul 18, 2024
1 parent 38496f7 commit fff996c
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 8 deletions.
7 changes: 6 additions & 1 deletion packages/next-safe-action/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,22 @@
"tsx": "^4.11.2",
"typescript": "^5.4.5",
"typescript-eslint": "^7.8.0",
"valibot": "^0.36.0",
"zod": "^3.23.6"
},
"peerDependencies": {
"next": ">= 14.0.0",
"react": ">= 18.2.0",
"react-dom": ">= 18.2.0",
"zod": ">= 3.0.0"
"zod": ">= 3.0.0",
"valibot": ">= 0.36.0"
},
"peerDependenciesMeta": {
"zod": {
"optional": true
},
"valibot": {
"optional": true
}
},
"repository": {
Expand Down
32 changes: 28 additions & 4 deletions packages/next-safe-action/src/adapters/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import type { GenericSchema, GenericSchemaAsync, InferInput, InferOutput } from "valibot";
import type { z } from "zod";

export type Schema = z.ZodType;
export type Infer<S extends Schema> = S extends z.ZodType ? z.infer<S> : never;
export type InferIn<S extends Schema> = S extends z.ZodType ? z.input<S> : never;
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
? InferOutput<S>
: never;
export type InferIn<S extends Schema> = S extends z.ZodType
? z.input<S>
: S extends GenericSchema
? InferInput<S>
: S extends GenericSchemaAsync
? InferInput<S>
: never;
export type InferArray<BAS extends readonly Schema[]> = {
[K in keyof BAS]: Infer<BAS[K]>;
};
Expand All @@ -16,7 +29,18 @@ export type ValidationIssue = {
};

export interface ValidationAdapter {
validate<S extends Schema>(
// zod
validate<S extends z.ZodType>(
schema: S,
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>(
schema: S,
data: unknown
): Promise<{ success: true; data: Infer<S> } | { success: false; issues: ValidationIssue[] }>;
Expand Down
27 changes: 27 additions & 0 deletions packages/next-safe-action/src/adapters/valibot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { getDotPath, safeParseAsync, type GenericSchema, type GenericSchemaAsync } from "valibot";
import type { Infer, ValidationAdapter } from "./types";

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

if (result.success) {
return {
success: true,
data: result.output as Infer<S>,
} as const;
}

return {
success: false,
issues: result.issues.map((issue) => ({
message: issue.message,
path: getDotPath(issue)?.split("."),
})),
} as const;
}
}

export function valibotAdapter() {
return new ValibotAdapter();
}
17 changes: 14 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fff996c

Please sign in to comment.