Capítulo 9 de 61

Chapter 9: Integers, BigInts & Booleans

Core Idea

Beyond general numbers, Zod has dedicated schemas for integer ranges, arbitrary-precision integers (bigint), and booleans, each with the matching range/sign checks.

Key Concepts

  • z.int(): restricts to JS's safe integer range (Number.isSafeInteger).
  • z.int32(): restricts to the 32-bit signed integer range specifically.
  • z.bigint(): validates JS bigint values, with the same family of checks as numbers: .gt(), .gte() (.min()), .lt(), .lte() (.max()), .positive(), .nonnegative(), .negative(), .nonpositive(), .multipleOf() (.step()) — all using n-suffixed bigint literals.
  • z.boolean(): validates true/false only, no truthy/falsy coercion (use z.coerce.boolean() for that).

Code Examples

z.int();     // safe integer range
z.int32();   // int32 range specifically

z.bigint().gt(5n);
z.bigint().gte(5n);        // alias .min(5n)
z.bigint().multipleOf(5n); // alias .step(5n)

z.boolean().parse(true);  // => true
z.boolean().parse(false); // => false
  • What it demonstrates: integer/bigint/boolean schemas mirror the general number API but with type-appropriate literals (n suffix for bigint).

Key Takeaways

  1. Use z.int() when the value must be a whole number within JS's safe integer range; use z.int32() when it must fit a specific 32-bit integer field (e.g. a database column or binary format).
  2. z.bigint() checks take n-suffixed bigint literals (5n, not 5) — mixing number and bigint literals is a type error, not a runtime one.
  3. z.boolean() is strict — it does not accept truthy/falsy values; that's what z.coerce.boolean() is for.

Connects To

  • Numbers: the general numeric schema these specialize from.
  • Primitives & Coercion: for converting non-boolean/non-integer input before validation.