Skip to content

Proposal: z.number().float() #2237

Answered by xeho91
xeho91 asked this question in Q&A
Mar 24, 2023 · 4 comments · 9 replies
Discussion options

You must be logged in to vote

Close!

It didn't consider the infinite numbers, so I tweaked it slightly.

There's the full sample:

import type { Float } from "type-fest/source/numeric";
import { z } from "zod";

const FLOAT_NUMBER_SCHEMA = z.number().refine((n) => {
    return !z.number().int().safeParse(n).success && z.number().finite().safeParse(n).success;
}, "should not be an integer");

export type { Float } from "type-fest/source/numeric";

export function validateFloatNumber<N extends number>(value: N): asserts value is Float<N> {
    FLOAT_NUMBER_SCHEMA.parse(value);
}

export function isFloatNumber<N extends number>(value: N): value is Float<N> {
    return FLOAT_NUMBER_SCHEMA.safeParse(value).success;
}

And th…

Replies: 4 comments 9 replies

Comment options

You must be logged in to vote
5 replies
@xeho91
Comment options

@JacobWeisenburger
Comment options

@xeho91
Comment options

@JacobWeisenburger
Comment options

@xeho91
Comment options

Comment options

You must be logged in to vote
3 replies
@xeho91
Comment options

Answer selected by JacobWeisenburger
@JacobWeisenburger
Comment options

@JacobWeisenburger
Comment options

Comment options

You must be logged in to vote
1 reply
@JacobWeisenburger
Comment options

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
4 participants