File tree Expand file tree Collapse file tree 2 files changed +10
-4
lines changed
Expand file tree Collapse file tree 2 files changed +10
-4
lines changed Original file line number Diff line number Diff 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.
404404func 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.
411412func 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.
Original file line number Diff line number Diff 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 ()
You can’t perform that action at this time.
0 commit comments