feat(BREAKING): Disable validation of cidripv4 when ip is not the begining of the block

pull/945/head
Martin Kagamino Lehoux 3 years ago
parent 9e2ea40380
commit 554e1a11c5
  1. 4
      baked_in.go
  2. 63
      validator_test.go

@ -332,9 +332,9 @@ func isMAC(fl FieldLevel) bool {
// isCIDRv4 is the validation function for validating if the field's value is a valid v4 CIDR address. // isCIDRv4 is the validation function for validating if the field's value is a valid v4 CIDR address.
func isCIDRv4(fl FieldLevel) bool { 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. // isCIDRv6 is the validation function for validating if the field's value is a valid v6 CIDR address.

@ -2564,19 +2564,26 @@ func TestCIDRv4Validation(t *testing.T) {
param string param string
expected bool expected bool
}{ }{
{"10.0.0.0/0", true}, {"0.0.0.0/0", true},
{"10.0.0.1/8", true}, {"10.0.0.0/0", false},
{"172.16.0.1/16", true}, {"10.0.0.0/8", true},
{"192.168.0.1/24", true}, {"10.0.0.1/8", false},
{"192.168.255.254/24", true}, {"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.254/48", false},
{"192.168.255.256/24", 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}, {"172.16.256.255/16", false},
{"2001:cdba:0000:0000:0000:0000:3257:9652/64", false}, {"2001:cdba:0000:0000:0000:0000:3257:9652/64", false},
{"2001:cdba:0000:0000:0000:0000:3257:9652/256", false}, {"2001:cdba:0000:0000:0000:0000:3257:9652/256", false},
{"2001:cdba:0:0:0:0:3257:9652/32", false}, {"2001:cdba:0:0:0:0:3257:9652/32", false},
{"2001:cdba::3257:9652/16", false}, {"2001:cdba::3257:9652/16", false},
{"172.56.1.0/16", false},
} }
validate := New() validate := New()
@ -12264,25 +12271,25 @@ func TestCreditCardFormatValidation(t *testing.T) {
} }
func TestMultiOrOperatorGroup(t *testing.T) { func TestMultiOrOperatorGroup(t *testing.T) {
tests := []struct { tests := []struct {
Value int `validate:"eq=1|gte=5,eq=1|lt=7"` Value int `validate:"eq=1|gte=5,eq=1|lt=7"`
expected bool expected bool
}{ }{
{1, true}, {2, false}, {5, true}, {6, true}, {8, false}, {1, true}, {2, false}, {5, true}, {6, true}, {8, false},
} }
validate := New() validate := New()
for i, test := range tests { for i, test := range tests {
errs := validate.Struct(test) errs := validate.Struct(test)
if test.expected { if test.expected {
if !IsEqual(errs, nil) { if !IsEqual(errs, nil) {
t.Fatalf("Index: %d multi_group_of_OR_operators failed Error: %s", i, errs) t.Fatalf("Index: %d multi_group_of_OR_operators failed Error: %s", i, errs)
} }
} else { } else {
if IsEqual(errs, nil) { if IsEqual(errs, nil) {
t.Fatalf("Index: %d multi_group_of_OR_operators should have errs", i) t.Fatalf("Index: %d multi_group_of_OR_operators should have errs", i)
} }
} }
} }
} }

Loading…
Cancel
Save