Files
zod-schema-class/src/util/effect.ts
Julien Valverdé ab328f2cb5
All checks were successful
continuous-integration/drone/push Build is passing
Util refactoring
2024-01-28 01:08:15 +01:00

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)
),
)