-
What?A way to validate if the provided number is a float (double) one. Example: const FLOAT_NUMBER_SCHEMA = z.number().float(); Why?We already have one which checks if it is an integer: const INTEGER_NUMBER_SCHEMA = z.number().integer(); And in my project, I have a case where I can only accept double numbers. function isFloat(value: number): boolean {
return Number.isFinite(value) && !Number.isInteger(value);
} I believe it would be intuitive to add it to I'd like to know if this proposal would be accepted, before I could volunteer myself for doing a PR for this one. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 9 replies
-
numbers in javascript are floats by default. |
Beta Was this translation helpful? Give feedback.
-
Is this what you are looking for? const schema = z.number()
.refine( n => !z.number().int().safeParse( n ).success, 'should not be integer' )
console.log( schema.parse( 42.42 ) ) // 42.42
const result = schema.safeParse( 42 )
!result.success && console.log( result.error.issues )
// [ { code: 'custom', message: 'should not be integer', path: [] } ] 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.
-
This is working for me,
It accepts both Int and float |
Beta Was this translation helpful? Give feedback.
-
Then use in your schema as This schema in combination with a number input that only allows number and "E" will result in validation that accepts both integers and floats. It will fail validation on
If you type a number that is higher than your max value, it will change the value to the max onBlur. Same for min |
Beta Was this translation helpful? Give feedback.
Close!
It didn't consider the infinite numbers, so I tweaked it slightly.
There's the full sample:
And th…