Skip to content

Commit

Permalink
Permissive validation of arrays of objects (#819)
Browse files Browse the repository at this point in the history
Some integrations are storing arrays of objects in group types. This is
valid, even if not recommended. Be permissive on these cases by now.
  • Loading branch information
jsoriano authored May 17, 2022
1 parent 155ca7e commit 712e237
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
9 changes: 8 additions & 1 deletion internal/fields/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,14 @@ func (v *Validator) parseElementValue(key string, definition FieldDefinition, va
case "float", "long", "double":
_, valid = val.(float64)
case "group":
return fmt.Errorf("field %q is a group of fields, it cannot store values", key)
switch val.(type) {
case map[string]interface{}:
// TODO: This is probably an element from an array of objects,
// even if not recommended, it should be validated.
valid = true
default:
return fmt.Errorf("field %q is a group of fields, it cannot store values", key)
}
default:
valid = true // all other types are considered valid not blocking validation
}
Expand Down
28 changes: 28 additions & 0 deletions internal/fields/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,34 @@ func Test_parseElementValue(t *testing.T) {
},
fail: true,
},
// arrays of objects can be stored in groups, even if not recommended
{
key: "host",
value: []interface{}{
map[string]interface{}{
"id": "somehost-id",
"hostname": "somehost",
},
map[string]interface{}{
"id": "otherhost-id",
"hostname": "otherhost",
},
},
definition: FieldDefinition{
Name: "host",
Type: "group",
Fields: []FieldDefinition{
{
Name: "id",
Type: "keyword",
},
{
Name: "hostname",
Type: "keyword",
},
},
},
},
} {
v := Validator{disabledDependencyManagement: true}
t.Run(test.key, func(t *testing.T) {
Expand Down

0 comments on commit 712e237

Please sign in to comment.