Skip to content

Safe integer

safeInteger validates that a number is a safe integer within JavaScript's IEEE-754 safe range.

It enforces strict numeric validation and rejects any non-number value, non-integer numbers, or integers outside the safe range. If the value is not a safe integer, the rule emits a single validation event. Otherwise, it produces no validation output.

Signature

Through the API:

.safeInteger()

And internally:

export const safeInteger: ValidationRule
(value: unknown, path: FieldPath) => Promise<ReadonlyArray<JaneEvent>>

Events

Event code Description
type.not.valid Value is not structurally a number
number.not.integer Number is not an integer
number.not.safe-integer Integer is outside JavaScript's safe range

Design rationale

  • Provides protection against precision loss and overflow-adjacent behavior.
  • Ensures numeric values can be represented exactly in JavaScript.
  • Combines integer and safe range validation for comprehensive safety.
  • Rejects non-number values with a clear structural-type diagnostic.
  • Never coerces or normalizes — validation is explicit and opt-in.
  • Emits exactly one event per failure for clarity and composability.
  • Async-compatible and returns a readonly array of JaneEvent objects.

Invoke

safeInteger runs only when explicitly included in a boundary or pipeline. It does not run automatically.

The rule activates when:

  • The value is any JavaScript value.
  • If the value is not a number, emits type.not.valid.
  • If the value is a number but not an integer, emits number.not.integer.
  • If the value is an integer but outside safe range, emits number.not.safe-integer.
  • If the value is a safe integer → returns an empty result set.

Examples

Valid safe integer

await safeInteger(42, "$");
// → []

Integer outside safe range

await safeInteger(9007199254740992, "$");
// → [
//     JaneEvent{
//       kind: "error",
//       code: "number.not.safe-integer",
//       path: "$",
//       ...
//     }
//   ]

Non-integer number

await safeInteger(42.5, "$");
// → [
//     JaneEvent{
//       kind: "error",
//       code: "number.not.integer",
//       path: "$",
//       ...
//     }
//   ]