API reference › @evolu/common › Type › IpAddress
const IpAddress: createType<
"IpAddress",
typeof String,
Ipv4Address | Ipv6Address,
IpAddressError
>(
"IpAddress",
String,
(value) =>
Ipv4Address.is(value) || Ipv6Address.is(value)
? ok(value)
: err({ type: "IpAddress", value }),
formatIpAddressError,
);
Defined in: packages/common/src/Type.ts:9493
IP address of either family: an Ipv4Address or a canonical Ipv6Address.
Use IpAddress for addresses whose family is not known in advance, such as the
address of a client or peer. It fails with one IpAddressError instead
of a UnionError that lists both families, and its Output narrows with
Ipv4Address.is or Ipv6Address.is.
Like its members, IpAddress does not normalize, so equal addresses are equal
strings. Use IpAddressFromString to convert other IPv6 spellings to
the canonical one. An IPv4-mapped address such as ::ffff:1.2.3.4 is an
Ipv6Address distinct from the Ipv4Address 1.2.3.4, and dual-stack servers,
such as Node's by default, report IPv4 clients in the mapped form, so convert
addresses from different sources to one form before comparing them.
A non-string fails with the String error.
Example
import { assertEqual, assertErr, assertOk, IpAddress } from "@evolu/common";
assertOk(IpAddress.fromUnknown("192.168.1.1"), "192.168.1.1");
assertOk(IpAddress.fromUnknown("2001:db8::1"), "2001:db8::1");
const invalid = IpAddress.fromUnknown("2001:DB8::1");
assertErr(invalid, { type: "IpAddress", value: "2001:DB8::1" });
assertEqual(
IpAddress.formatError(invalid.error),
'The value "2001:DB8::1" is not a valid IPv4 address or canonical IPv6 address.',
);