API reference › @evolu/common › Type › Ipv6AddressFromString

const Ipv6AddressFromString: transform(
  "Ipv6AddressFromString",
  String,
  Ipv6Address,
  {
    from: (value: string): Result<string, Ipv6AddressFromStringError> => {
      const canonical = canonicalizeIpv6Address(value);
      return canonical === undefined
        ? err({ type: "Ipv6AddressFromString", value })
        : ok(canonical);
    },
    to: (value: Ipv6Address) => value,
  },
  formatIpv6AddressFromStringError,
);

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

Transforms IPv6 address text into its canonical Ipv6Address.

Accepts the text forms of RFC 4291 section 2.2: eight groups of one to four hexadecimal digits in either case, at most one :: replacing one or more zero groups, and an optional trailing dotted IPv4 address that follows the Ipv4Address rules. Like Ipv6Address, it rejects zone identifiers, brackets, prefix lengths, and whitespace.

Encoding returns the canonical text, not the original spelling.

Example

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

assertOk(
  Ipv6AddressFromString.fromUnknown("2001:0DB8:0:0:0:0:0:1"),
  "2001:db8::1",
);
assertOk(Ipv6AddressFromString.fromUnknown("::ffff:102:304"), "::ffff:1.2.3.4");

const invalid = Ipv6AddressFromString.fromUnknown("fe80::1%eth0");
assertErr(invalid, {
  type: "Ipv6AddressFromString",
  value: "fe80::1%eth0",
});
assertEqual(
  Ipv6AddressFromString.formatError(invalid.error),
  'The value "fe80::1%eth0" is not a valid IPv6 address.',
);