diff --git a/baked_in.go b/baked_in.go index ee6e4a9..dac5145 100644 --- a/baked_in.go +++ b/baked_in.go @@ -81,6 +81,9 @@ var bakedInValidators = map[string]Func{ "ipv4": isIPv4, "ipv6": isIPv6, "ip": isIP, + "cidrv4": isCIDRv4, + "cidrv6": isCIDRv6, + "cidr": isCIDR, "mac": isMac, } @@ -89,6 +92,27 @@ func isMac(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Va return err == nil } +func isCIDRv4(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool { + + ip, _, err := net.ParseCIDR(field.String()) + + return err == nil && ip.To4() != nil +} + +func isCIDRv6(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool { + + ip, _, err := net.ParseCIDR(field.String()) + + return err == nil && ip.To4() == nil +} + +func isCIDR(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool { + + _, _, err := net.ParseCIDR(field.String()) + + return err == nil +} + func isIPv4(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool { ip := net.ParseIP(field.String()) diff --git a/doc.go b/doc.go index af06a0d..a0a6001 100644 --- a/doc.go +++ b/doc.go @@ -452,6 +452,18 @@ Here is a list of the current built in validators: This validates that a string value contains a valid v6 IP Adress. (Usage: ipv6) + cidr + This validates that a string value contains a valid CIDR Adress. + (Usage: ip) + + cidrv4 + This validates that a string value contains a valid v4 CIDR Adress. + (Usage: cidrv4) + + cidrv6 + This validates that a string value contains a valid v6 CIDR Adress. + (Usage: cidrv6) + mac This validates that a string value contains a valid MAC Adress defined by go's ParseMAC accepted formats and types see: diff --git a/validator_test.go b/validator_test.go index 6875693..8a631bf 100644 --- a/validator_test.go +++ b/validator_test.go @@ -1542,6 +1542,129 @@ func TestIPv4Validation(t *testing.T) { } } +func TestCIDRValidation(t *testing.T) { + tests := []struct { + 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}, + {"192.168.255.254/48", false}, + {"192.168.255.256/24", false}, + {"172.16.255.254/16", true}, + {"172.16.256.255/16", false}, + {"2001:cdba:0000:0000:0000:0000:3257:9652/64", true}, + {"2001:cdba:0000:0000:0000:0000:3257:9652/256", false}, + {"2001:cdba:0:0:0:0:3257:9652/32", true}, + {"2001:cdba::3257:9652/16", true}, + } + + for i, test := range tests { + + errs := validate.Field(test.param, "cidr") + + if test.expected == true { + if !IsEqual(errs, nil) { + t.Fatalf("Index: %d cidr failed Error: %s", i, errs) + } + } else { + if IsEqual(errs, nil) { + t.Fatalf("Index: %d cidr failed Error: %s", i, errs) + } else { + val := errs.(ValidationErrors)[""] + if val.Tag != "cidr" { + t.Fatalf("Index: %d cidr failed Error: %s", i, errs) + } + } + } + } +} + +func TestCIDRv6Validation(t *testing.T) { + tests := []struct { + param string + expected bool + }{ + {"10.0.0.0/0", false}, + {"10.0.0.1/8", false}, + {"172.16.0.1/16", false}, + {"192.168.0.1/24", false}, + {"192.168.255.254/24", false}, + {"192.168.255.254/48", false}, + {"192.168.255.256/24", false}, + {"172.16.255.254/16", false}, + {"172.16.256.255/16", false}, + {"2001:cdba:0000:0000:0000:0000:3257:9652/64", true}, + {"2001:cdba:0000:0000:0000:0000:3257:9652/256", false}, + {"2001:cdba:0:0:0:0:3257:9652/32", true}, + {"2001:cdba::3257:9652/16", true}, + } + + for i, test := range tests { + + errs := validate.Field(test.param, "cidrv6") + + if test.expected == true { + if !IsEqual(errs, nil) { + t.Fatalf("Index: %d cidrv6 failed Error: %s", i, errs) + } + } else { + if IsEqual(errs, nil) { + t.Fatalf("Index: %d cidrv6 failed Error: %s", i, errs) + } else { + val := errs.(ValidationErrors)[""] + if val.Tag != "cidrv6" { + t.Fatalf("Index: %d cidrv6 failed Error: %s", i, errs) + } + } + } + } +} + +func TestCIDRv4Validation(t *testing.T) { + tests := []struct { + 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}, + {"192.168.255.254/48", false}, + {"192.168.255.256/24", false}, + {"172.16.255.254/16", true}, + {"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}, + } + + for i, test := range tests { + + errs := validate.Field(test.param, "cidrv4") + + if test.expected == true { + if !IsEqual(errs, nil) { + t.Fatalf("Index: %d cidrv4 failed Error: %s", i, errs) + } + } else { + if IsEqual(errs, nil) { + t.Fatalf("Index: %d cidrv4 failed Error: %s", i, errs) + } else { + val := errs.(ValidationErrors)[""] + if val.Tag != "cidrv4" { + t.Fatalf("Index: %d cidrv4 failed Error: %s", i, errs) + } + } + } + } +} + func TestSliceMapArrayChanFuncPtrInterfaceRequiredValidation(t *testing.T) { var m map[string]string