Skip to content

Bigint safe

bigintSafe validates that a value is a string representing a BigInt within JavaScript’s safe integer range.

It enforces strict bigint‑string semantics: the value must be a string, must parse successfully as a BigInt literal, and must fall between Number.MIN_SAFE_INTEGER and Number.MAX_SAFE_INTEGER (inclusive). If any condition fails, the rule emits a single validation event. Otherwise, it produces no validation output.

Signature

Through the API:

.bigintSafe();

And internally:

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

Events

Event code Description
bigint.not.string Value is not a string.
bigint.not.bigint String is not a valid bigint literal.
bigint.not.safe Parsed bigint is outside JavaScript’s safe integer range.

Design rationale

  • Enforces strict bigint‑string validation with no coercion or normalization.
  • Rejects non‑string values early with a structural‑type diagnostic.
  • Treats empty strings and malformed literals as invalid bigint values.
  • Uses JavaScript’s BigInt() constructor for canonical parsing.
  • Ensures the parsed value falls within the safe integer range (±9,007,199,254,740,991).
  • Emits exactly one event per failure for clarity and composability.
  • Pure, total, async‑compatible, and returns a readonly array of JaneEvent objects.
  • Preserves the provided path and supports pipeline‑level userMessage overrides.

Invoke

bigintSafe 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 string, emits bigint.not.string.
  • If the string is empty or cannot be parsed by BigInt, emits bigint.not.bigint.
  • If the parsed bigint is outside the safe integer range, emits bigint.not.safe.
  • Otherwise, returns an empty result set.

Examples

Valid — bigint string within safe range

await bigintSafe("9007199254740991", "$"); // MAX_SAFE_INTEGER
// → []

await bigintSafe("-9007199254740991", "$"); // MIN_SAFE_INTEGER
// → []

Invalid — not a string

await bigintSafe(123n, "$");
// → [
//     JaneEvent{
//       kind: "error",
//       code: "bigint.not.string",
//       path: "$",
//       ...
//     }
//   ]

Invalid — malformed bigint literal

await bigintSafe("01", "$");
// → [
//     JaneEvent{
//       kind: "error",
//       code: "bigint.not.bigint",
//       path: "$",
//       ...
//     }
//   ]

Invalid — outside safe range

await bigintSafe("9007199254740992", "$"); // MAX_SAFE + 1
// → [
//     JaneEvent{
//       kind: "error",
//       code: "bigint.not.safe",
//       path: "$",
//       ...
//     }
//   ]