diff --git a/pkg/api/validation/path/name.go b/pkg/api/validation/path/name.go index 981d9bb465c..a50cd089dfb 100644 --- a/pkg/api/validation/path/name.go +++ b/pkg/api/validation/path/name.go @@ -35,25 +35,27 @@ func IsValidPathSegmentName(name string) []string { } } + var errors []string for _, illegalContent := range NameMayNotContain { if strings.Contains(name, illegalContent) { - return []string{fmt.Sprintf(`may not contain '%s'`, illegalContent)} + errors = append(errors, fmt.Sprintf(`may not contain '%s'`, illegalContent)) } } - return nil + return errors } // IsValidPathSegmentPrefix validates the name can be used as a prefix for a name which will be encoded as a path segment // It does not check for exact matches with disallowed names, since an arbitrary suffix might make the name valid func IsValidPathSegmentPrefix(name string) []string { + var errors []string for _, illegalContent := range NameMayNotContain { if strings.Contains(name, illegalContent) { - return []string{fmt.Sprintf(`may not contain '%s'`, illegalContent)} + errors = append(errors, fmt.Sprintf(`may not contain '%s'`, illegalContent)) } } - return nil + return errors } // ValidatePathSegmentName validates the name can be safely encoded as a path segment diff --git a/pkg/api/validation/path/name_test.go b/pkg/api/validation/path/name_test.go index 779412f823c..42890527388 100644 --- a/pkg/api/validation/path/name_test.go +++ b/pkg/api/validation/path/name_test.go @@ -130,3 +130,39 @@ func TestValidatePathSegmentName(t *testing.T) { } } } + +func TestValidateWithMultiErrors(t *testing.T) { + testcases := map[string]struct { + Name string + Prefix bool + ExpectedMsg []string + }{ + "slash,percent": { + Name: "foo//bar%", + Prefix: false, + ExpectedMsg: []string{"may not contain '/'", "may not contain '%'"}, + }, + "slash,percent,prefix": { + Name: "foo//bar%", + Prefix: true, + ExpectedMsg: []string{"may not contain '/'", "may not contain '%'"}, + }, + } + + for k, tc := range testcases { + msgs := ValidatePathSegmentName(tc.Name, tc.Prefix) + if len(tc.ExpectedMsg) == 0 && len(msgs) > 0 { + t.Errorf("%s: expected no message, got %v", k, msgs) + } + if len(tc.ExpectedMsg) > 0 && len(msgs) == 0 { + t.Errorf("%s: expected error message, got none", k) + } + if len(tc.ExpectedMsg) > 0 { + for i := 0; i < len(tc.ExpectedMsg); i++ { + if msgs[i] != tc.ExpectedMsg[i] { + t.Errorf("%s: expected message containing %q, got %v", k, tc.ExpectedMsg[i], msgs[i]) + } + } + } + } +}