This commit is contained in:
26
src/util/effect.ts
Normal file
26
src/util/effect.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { Effect, pipe } from "effect"
|
||||
import { z } from "zod"
|
||||
|
||||
|
||||
/**
|
||||
* Parses a value using a ZodType schema wrapped in an Effect monad.
|
||||
*
|
||||
* @param schema - The ZodType schema to use for parsing.
|
||||
* @param args - The arguments to pass to the `safeParseAsync` method of the schema.
|
||||
* @returns An Effect monad representing the parsing result.
|
||||
*/
|
||||
export const parseZodTypeEffect = <
|
||||
Output,
|
||||
Input,
|
||||
>(
|
||||
schema: z.ZodType<Output, z.ZodTypeDef, Input>,
|
||||
...args: Parameters<typeof schema.safeParseAsync>
|
||||
) => pipe(
|
||||
Effect.promise(() => schema.safeParseAsync(...args)),
|
||||
|
||||
Effect.flatMap(response =>
|
||||
response.success
|
||||
? Effect.succeed(response.data)
|
||||
: Effect.fail(response.error)
|
||||
),
|
||||
)
|
||||
Reference in New Issue
Block a user