Skip to content

Is IP

isIp validates that a value is an IPv4 or IPv6 address.

It enforces strict structural and format requirements: the value must be a string, and it must match canonical patterns for either IPv4 or IPv6. Any non‑string value or incorrectly formatted address results in a single validation event. Otherwise, it produces no validation output.

Signature

Through the API:

.isIp()

And internally:

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

Events

Event code Description
type.not.valid Value is not a string.
string.not.ip String does not match IPv4 or IPv6 patterns.

Design rationale

  • Supports both IPv4 and IPv6 using canonical, widely accepted regex patterns.
  • Rejects non‑string values early with a structural‑type diagnostic.
  • Avoids normalization — contributors must provide canonical IP literals.
  • 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

isIp 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 type.not.valid.
  • If the string does not match the IPv4 pattern: ^(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}$ or the IPv6 pattern ^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|::1|::)$, emits string.not.ip.
  • Otherwise, returns an empty result set.

Examples

Valid — IPv4

await isIp("192.168.0.1", "$");
// → []

await isIp("8.8.8.8", "$");
// → []

Valid — IPv6

await isIp("2001:0db8:85a3:0000:0000:8a2e:0370:7334", "$");
// → []

await isIp("::1", "$");
// → []

Invalid — non‑string values

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

Invalid — incorrect format

await isIp("999.999.999.999", "$");
// → [
//     JaneEvent{
//       kind: "error",
//       code: "string.not.ip",
//       path: "$",
//       ...
//     }
//   ]

await isIp("abcd::12345::1", "$");
// → [
//     JaneEvent{
//       kind: "error",
//       code: "string.not.ip",
//       path: "$",
//       ...
//     }
//   ]