Bounded Integer Validation Schemas (lib/bounded-int.ts)

rekey · apps/api/src/lib/bounded-int.ts · typescript · MIT
database, dev-tools, fw:zod, tag:validation, tag:integer-bounds

Zod schemas with Postgres-safe integer ceilings (INT4_MAX, MONEY_MAX) to prevent 500 errors from out-of-range values, returning 400 with field names instead.

// Source: https://github.com/rekey-dev/rekey | License: MIT | Copyright 2026 Rekey
// Provided via justclaw.it.com; keep this notice with the code.
/**
 * Integer schemas with an upper bound, because Postgres has one.
 *
 * Every money and metering field in this codebase was written as
 * `z.number().int().min(0)`, a floor, no ceiling. Zod accepted anything up to
 * `Number.MAX_SAFE_INTEGER`, Prisma passed it through, and Postgres answered
 * `22003 value out of range for type integer`. An external audit turned that
 * into a 500 on five routes by sending `9007199254740991` as a usage quantity,
 * a plan amount, and a credit grant.
 *
 * A 500 is the wrong answer to a number the caller typed. It reads as "the
 * server broke" rather than "that value is not allowed", it pages whoever owns


... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key