@@ -3,6 +3,7 @@ package validate
33import (
44 "fmt"
55 "regexp"
6+ "strings"
67
78 "go.uber.org/multierr"
89)
@@ -12,6 +13,10 @@ type regexKV struct {
1213 V * regexp.Regexp
1314}
1415
16+ func (r regexKV ) String () string {
17+ return fmt .Sprintf ("%s=%s" , r .K , r .V )
18+ }
19+
1520// AllowedLabels allows labels to be validated against a set of allowed labels.
1621// The zero value is ready to use and denies all labels.
1722type AllowedLabels struct {
@@ -46,7 +51,11 @@ func (l *AllowedLabels) Validate(lbls map[string]string) error {
4651 violations = append (violations , l .ValidateLabel (k , v ))
4752 }
4853
49- return multierr .Combine (violations ... )
54+ if err := multierr .Combine (violations ... ); err != nil {
55+ return fmt .Errorf ("label validation failed: %w, allowed labels: %s" , err , l .formatLabels ())
56+ }
57+
58+ return nil
5059}
5160
5261func anchor (s string ) string {
@@ -63,3 +72,16 @@ func (l *AllowedLabels) ValidateLabel(key, value string) error {
6372
6473 return fmt .Errorf ("label %s=%s is not allowed" , key , value )
6574}
75+
76+ func (l * AllowedLabels ) String () string {
77+ return fmt .Sprintf ("allowed %s" , l .formatLabels ())
78+ }
79+
80+ func (l * AllowedLabels ) formatLabels () string {
81+ s := make ([]string , 0 , len (l .allowed ))
82+ for _ , allowed := range l .allowed {
83+ s = append (s , allowed .String ())
84+ }
85+
86+ return fmt .Sprintf ("{ %s }" , strings .Join (s , ", " ))
87+ }
0 commit comments