Skip to content

Commit 0cd13c1

Browse files
committed
fix IPv4 mapped IPv6 address cannot verify for "ipv6" and "ipv4" tag
1 parent a947377 commit 0cd13c1

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

baked_in.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -402,16 +402,18 @@ func isCIDR(fl FieldLevel) bool {
402402

403403
// isIPv4 is the validation function for validating if a value is a valid v4 IP address.
404404
func isIPv4(fl FieldLevel) bool {
405-
ip := net.ParseIP(fl.Field().String())
405+
ipStr := fl.Field().String()
406+
ip := net.ParseIP(ipStr)
406407

407-
return ip != nil && ip.To4() != nil
408+
return ip != nil && strings.Contains(ipStr, ".") && !strings.Contains(ipStr, ":")
408409
}
409410

410411
// isIPv6 is the validation function for validating if the field's value is a valid v6 IP address.
411412
func isIPv6(fl FieldLevel) bool {
412-
ip := net.ParseIP(fl.Field().String())
413+
ipStr := fl.Field().String()
414+
ip := net.ParseIP(ipStr)
413415

414-
return ip != nil && ip.To4() == nil
416+
return ip != nil && strings.Contains(ipStr, ":")
415417
}
416418

417419
// isIP is the validation function for validating if the field's value is a valid v4 or v6 IP address.

validator_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2419,6 +2419,8 @@ func TestIPv6Validation(t *testing.T) {
24192419
{"2001:cdba:0000:0000:0000:0000:3257:9652", true},
24202420
{"2001:cdba:0:0:0:0:3257:9652", true},
24212421
{"2001:cdba::3257:9652", true},
2422+
{"::ffff:192.168.1.1", true},
2423+
{"::ffff:c0a8:0101", true},
24222424
}
24232425

24242426
validate := New()
@@ -2459,6 +2461,8 @@ func TestIPv4Validation(t *testing.T) {
24592461
{"2001:cdba:0000:0000:0000:0000:3257:9652", false},
24602462
{"2001:cdba:0:0:0:0:3257:9652", false},
24612463
{"2001:cdba::3257:9652", false},
2464+
{"::ffff:192.168.1.1", false},
2465+
{"::ffff:c0a8:0101", false},
24622466
}
24632467

24642468
validate := New()

0 commit comments

Comments
 (0)