From 712e237319295727f614b11b1199f244d1818f9f Mon Sep 17 00:00:00 2001 From: Jaime Soriano Pastor Date: Tue, 17 May 2022 14:40:07 +0200 Subject: [PATCH] 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. --- internal/fields/validate.go | 9 ++++++++- internal/fields/validate_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/internal/fields/validate.go b/internal/fields/validate.go index 3af27d5ac..d41751d36 100644 --- a/internal/fields/validate.go +++ b/internal/fields/validate.go @@ -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 } diff --git a/internal/fields/validate_test.go b/internal/fields/validate_test.go index 048da75f1..05fc3b298 100644 --- a/internal/fields/validate_test.go +++ b/internal/fields/validate_test.go @@ -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) {