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 {
402
402
403
403
// isIPv4 is the validation function for validating if a value is a valid v4 IP address.
404
404
func isIPv4 (fl FieldLevel ) bool {
405
- ip := net .ParseIP (fl .Field ().String ())
405
+ ipStr := fl .Field ().String ()
406
+ ip := net .ParseIP (ipStr )
406
407
407
- return ip != nil && ip . To4 () != nil
408
+ return ip != nil && strings . Contains ( ipStr , "." ) && ! strings . Contains ( ipStr , ":" )
408
409
}
409
410
410
411
// isIPv6 is the validation function for validating if the field's value is a valid v6 IP address.
411
412
func isIPv6 (fl FieldLevel ) bool {
412
- ip := net .ParseIP (fl .Field ().String ())
413
+ ipStr := fl .Field ().String ()
414
+ ip := net .ParseIP (ipStr )
413
415
414
- return ip != nil && ip . To4 () == nil
416
+ return ip != nil && strings . Contains ( ipStr , ":" )
415
417
}
416
418
417
419
// 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) {
2419
2419
{"2001:cdba:0000:0000:0000:0000:3257:9652" , true },
2420
2420
{"2001:cdba:0:0:0:0:3257:9652" , true },
2421
2421
{"2001:cdba::3257:9652" , true },
2422
+ {"::ffff:192.168.1.1" , true },
2423
+ {"::ffff:c0a8:0101" , true },
2422
2424
}
2423
2425
2424
2426
validate := New ()
@@ -2459,6 +2461,8 @@ func TestIPv4Validation(t *testing.T) {
2459
2461
{"2001:cdba:0000:0000:0000:0000:3257:9652" , false },
2460
2462
{"2001:cdba:0:0:0:0:3257:9652" , false },
2461
2463
{"2001:cdba::3257:9652" , false },
2464
+ {"::ffff:192.168.1.1" , false },
2465
+ {"::ffff:c0a8:0101" , false },
2462
2466
}
2463
2467
2464
2468
validate := New ()
You can’t perform that action at this time.
0 commit comments