Skip to content

Commit a9e4fe6

Browse files
feat(flagset): improve validation error formatting in Load function
Signed-off-by: Adityasinghvats <131326798+Adityasinghvats@users.noreply.github.com>
1 parent 593bffa commit a9e4fe6

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

internal/flagset/flagset.go

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"sort"
8+
"strings"
89

910
"github.com/open-feature/cli/internal/filesystem"
1011
"github.com/open-feature/cli/internal/manifest"
@@ -64,7 +65,7 @@ func Load(manifestPath string) (*Flagset, error) {
6465
if err != nil {
6566
return nil, err
6667
} else if len(validationErrors) > 0 {
67-
return nil, fmt.Errorf("validation failed: %v", validationErrors)
68+
return nil, errors.New(FormatValidationError(validationErrors))
6869
}
6970

7071
var flagset Flagset
@@ -132,3 +133,47 @@ func (fs *Flagset) UnmarshalJSON(data []byte) error {
132133

133134
return nil
134135
}
136+
func FormatValidationError(issues []manifest.ValidationError) string {
137+
var sb strings.Builder
138+
sb.WriteString("flag manifest validation failed:\n")
139+
sb.WriteString("+--------------+-------------+------------------+\n")
140+
sb.WriteString("| flag type | flag path | error messages |\n")
141+
sb.WriteString("+--------------+-------------+------------------+\n")
142+
143+
// Group messages by flag path
144+
grouped := make(map[string]struct {
145+
flagType string
146+
messages []string
147+
})
148+
149+
for _, issue := range issues {
150+
entry := grouped[issue.Path]
151+
entry.flagType = issue.Type
152+
entry.messages = append(entry.messages, issue.Message)
153+
grouped[issue.Path] = entry
154+
}
155+
156+
// Sort paths for consistent output
157+
paths := make([]string, 0, len(grouped))
158+
for path := range grouped {
159+
paths = append(paths, path)
160+
}
161+
sort.Strings(paths)
162+
163+
// Format each row
164+
for _, path := range paths {
165+
entry := grouped[path]
166+
sb.WriteString(fmt.Sprintf("- [%-12s] [%-11s]\n \t~ %-16s \n\t\t%s\n \t\t\t%s:\n \t\t\t\t%s\n\t\t\t\t%s \n\n",
167+
entry.flagType,
168+
path,
169+
strings.Join(entry.messages, ", \n\t~ "),
170+
"Suggestions:",
171+
path,
172+
"flagType: boolean",
173+
"defaultValue: true",
174+
))
175+
}
176+
177+
sb.WriteString("+--------------+-------------+------------------+\n")
178+
return sb.String()
179+
}

0 commit comments

Comments
 (0)