Skip to content

Commit 712e237

Browse files
authored
Permissive validation of arrays of objects (#819)
Some integrations are storing arrays of objects in group types. This is valid, even if not recommended. Be permissive on these cases by now.
1 parent 155ca7e commit 712e237

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

internal/fields/validate.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,14 @@ func (v *Validator) parseElementValue(key string, definition FieldDefinition, va
417417
case "float", "long", "double":
418418
_, valid = val.(float64)
419419
case "group":
420-
return fmt.Errorf("field %q is a group of fields, it cannot store values", key)
420+
switch val.(type) {
421+
case map[string]interface{}:
422+
// TODO: This is probably an element from an array of objects,
423+
// even if not recommended, it should be validated.
424+
valid = true
425+
default:
426+
return fmt.Errorf("field %q is a group of fields, it cannot store values", key)
427+
}
421428
default:
422429
valid = true // all other types are considered valid not blocking validation
423430
}

internal/fields/validate_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,34 @@ func Test_parseElementValue(t *testing.T) {
237237
},
238238
fail: true,
239239
},
240+
// arrays of objects can be stored in groups, even if not recommended
241+
{
242+
key: "host",
243+
value: []interface{}{
244+
map[string]interface{}{
245+
"id": "somehost-id",
246+
"hostname": "somehost",
247+
},
248+
map[string]interface{}{
249+
"id": "otherhost-id",
250+
"hostname": "otherhost",
251+
},
252+
},
253+
definition: FieldDefinition{
254+
Name: "host",
255+
Type: "group",
256+
Fields: []FieldDefinition{
257+
{
258+
Name: "id",
259+
Type: "keyword",
260+
},
261+
{
262+
Name: "hostname",
263+
Type: "keyword",
264+
},
265+
},
266+
},
267+
},
240268
} {
241269
v := Validator{disabledDependencyManagement: true}
242270
t.Run(test.key, func(t *testing.T) {

0 commit comments

Comments
 (0)