-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathvalidate.go
More file actions
208 lines (177 loc) · 4.67 KB
/
Copy pathvalidate.go
File metadata and controls
208 lines (177 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package manifest
import (
"bytes"
"encoding/json"
"fmt"
"sort"
"strings"
schema "github.com/open-feature/cli/schema/v0"
"github.com/xeipuuv/gojsonschema"
)
type ValidationError struct {
Type string `json:"type"`
Path string `json:"path"`
Message string `json:"message"`
}
func Validate(data []byte) ([]ValidationError, error) {
schemaLoader := gojsonschema.NewStringLoader(schema.SchemaFile)
manifestLoader := gojsonschema.NewBytesLoader(data)
result, err := gojsonschema.Validate(schemaLoader, manifestLoader)
if err != nil {
return nil, fmt.Errorf("failed to validate manifest: %w", err)
}
var issues []ValidationError
for _, err := range result.Errors() {
if strings.HasPrefix(err.Field(), "flags") && err.Type() == "number_one_of" {
issues = append(issues, ValidationError{
Type: err.Type(),
Path: err.Field(),
Message: "flagType must be 'boolean', 'string', 'integer', 'float', or 'object'",
})
} else {
issues = append(issues, ValidationError{
Type: err.Type(),
Path: err.Field(),
Message: err.Description(),
})
}
}
// Check for duplicate flag keys
duplicates := findDuplicateFlagKeys(data)
for _, key := range duplicates {
issues = append(issues, ValidationError{
Type: "duplicate_key",
Path: fmt.Sprintf("flags.%s", key),
Message: fmt.Sprintf("flag '%s' is defined multiple times in the manifest", key),
})
}
return issues, nil
}
// findDuplicateFlagKeys parses the raw JSON to detect duplicate keys within the "flags" object.
// Standard JSON unmarshaling silently accepts duplicates (taking the last value), so we use
// a token-based approach to detect them.
func findDuplicateFlagKeys(data []byte) []string {
decoder := json.NewDecoder(bytes.NewReader(data))
// Navigate to the root object
token, err := decoder.Token()
if err != nil || token != json.Delim('{') {
return nil
}
// Look for the "flags" key at the top level
for decoder.More() {
keyToken, err := decoder.Token()
if err != nil {
return nil
}
key, ok := keyToken.(string)
if !ok {
continue
}
if key == "flags" {
return findDuplicatesInObject(decoder)
}
// Skip the value for non-"flags" keys
skipValue(decoder)
}
return nil
}
// findDuplicatesInObject reads an object from the decoder and returns any duplicate keys.
func findDuplicatesInObject(decoder *json.Decoder) []string {
token, err := decoder.Token()
if err != nil || token != json.Delim('{') {
return nil
}
seen := make(map[string]bool)
var duplicates []string
for decoder.More() {
keyToken, err := decoder.Token()
if err != nil {
break
}
key, ok := keyToken.(string)
if !ok {
continue
}
if seen[key] {
duplicates = append(duplicates, key)
} else {
seen[key] = true
}
// Skip the value
skipValue(decoder)
}
// Consume the closing brace
_, err = decoder.Token()
if err != nil {
return duplicates
}
// Sort for consistent output
sort.Strings(duplicates)
return duplicates
}
// skipValue advances the decoder past one complete JSON value (object, array, or primitive).
func skipValue(decoder *json.Decoder) {
token, err := decoder.Token()
if err != nil {
return
}
switch t := token.(type) {
case json.Delim:
switch t {
case '{':
// Skip object contents
for decoder.More() {
if _, err := decoder.Token(); err != nil { // key
return
}
skipValue(decoder)
}
if _, err := decoder.Token(); err != nil { // closing }
return
}
case '[':
// Skip array contents
for decoder.More() {
skipValue(decoder)
}
if _, err := decoder.Token(); err != nil { // closing ]
return
}
}
}
// Primitives (string, number, bool, null) are already consumed by the Token() call
}
func FormatValidationError(issues []ValidationError) string {
var sb strings.Builder
sb.WriteString("flag manifest validation failed:\n\n")
// Group messages by flag path
grouped := make(map[string]struct {
flagType string
messages []string
})
for _, issue := range issues {
entry := grouped[issue.Path]
entry.flagType = issue.Type
entry.messages = append(entry.messages, issue.Message)
grouped[issue.Path] = entry
}
// Sort paths for consistent output
paths := make([]string, 0, len(grouped))
for path := range grouped {
paths = append(paths, path)
}
sort.Strings(paths)
// Format each row
for _, path := range paths {
entry := grouped[path]
flagType := entry.flagType
if flagType == "" {
flagType = "missing"
}
fmt.Fprintf(&sb, "- flagType: %s\n flagPath: %s\n errors:\n ~ %s\n \tSuggestions:\n \t- flagType: boolean\n \t- defaultValue: true\n\n",
flagType,
path,
strings.Join(entry.messages, "\n ~ "))
}
return sb.String()
}