Map ZodEffects back to ZodObject #3281
-
I may be missing something, but there appears to be no way to assert that a As a quick example:
Is there any way around this? |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
Is this what you are looking for? const zWage = z.number()
.transform( x => Math.round( x * 10000 ) / 10000 )
.pipe( z.number().min( 34.345345 ) )
console.log( zWage.parse( 123.456789 ) ) // 123.4568 If you found my answer satisfactory, please consider supporting me. Even a small amount is greatly appreciated. Thanks friend! 🙏 |
Beta Was this translation helpful? Give feedback.
-
No -- We have a fairly large codebase that uses Zod for hundreds of API calls. I want to make a generalistic zWage which can be used everywhere where we want it to round to 4 decimal places. After that, however, i would like to be able to still call zWage.min(), or any other method that can be called a z.number(). I don't understand the benefit of the ZodEffects wrapper; it's only inconvenient to me that this is no longer seen as a ZodNumber. |
Beta Was this translation helpful? Give feedback.
-
As far as I know, that is not possible and would require quite a significant change to how zod works. |
Beta Was this translation helpful? Give feedback.
-
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( input )
if ( !result.success ) return result
return {
...result,
data: round( result.data, 4 )
}
},
async safeParseAsync ( input: unknown ) {
const result = await baseSchema.safeParseAsync( input )
if ( !result.success ) return result
return {
...result,
data: round( result.data, 4 )
}
},
min ( value: number ) {
return makeZWage( baseSchema.min( value ) )
},
max ( value: number ) {
return makeZWage( baseSchema.max( value ) )
},
}
}
const zWage = makeZWage()
console.log( zWage.parse( 123.456789 ) ) // 123.4568
console.log( zWage.min( 34.345345 ).parse( 123.456789 ) ) // 123.4568
console.log( zWage.safeParse( 123.456789 ) ) // { success: true, data: 123.4568 }
console.log( await zWage.parseAsync( 123.456789 ) ) // 123.4568
console.log( await zWage.safeParseAsync( 123.456789 ) ) // { success: true, data: 123.4568 } If you found my answer satisfactory, please consider supporting me. Even a small amount is greatly appreciated. Thanks friend! 🙏 |
Beta Was this translation helpful? Give feedback.
-
Did my solution help you? or would you like this issue to remain open as a new feature request? |
Beta Was this translation helpful? Give feedback.
Perhaps something like this could work as a temporary workaround: