API reference › @evolu/common › Type › Ipv4Address

const Ipv4Address: brand(
  "Ipv4Address",
  String,
  (value: string) =>
    ipv4AddressRegex.test(value)
      ? ok()
      : err<Ipv4AddressError>({ type: "Ipv4Address", value }),
  formatIpv4AddressError,
);

Defined in: packages/common/src/Type.ts:9301

IPv4 address in dotted-decimal form, as the IPv4address rule of RFC 3986 section 3.2.2 defines it.

Ipv4Address accepts four decimal numbers from 0 to 255 separated by dots, such as 192.168.1.1. Every address has exactly one Ipv4Address, so equal addresses are equal strings.

Ipv4Address does not normalize. It rejects leading zeros, because some parsers, including the WHATWG URL Standard, read 010 as octal 8 and others as decimal 10. It also rejects the hexadecimal, integer, and shortened forms that URL parsers accept, such as 0x7f.0.0.1, 2130706433, and 127.1, as well as prefix lengths and whitespace. Trim text from other sources before validating it, but do not strip leading zeros, because their meaning depends on the parser that wrote them.

Example

import { assertEqual, assertErr, assertOk, Ipv4Address } from "@evolu/common";

assertOk(Ipv4Address.fromUnknown("192.168.1.1"), "192.168.1.1");

const invalid = Ipv4Address.fromUnknown("192.168.01.1");
assertErr(invalid);
assertEqual(invalid.error, {
  type: "Ipv4Address",
  value: "192.168.01.1",
});