Skip to content

Commit

Permalink
Make validation.IsValidIP return a field.ErrorList for consistency
Browse files Browse the repository at this point in the history
Kubernetes-commit: 519dd6887dc275f77d5be32a1f711ff71117c87d
  • Loading branch information
danwinship authored and k8s-publishing-bot committed Dec 27, 2023
1 parent cc2017e commit 89b9414
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
7 changes: 4 additions & 3 deletions pkg/util/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,12 @@ func IsValidPortName(port string) []string {
}

// IsValidIP tests that the argument is a valid IP address.
func IsValidIP(value string) []string {
func IsValidIP(fldPath *field.Path, value string) field.ErrorList {
var allErrors field.ErrorList
if netutils.ParseIPSloppy(value) == nil {
return []string{"must be a valid IP address, (e.g. 10.9.8.7 or 2001:db8::ffff)"}
allErrors = append(allErrors, field.Invalid(fldPath, value, "must be a valid IP address, (e.g. 10.9.8.7 or 2001:db8::ffff)"))
}
return nil
return allErrors
}

// IsValidIPv4Address tests that the argument is a valid IPv4 address.
Expand Down
16 changes: 8 additions & 8 deletions pkg/util/validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,20 +458,20 @@ func TestIsValidIP(t *testing.T) {
},
} {
t.Run(tc.name, func(t *testing.T) {
msgs := IsValidIP(tc.in)
errs := IsValidIP(field.NewPath(""), tc.in)
if tc.err == "" {
if len(msgs) != 0 {
t.Errorf("expected %q to be valid but got: %v", tc.in, msgs)
if len(errs) != 0 {
t.Errorf("expected %q to be valid but got: %v", tc.in, errs)
}
} else {
if len(msgs) != 1 {
t.Errorf("expected %q to have 1 error but got: %v", tc.in, msgs)
} else if !strings.Contains(msgs[0], tc.err) {
t.Errorf("expected error for %q to contain %q but got: %q", tc.in, tc.err, msgs[0])
if len(errs) != 1 {
t.Errorf("expected %q to have 1 error but got: %v", tc.in, errs)
} else if !strings.Contains(errs[0].Detail, tc.err) {
t.Errorf("expected error for %q to contain %q but got: %q", tc.in, tc.err, errs[0].Detail)
}
}

errs := IsValidIPv4Address(field.NewPath(""), tc.in)
errs = IsValidIPv4Address(field.NewPath(""), tc.in)
if tc.family == 4 {
if len(errs) != 0 {
t.Errorf("expected %q to pass IsValidIPv4Address but got: %v", tc.in, errs)
Expand Down

0 comments on commit 89b9414

Please sign in to comment.