From 89b941452299f7f7920eee87862a93ca7ad7b166 Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Tue, 26 Dec 2023 21:11:15 -0500 Subject: [PATCH] Make validation.IsValidIP return a field.ErrorList for consistency Kubernetes-commit: 519dd6887dc275f77d5be32a1f711ff71117c87d --- pkg/util/validation/validation.go | 7 ++++--- pkg/util/validation/validation_test.go | 16 ++++++++-------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/pkg/util/validation/validation.go b/pkg/util/validation/validation.go index 6f8a2e26b..0eb33e68a 100644 --- a/pkg/util/validation/validation.go +++ b/pkg/util/validation/validation.go @@ -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. diff --git a/pkg/util/validation/validation_test.go b/pkg/util/validation/validation_test.go index aeda7bf56..2ce11d560 100644 --- a/pkg/util/validation/validation_test.go +++ b/pkg/util/validation/validation_test.go @@ -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)