Codebase list golang-github-go-playground-validator-v10 / 7b75815
Add workaround for go issue joeybloggs 8 years ago
2 changed file(s) with 58 addition(s) and 9 deletion(s). Raw diff Collapse all Expand all
12491249 // IsTCP4AddrResolvable is the validation function for validating if the field's value is a resolvable tcp4 address.
12501250 // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
12511251 func IsTCP4AddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
1252
1253 val := field.String()
1254
1255 if idx := strings.LastIndex(val, ":"); idx != -1 {
1256 val = val[0:idx]
1257 }
1258
1259 if !IsIPv4(v, topStruct, currentStructOrField, reflect.ValueOf(val), fieldType, fieldKind, param) {
1260 return false
1261 }
1262
12521263 _, err := net.ResolveTCPAddr("tcp4", field.String())
12531264 return err == nil
12541265 }
12561267 // IsTCP6AddrResolvable is the validation function for validating if the field's value is a resolvable tcp6 address.
12571268 // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
12581269 func IsTCP6AddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
1270
1271 val := field.String()
1272
1273 if idx := strings.LastIndex(val, ":"); idx != -1 {
1274 if idx != 0 && val[idx-1:idx] == "]" {
1275 val = val[1 : idx-1]
1276 }
1277 }
1278
1279 if !IsIPv6(v, topStruct, currentStructOrField, reflect.ValueOf(val), fieldType, fieldKind, param) {
1280 return false
1281 }
1282
12591283 _, err := net.ResolveTCPAddr("tcp6", field.String())
12601284 return err == nil
12611285 }
12631287 // IsTCPAddrResolvable is the validation function for validating if the field's value is a resolvable tcp address.
12641288 // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
12651289 func IsTCPAddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
1290
1291 // if string before the post is blank then invalid
1292
12661293 _, err := net.ResolveTCPAddr("tcp", field.String())
12671294 return err == nil
12681295 }
12911318 // IsIP4AddrResolvable is the validation function for validating if the field's value is a resolvable ip4 address.
12921319 // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
12931320 func IsIP4AddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
1321
1322 if !IsIPv4(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param) {
1323 return false
1324 }
1325
12941326 _, err := net.ResolveIPAddr("ip4", field.String())
12951327 return err == nil
12961328 }
12981330 // IsIP6AddrResolvable is the validation function for validating if the field's value is a resolvable ip6 address.
12991331 // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
13001332 func IsIP6AddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
1333
1334 if !IsIPv6(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param) {
1335 return false
1336 }
1337
13011338 _, err := net.ResolveIPAddr("ip6", field.String())
13021339 return err == nil
13031340 }
13051342 // IsIPAddrResolvable is the validation function for validating if the field's value is a resolvable ip address.
13061343 // NOTE: This is exposed for use within your own custom functions and not intended to be called directly.
13071344 func IsIPAddrResolvable(v *Validate, topStruct reflect.Value, currentStructOrField reflect.Value, field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string) bool {
1345
1346 if !IsIP(v, topStruct, currentStructOrField, field, fieldType, fieldKind, param) {
1347 return false
1348 }
1349
13081350 _, err := net.ResolveIPAddr("ip", field.String())
13091351 return err == nil
13101352 }
17541754 param string
17551755 expected bool
17561756 }{
1757 {"", false},
17571758 {"10.0.0.1", true},
17581759 {"172.16.0.1", true},
17591760 {"192.168.0.1", true},
20222023 param string
20232024 expected bool
20242025 }{
2025 {":80", true},
2026 {"127.0.0.1:80", true},
2026 {":80", false},
2027 {"127.0.0.1:80", false},
20272028 {"[::1]:80", true},
20282029 {"256.0.0.0:1", false},
20292030 {"[::1]", false},
20532054 param string
20542055 expected bool
20552056 }{
2056 {":80", true},
2057 {":80", false},
20572058 {"127.0.0.1:80", true},
2058 {"[::1]:80", true}, // https://github.com/golang/go/issues/14037
2059 {"[::1]:80", false}, // https://github.com/golang/go/issues/14037
20592060 {"256.0.0.0:1", false},
20602061 {"[::1]", false},
20612062 }
21792180 param string
21802181 expected bool
21812182 }{
2182 {"", true},
2183 {"", false},
21832184 {"127.0.0.1", true},
2185 {"127.0.0.1:80", false},
21842186 {"::1", true},
21852187 {"256.0.0.0", false},
2188 {"localhost", false},
21862189 }
21872190
21882191 for i, test := range tests {
22092212 param string
22102213 expected bool
22112214 }{
2212 {"", true},
2213 {"127.0.0.1", true}, // https://github.com/golang/go/issues/14037
2215 {"", false},
2216 {"127.0.0.1", false}, // https://github.com/golang/go/issues/14037
2217 {"127.0.0.1:80", false},
22142218 {"::1", true},
2219 {"0:0:0:0:0:0:0:1", true},
22152220 {"256.0.0.0", false},
22162221 }
22172222
22392244 param string
22402245 expected bool
22412246 }{
2242 {"", true},
2247 {"", false},
22432248 {"127.0.0.1", true},
2244 {"::1", true}, // https://github.com/golang/go/issues/14037
2249 {"127.0.0.1:80", false},
2250 {"::1", false}, // https://github.com/golang/go/issues/14037
22452251 {"256.0.0.0", false},
2252 {"localhost", false},
22462253 }
22472254
22482255 for i, test := range tests {