Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(BREAKING): Change CIDRIPv4 validation #945

Merged
merged 2 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions baked_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,9 @@ func isMAC(fl FieldLevel) bool {

// isCIDRv4 is the validation function for validating if the field's value is a valid v4 CIDR address.
func isCIDRv4(fl FieldLevel) bool {
ip, _, err := net.ParseCIDR(fl.Field().String())
ip, net, err := net.ParseCIDR(fl.Field().String())

return err == nil && ip.To4() != nil
return err == nil && ip.To4() != nil && net.IP.Equal(ip)
}

// isCIDRv6 is the validation function for validating if the field's value is a valid v6 CIDR address.
Expand Down
19 changes: 13 additions & 6 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2575,19 +2575,26 @@ func TestCIDRv4Validation(t *testing.T) {
param string
expected bool
}{
{"10.0.0.0/0", true},
{"10.0.0.1/8", true},
{"172.16.0.1/16", true},
{"192.168.0.1/24", true},
{"192.168.255.254/24", true},
{"0.0.0.0/0", true},
{"10.0.0.0/0", false},
{"10.0.0.0/8", true},
{"10.0.0.1/8", false},
{"172.16.0.0/16", true},
{"172.16.0.1/16", false},
{"192.168.0.0/24", true},
{"192.168.0.1/24", false},
{"192.168.255.0/24", true},
{"192.168.255.254/24", false},
{"192.168.255.254/48", false},
{"192.168.255.256/24", false},
{"172.16.255.254/16", true},
{"172.16.0.0/16", true},
{"172.16.255.254/16", false},
{"172.16.256.255/16", false},
{"2001:cdba:0000:0000:0000:0000:3257:9652/64", false},
{"2001:cdba:0000:0000:0000:0000:3257:9652/256", false},
{"2001:cdba:0:0:0:0:3257:9652/32", false},
{"2001:cdba::3257:9652/16", false},
{"172.56.1.0/16", false},
}

validate := New()
Expand Down