Skip to content

Comments

feat: add z.ipv4Range() and z.ipv6Range() validation#5622

Open
wataryooou wants to merge 4 commits intocolinhacks:mainfrom
wataryooou:ip-range-validation-zod4
Open

feat: add z.ipv4Range() and z.ipv6Range() validation#5622
wataryooou wants to merge 4 commits intocolinhacks:mainfrom
wataryooou:ip-range-validation-zod4

Conversation

@wataryooou
Copy link
Contributor

@wataryooou wataryooou commented Jan 10, 2026

Closes #2758

This PR introduces ipv4Range and ipv6Range validation schemas that check if an IP address falls within a specified CIDR range.

API

// Standalone schemas (recommended)
z.ipv4Range("192.168.1.0/24").parse("192.168.1.10");  // ✅
z.ipv4Range("192.168.1.0/24").parse("192.168.2.10");  // ❌

z.ipv6Range("2001:db8::/32").parse("2001:db8::1");    // ✅
z.ipv6Range("fe80::/10").parse("fe80::1");            // ✅

// Method chaining
z.string().ipv4Range("10.0.0.0/8")
z.string().ipv6Range("::1/128")

// Custom error message
z.ipv4Range("192.168.1.0/24", "IP must be in private range")

Features

  • Validates both IP format and range inclusion in a single check
  • Throws at schema construction time for invalid CIDR prefix length
  • Supports IPv4 prefix 0-32 and IPv6 prefix 0-128
  • Full :: notation support for IPv6

Additional Notes

These features were initially implemented in this PR (#3754) before zod v4, but we've refactored the PR to resolve conflicts and align with the zod4 (and mini) syntax conventions.

@pullfrog
Copy link
Contributor

pullfrog bot commented Jan 10, 2026

Pullfrog  | Review this ➔pullfrog.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support IP Range Validation

1 participant