correct IsUPDAddrResolvable functions

pull/234/head
joeybloggs 9 years ago
parent faf40fd62f
commit 4cb9152af0
  1. 37
      baked_in.go
  2. 10
      validator_test.go

@ -87,9 +87,9 @@ var bakedInValidators = map[string]Func{
"tcp4_addr": IsTCP4AddrResolvable, "tcp4_addr": IsTCP4AddrResolvable,
"tcp6_addr": IsTCP6AddrResolvable, "tcp6_addr": IsTCP6AddrResolvable,
"tcp_addr": IsTCPAddrResolvable, "tcp_addr": IsTCPAddrResolvable,
"udp4_addr": IsUDP4AddrResolvable, // need to do "udp4_addr": IsUDP4AddrResolvable,
"udp6_addr": IsUDP6AddrResolvable, // need to do "udp6_addr": IsUDP6AddrResolvable,
"udp_addr": IsUDPAddrResolvable, // need to do "udp_addr": IsUDPAddrResolvable,
"ip4_addr": IsIP4AddrResolvable, "ip4_addr": IsIP4AddrResolvable,
"ip6_addr": IsIP6AddrResolvable, "ip6_addr": IsIP6AddrResolvable,
"ip_addr": IsIPAddrResolvable, "ip_addr": IsIPAddrResolvable,
@ -1302,6 +1302,17 @@ func IsTCPAddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrFi
// IsUDP4AddrResolvable is the validation function for validating if the field's value is a resolvable udp4 address. // IsUDP4AddrResolvable is the validation function for validating if the field's value is a resolvable udp4 address.
// NOTE: This is exposed for use within your own custom functions and not intended to be called directly. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
func IsUDP4AddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool { func IsUDP4AddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
val := field.String()
if idx := strings.LastIndex(val, ":"); idx != -1 {
val = val[0:idx]
}
if !IsIPv4(v, topStruct, currentStructOrField, reflect.ValueOf(val), fieldType, fieldKind, param) {
return false
}
_, err := net.ResolveUDPAddr("udp4", field.String()) _, err := net.ResolveUDPAddr("udp4", field.String())
return err == nil return err == nil
} }
@ -1309,6 +1320,19 @@ func IsUDP4AddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrF
// IsUDP6AddrResolvable is the validation function for validating if the field's value is a resolvable udp6 address. // IsUDP6AddrResolvable is the validation function for validating if the field's value is a resolvable udp6 address.
// NOTE: This is exposed for use within your own custom functions and not intended to be called directly. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
func IsUDP6AddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool { func IsUDP6AddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
val := field.String()
if idx := strings.LastIndex(val, ":"); idx != -1 {
if idx != 0 && val[idx-1:idx] == "]" {
val = val[1 : idx-1]
}
}
if !IsIPv6(v, topStruct, currentStructOrField, reflect.ValueOf(val), fieldType, fieldKind, param) {
return false
}
_, err := net.ResolveUDPAddr("udp6", field.String()) _, err := net.ResolveUDPAddr("udp6", field.String())
return err == nil return err == nil
} }
@ -1316,6 +1340,13 @@ func IsUDP6AddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrF
// IsUDPAddrResolvable is the validation function for validating if the field's value is a resolvable udp address. // IsUDPAddrResolvable is the validation function for validating if the field's value is a resolvable udp address.
// NOTE: This is exposed for use within your own custom functions and not intended to be called directly. // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
func IsUDPAddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool { func IsUDPAddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
// if string before the post is blank then invalid
if !IsUDP4AddrResolvable(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param) &&
!IsUDP6AddrResolvable(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param) {
return false
}
_, err := net.ResolveUDPAddr("udp", field.String()) _, err := net.ResolveUDPAddr("udp", field.String())
return err == nil return err == nil
} }

@ -2087,7 +2087,7 @@ func TestUDPAddrValidation(t *testing.T) {
param string param string
expected bool expected bool
}{ }{
{":80", true}, {":80", false},
{"127.0.0.1:80", true}, {"127.0.0.1:80", true},
{"[::1]:80", true}, {"[::1]:80", true},
{"256.0.0.0:1", false}, {"256.0.0.0:1", false},
@ -2118,8 +2118,8 @@ func TestUDP6AddrValidation(t *testing.T) {
param string param string
expected bool expected bool
}{ }{
{":80", true}, {":80", false},
{"127.0.0.1:80", true}, {"127.0.0.1:80", false},
{"[::1]:80", true}, {"[::1]:80", true},
{"256.0.0.0:1", false}, {"256.0.0.0:1", false},
{"[::1]", false}, {"[::1]", false},
@ -2149,9 +2149,9 @@ func TestUDP4AddrValidation(t *testing.T) {
param string param string
expected bool expected bool
}{ }{
{":80", true}, {":80", false},
{"127.0.0.1:80", true}, {"127.0.0.1:80", true},
{"[::1]:80", true}, // https://github.com/golang/go/issues/14037 {"[::1]:80", false}, // https://github.com/golang/go/issues/14037
{"256.0.0.0:1", false}, {"256.0.0.0:1", false},
{"[::1]", false}, {"[::1]", false},
} }

Loading…
Cancel
Save