Skip to content

Instantly share code, notes, and snippets.

@fwoelffel
Created January 31, 2023 14:17
Show Gist options
  • Save fwoelffel/fff1e5327c955c9b6264412561a01da5 to your computer and use it in GitHub Desktop.
Save fwoelffel/fff1e5327c955c9b6264412561a01da5 to your computer and use it in GitHub Desktop.
import { function as fp, either as E } from 'fp-ts';
import type {
SafeParseReturnType,
SafeParseSuccess,
z,
ZodError,
ZodType,
} from 'zod';
const isSafeParseSuccess = <I, O>(
parseResult: SafeParseReturnType<I, O>,
): parseResult is SafeParseSuccess<O> => parseResult.success;
export const safeParseEither = <T extends ZodType>(
type: T,
): ((x: unknown) => E.Either<ZodError<z.input<T>>, z.output<T>>) =>
fp.flow(type.safeParse, (parseResult) =>
isSafeParseSuccess(parseResult)
? E.right(parseResult.data)
: E.left(parseResult.error),
);
@err0r500
Copy link

great ! thanks ! ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment