Skip to content

Commit

Permalink
suggestions for effect plugin (#3449)
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed Apr 27, 2024
1 parent f46279f commit 6f56869
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions plugin/effect/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import { Effect } from "effect";
import * as Effect from "effect/Effect";
import * as z from "zod";

function zodEffect(this: z.ZodType, data: unknown, params?: any) {
return Effect.tryPromise({
try: async () => this.parseAsync(data, params),
catch(error) {
return error as z.ZodError;
},
});
return Effect.flatMap(
Effect.promise(() => this.safeParseAsync(data, params)),
(result) =>
result.success ? Effect.succeed(result.data) : Effect.fail(result.error)
);
}

function zodEffectSync(this: z.ZodType, data: unknown, params?: any) {
return Effect.try({
try: () => this.parse(data, params),
catch(error) {
return error as z.ZodError;
},
return Effect.suspend(() => {
const result = this.safeParse(data, params);
return result.success
? Effect.succeed(result.data)
: Effect.fail(result.error);
});
}

Expand All @@ -31,10 +30,10 @@ declare module "zod" {
interface ZodType {
effect(
...args: Parameters<z.ZodType["parseAsync"]>
): Effect.Effect<this["_output"], z.ZodError<this["_output"]>, never>;
): Effect.Effect<this["_output"], z.ZodError<this["_output"]>>;
effectSync(
...args: Parameters<z.ZodType["parse"]>
): Effect.Effect<this["_output"], z.ZodError<this["_output"]>, never>;
): Effect.Effect<this["_output"], z.ZodError<this["_output"]>>;
}

interface ZodError {
Expand Down

0 comments on commit 6f56869

Please sign in to comment.