Special numeric sentinel
Validate a computed numeric result that may intentionally be NaN.
Zod · Module
z.nan() creates a schema that accepts JavaScript’s NaN value and rejects all other inputs. Use it when NaN must be validated explicitly rather than treated as an ordinary number.
z.nan() creates a Zod schema dedicated to validating JavaScript's NaN value. Parsing NaN succeeds, while parsing any other value fails with a validation error.
z.nan()A Zod schema that accepts NaN and rejects other values.
import { z } from "zod";
const schema = z.nan();
console.log(schema.parse(NaN)); // NaN
schema.parse("anything else"); // throws ZodErrorCreates a schema that accepts JavaScript NaN and rejects other values.
Validate a computed numeric result that may intentionally be NaN.
Guard an API or function boundary where NaN is an explicitly permitted value.
Use NaN as one branch of a broader numeric schema.
Use z.nan() to define a schema for JavaScript's NaN value. Calling parse(NaN) succeeds, while parsing another value fails.
Follow-up: How would you validate ordinary numeric input instead? Use z.number(), which accepts finite numbers but rejects NaN and Infinity.
No arguments are required: the declaration is z.nan(). It returns a Zod schema whose parse operation accepts NaN.
Follow-up: What should be used if the input may be either NaN or a regular number? Combine schemas with z.union([z.number(), z.nan()]).
z.number() accepts finite numbers, but NaN is not finite and is rejected by that schema. z.nan() exists for the special case where NaN must be accepted.
Follow-up: What happens with Infinity? z.number() also rejects Infinity, and z.nan() rejects it because it only accepts NaN.
z.number()parse()safeParse()z.union()z.infer