Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix IPv4 mapped IPv6 address cannot verify for "ipv6" and "ipv4" tag #1312

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions baked_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,16 +402,18 @@ func isCIDR(fl FieldLevel) bool {

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

return ip != nil && ip.To4() != nil
return ip != nil && strings.Contains(ipStr, ".") && !strings.Contains(ipStr, ":")
}

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

return ip != nil && ip.To4() == nil
return ip != nil && strings.Contains(ipStr, ":")
}

// isIP is the validation function for validating if the field's value is a valid v4 or v6 IP address.
Expand Down
4 changes: 4 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2419,6 +2419,8 @@ func TestIPv6Validation(t *testing.T) {
{"2001:cdba:0000:0000:0000:0000:3257:9652", true},
{"2001:cdba:0:0:0:0:3257:9652", true},
{"2001:cdba::3257:9652", true},
{"::ffff:192.168.1.1", true},
{"::ffff:c0a8:0101", true},
}

validate := New()
Expand Down Expand Up @@ -2459,6 +2461,8 @@ func TestIPv4Validation(t *testing.T) {
{"2001:cdba:0000:0000:0000:0000:3257:9652", false},
{"2001:cdba:0:0:0:0:3257:9652", false},
{"2001:cdba::3257:9652", false},
{"::ffff:192.168.1.1", false},
{"::ffff:c0a8:0101", false},
}

validate := New()
Expand Down