z.number().multipleOf(0.01)
will do the hack(yes, it can work with non-integer too!).
However, I worry about IEEE 754 representation issue(it even has its own name - but I forgot - and is not specific to JS), when
z.number().multipleOf(0.01).parse(0.1 + 0.1 + 0.1)
will throw, since 0.1 + 0.1 + 0.1 under the hood becomes 0.300000000004
Maybe refine()
will be more reliable with checking against Number.EPSILON
:
z.number()
.refine(x => x * 100 - Math.trunc(x * 100)< Number.EPSILON)
.parse(0.1 + 0.1 + 0.1) // ok
[upd] as @captain-yossarianfromUkraine noticed, Number.EPSILON should help with float-number thing
PS cannot evaluate this approach against z.custom()
proposed by @vera