IPv4 configuration
Validate an IPv4-only configuration value, such as a private network address.
Zod · Module
Zod provides separate schemas for IPv4 and IPv6 addresses. Use z.union() to accept either format.
The current Zod API replaces the older string .ip() refinement with dedicated z.ipv4() and z.ipv6() schemas. Combine them with z.union() when input may use either address family; parsing returns the original valid string or reports a validation error.
z.ipv4();
z.ipv6();
z.union([z.ipv4(), z.ipv6()]);A Zod schema that validates IP address strings; parsing a valid value returns the input string.
z.ipv4()..parse().z.ipv6()..parse().z.union() when either address family is allowed.z.string().ip() form in the current API; use the dedicated IPv4 or IPv6 constructors.import { z } from "zod";
const ipAddress = z.union([z.ipv4(), z.ipv6()]);
console.log(ipAddress.parse("192.168.0.0"));
console.log(ipAddress.parse("2001:db8:85a3::8a2e:370:7334"));Creates a schema accepting either an IPv4 or IPv6 address, then parses one value of each kind.
Validate an IPv4-only configuration value, such as a private network address.
Validate an IPv6-only endpoint or network address.
Accept client addresses from systems that may provide either address family.
Use z.ipv4() for IPv4 addresses and z.ipv6() for IPv6 addresses. These are separate schema constructors in the current API.
Follow-up: How would you accept either family? Create z.union([z.ipv4(), z.ipv6()]).
z.ipv4() validates IPv4 strings, while z.ipv6() validates IPv6 strings. A successful .parse() returns the validated input.
Follow-up: What should replace z.string().ip()? Use the appropriate dedicated constructor, or a union of both constructors.
Use a union containing both schemas when the application accepts either address family. This keeps the accepted formats explicit instead of relying on a single broad string refinement.
Follow-up: How would you restrict an endpoint to IPv6 only? Use z.ipv6() directly as the endpoint's input schema.
z.ipv4()z.ipv6()z.union()