Skip to content

Is port

isPort validates that a value is a TCP/UDP port number.

It enforces strict numeric and range requirements: the value must be a number, must be an integer, and must fall between 0 and 65535 inclusive. Any non‑number value or out‑of‑range integer results in a single validation event. Otherwise, it produces no validation output.

Signature

Through the API:

.port()

And internally:

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

Events

Event code Description
type.not.valid Value is not a number.
number.not.in-port-range Number is not an integer between 0 and 65535.

Design rationale

  • Enforces strict port semantics: integer, non‑negative, ≤ 65535.
  • Rejects non‑number values early with a structural‑type diagnostic.
  • Uses Number.isInteger to avoid accepting floats or numeric strings.
  • 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

isPort 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 number is not an integer or is outside 0–65535, emits number.not.in-port-range.
  • Otherwise, returns an empty result set.

Examples

Valid — integer ports

await isPort(0, "$");
// → []

await isPort(80, "$");
// → []

await isPort(65535, "$");
// → []

Invalid — non‑number values

await isPort("80", "$");
// → [
//     JaneEvent{
//       kind: "error",
//       code: "type.not.valid",
//       path: "$",
//       ...
//     }
//   ]

Invalid — out‑of‑range or non‑integer

await isPort(-1, "$");
// → [
//     JaneEvent{
//       kind: "error",
//       code: "number.not.in-port-range",
//       path: "$",
//       ...
//     }
//   ]


await isPort(70000, "$");
// → [
//     JaneEvent{
//       kind: "error",
//       code: "number.not.in-port-range",
//       path: "$",
//       ...
//     }
//   ]


await isPort(3.14, "$");
// → [
//     JaneEvent{
//       kind: "error",
//       code: "number.not.in-port-range",
//       path: "$",
//       ...
//     }
//   ]