Merge pull request #193 from krhubert/v8

add cidr, cidrv4 and cidrv6 validators
pull/194/head v8.4
Dean Karn 9 years ago
commit 17084684e6
  1. 24
      baked_in.go
  2. 12
      doc.go
  3. 123
      validator_test.go

@ -81,6 +81,9 @@ var bakedInValidators = map[string]Func{
"ipv4": isIPv4, "ipv4": isIPv4,
"ipv6": isIPv6, "ipv6": isIPv6,
"ip": isIP, "ip": isIP,
"cidrv4": isCIDRv4,
"cidrv6": isCIDRv6,
"cidr": isCIDR,
"mac": isMac, "mac": isMac,
} }
@ -89,6 +92,27 @@ func isMac(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Va
return err == nil 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 { 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()) ip := net.ParseIP(field.String())

@ -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. This validates that a string value contains a valid v6 IP Adress.
(Usage: ipv6) (Usage: ipv6)
cidr
This validates that a string value contains a valid CIDR Adress.
(Usage: cidr)
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 mac
This validates that a string value contains a valid MAC Adress defined This validates that a string value contains a valid MAC Adress defined
by go's ParseMAC accepted formats and types see: by go's ParseMAC accepted formats and types see:

@ -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) { func TestSliceMapArrayChanFuncPtrInterfaceRequiredValidation(t *testing.T) {
var m map[string]string var m map[string]string

Loading…
Cancel
Save