Skip to content

Commit f1b90c7

Browse files
authored
Support validation of arrays of arrays of nested objects (#1498)
1 parent 91fe607 commit f1b90c7

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

internal/fields/validate.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,13 @@ func (v *Validator) parseSingleElementValue(key string, definition FieldDefiniti
797797
return nil
798798
}
799799
return errs
800+
case []interface{}:
801+
// This can be an array of array of objects. Elasticsearh will probably
802+
// flatten this. So even if this is quite unexpected, let's try to handle it.
803+
if v.specVersion.LessThan(semver3_0_1) {
804+
break
805+
}
806+
return forEachElementValue(key, definition, val, doc, v.parseSingleElementValue)
800807
default:
801808
return fmt.Errorf("field %q is a group of fields, it cannot store values", key)
802809
}

internal/fields/validate_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,63 @@ func Test_parseElementValue(t *testing.T) {
679679
}
680680
},
681681
},
682+
// arrays of elements in nested objects
683+
{
684+
key: "good_array_of_nested",
685+
value: []interface{}{
686+
[]interface{}{
687+
map[string]interface{}{
688+
"id": "somehost-id",
689+
"hostname": "somehost",
690+
},
691+
},
692+
},
693+
definition: FieldDefinition{
694+
Name: "good_array_of_nested",
695+
Type: "nested",
696+
Fields: []FieldDefinition{
697+
{
698+
Name: "id",
699+
Type: "keyword",
700+
},
701+
{
702+
Name: "hostname",
703+
Type: "keyword",
704+
},
705+
},
706+
},
707+
specVersion: *semver3_0_1,
708+
},
709+
{
710+
key: "array_of_nested",
711+
value: []interface{}{
712+
[]interface{}{
713+
map[string]interface{}{
714+
"id": "somehost-id",
715+
"hostname": "somehost",
716+
},
717+
},
718+
},
719+
definition: FieldDefinition{
720+
Name: "array_of_nested",
721+
Type: "nested",
722+
Fields: []FieldDefinition{
723+
{
724+
Name: "id",
725+
Type: "keyword",
726+
},
727+
},
728+
},
729+
specVersion: *semver3_0_1,
730+
fail: true,
731+
assertError: func(t *testing.T, err error) {
732+
var errs multierror.Error
733+
require.ErrorAs(t, err, &errs)
734+
if assert.Len(t, errs, 1) {
735+
assert.Contains(t, errs[0].Error(), `"array_of_nested.hostname" is undefined`)
736+
}
737+
},
738+
},
682739
} {
683740

684741
t.Run(test.key, func(t *testing.T) {

0 commit comments

Comments
 (0)