Add workaround for go issue

pull/234/head
joeybloggs 9 years ago
parent 3eaf857419
commit 7b758151c6
  1. 42
      baked_in.go
  2. 25
      validator_test.go

@ -1250,6 +1250,17 @@ func HasMaxOf(v *Validate, topStruct reflect.Value, currentStructOrField reflect
// IsTCP4AddrResolvable is the validation function for validating if the field's value is a resolvable tcp4 address.
// NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
func IsTCP4AddrResolvable(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.ResolveTCPAddr("tcp4", field.String())
return err == nil
}
@ -1257,6 +1268,19 @@ func IsTCP4AddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrF
// IsTCP6AddrResolvable is the validation function for validating if the field's value is a resolvable tcp6 address.
// NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
func IsTCP6AddrResolvable(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.ResolveTCPAddr("tcp6", field.String())
return err == nil
}
@ -1264,6 +1288,9 @@ func IsTCP6AddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrF
// IsTCPAddrResolvable is the validation function for validating if the field's value is a resolvable tcp address.
// NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
func IsTCPAddrResolvable(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
_, err := net.ResolveTCPAddr("tcp", field.String())
return err == nil
}
@ -1292,6 +1319,11 @@ func IsUDPAddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrFi
// IsIP4AddrResolvable is the validation function for validating if the field's value is a resolvable ip4 address.
// NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
func IsIP4AddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
if !IsIPv4(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param) {
return false
}
_, err := net.ResolveIPAddr("ip4", field.String())
return err == nil
}
@ -1299,6 +1331,11 @@ func IsIP4AddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrFi
// IsIP6AddrResolvable is the validation function for validating if the field's value is a resolvable ip6 address.
// NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
func IsIP6AddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
if !IsIPv6(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param) {
return false
}
_, err := net.ResolveIPAddr("ip6", field.String())
return err == nil
}
@ -1306,6 +1343,11 @@ func IsIP6AddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrFi
// IsIPAddrResolvable is the validation function for validating if the field's value is a resolvable ip address.
// NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
func IsIPAddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
if !IsIP(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param) {
return false
}
_, err := net.ResolveIPAddr("ip", field.String())
return err == nil
}

@ -1755,6 +1755,7 @@ func TestIPValidation(t *testing.T) {
param string
expected bool
}{
{"", false},
{"10.0.0.1", true},
{"172.16.0.1", true},
{"192.168.0.1", true},
@ -2023,8 +2024,8 @@ func TestTCP6AddrValidation(t *testing.T) {
param string
expected bool
}{
{":80", true},
{"127.0.0.1:80", true},
{":80", false},
{"127.0.0.1:80", false},
{"[::1]:80", true},
{"256.0.0.0:1", false},
{"[::1]", false},
@ -2054,9 +2055,9 @@ func TestTCP4AddrValidation(t *testing.T) {
param string
expected bool
}{
{":80", true},
{":80", false},
{"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},
{"[::1]", false},
}
@ -2180,10 +2181,12 @@ func TestIPAddrValidation(t *testing.T) {
param string
expected bool
}{
{"", true},
{"", false},
{"127.0.0.1", true},
{"127.0.0.1:80", false},
{"::1", true},
{"256.0.0.0", false},
{"localhost", false},
}
for i, test := range tests {
@ -2210,9 +2213,11 @@ func TestIP6AddrValidation(t *testing.T) {
param string
expected bool
}{
{"", true},
{"127.0.0.1", true}, // https://github.com/golang/go/issues/14037
{"", false},
{"127.0.0.1", false}, // https://github.com/golang/go/issues/14037
{"127.0.0.1:80", false},
{"::1", true},
{"0:0:0:0:0:0:0:1", true},
{"256.0.0.0", false},
}
@ -2240,10 +2245,12 @@ func TestIP4AddrValidation(t *testing.T) {
param string
expected bool
}{
{"", true},
{"", false},
{"127.0.0.1", true},
{"::1", true}, // https://github.com/golang/go/issues/14037
{"127.0.0.1:80", false},
{"::1", false}, // https://github.com/golang/go/issues/14037
{"256.0.0.0", false},
{"localhost", false},
}
for i, test := range tests {

Loading…
Cancel
Save