Created
January 31, 2023 14:17
-
-
Save fwoelffel/fff1e5327c955c9b6264412561a01da5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
great ! thanks ! ;)