27 lines
740 B
TypeScript
27 lines
740 B
TypeScript
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)
|
|
),
|
|
)
|