Skip to content

Map ZodEffects back to ZodObject #3281

Answered by JacobWeisenburger
Rmonik asked this question in Q&A
Discussion options

You must be logged in to vote

Perhaps something like this could work as a temporary workaround:

function round ( number: number, decimalPlaces: number ) {
    const factor = Math.pow( 10, decimalPlaces )
    return Math.round( number * factor ) / factor
}

function makeZWage ( baseSchema = z.number() ) {
    return {
        parse ( input: unknown ): number {
            const number = baseSchema.parse( input )
            return round( number, 4 )
        },
        async parseAsync ( input: unknown ): Promise<number> {
            const number = await baseSchema.parseAsync( input )
            return round( number, 4 )
        },
        safeParse ( input: unknown ) {
            const result = baseSchema.safeParse( i…

Replies: 5 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by JacobWeisenburger
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #3257 on February 28, 2024 02:29.