Skip to content

Commit

Permalink
Validate arrays of objects (#1489)
Browse files Browse the repository at this point in the history
Add validation for arrays of objects, considering them as valid when
they are inside nested or group.
When group is used, a filterable error is given, mentioning that nested
should be used instead on this case.
  • Loading branch information
jsoriano authored Oct 5, 2023
1 parent 29ded9d commit beabd87
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 12 deletions.
20 changes: 17 additions & 3 deletions internal/fields/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ import (
var (
semver2_0_0 = semver.MustParse("2.0.0")
semver2_3_0 = semver.MustParse("2.3.0")
semver3_0_0 = semver.MustParse("3.0.0")

defaultExternal = "ecs"

errArrayOfObjects = errors.New("array of objects not used as nested type can lead to unexpected results")
)

// Validator is responsible for fields validation.
Expand Down Expand Up @@ -783,11 +786,22 @@ func (v *Validator) parseSingleElementValue(key string, definition FieldDefiniti
return fmt.Errorf("the IP %q is not one of the allowed test IPs (see: https://github.com/elastic/elastic-package/blob/main/internal/fields/_static/allowed_geo_ips.txt)", valStr)
}
// Groups should only contain nested fields, not single values.
case "group":
switch val.(type) {
case "group", "nested":
switch val := val.(type) {
case map[string]interface{}:
// TODO: This is probably an element from an array of objects,
// This is probably an element from an array of objects,
// even if not recommended, it should be validated.
if v.specVersion.LessThan(semver3_0_0) {
break
}
errs := v.validateMapElement(key, common.MapStr(val), doc)
if definition.Type == "group" {
errs = append(errs, errArrayOfObjects)
}
if len(errs) == 0 {
return nil
}
return errs
default:
return fmt.Errorf("field %q is a group of fields, it cannot store values", key)
}
Expand Down
85 changes: 76 additions & 9 deletions internal/fields/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import (
"sort"
"testing"

"github.com/Masterminds/semver/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/elastic/elastic-package/internal/common"
"github.com/elastic/elastic-package/internal/multierror"
)

type results struct {
Expand Down Expand Up @@ -309,10 +311,12 @@ func TestValidate_ExpectedDatasets(t *testing.T) {

func Test_parseElementValue(t *testing.T) {
for _, test := range []struct {
key string
value interface{}
definition FieldDefinition
fail bool
key string
value interface{}
definition FieldDefinition
fail bool
assertError func(t *testing.T, err error)
specVersion semver.Version
}{
// Arrays
{
Expand Down Expand Up @@ -619,17 +623,80 @@ func Test_parseElementValue(t *testing.T) {
},
},
},
// elements in arrays of objects should be validated
{
key: "details",
value: []interface{}{
map[string]interface{}{
"id": "somehost-id",
"hostname": "somehost",
},
},
definition: FieldDefinition{
Name: "details",
Type: "group",
Fields: []FieldDefinition{
{
Name: "id",
Type: "keyword",
},
},
},
specVersion: *semver3_0_0,
fail: true,
assertError: func(t *testing.T, err error) {
errs := err.(multierror.Error)
if assert.Len(t, errs, 2) {
assert.Contains(t, errs[0].Error(), `"details.hostname" is undefined`)
assert.ErrorIs(t, errs[1], errArrayOfObjects)
}
},
},
// elements in nested objects
{
key: "nested",
value: []interface{}{
map[string]interface{}{
"id": "somehost-id",
"hostname": "somehost",
},
},
definition: FieldDefinition{
Name: "nested",
Type: "nested",
Fields: []FieldDefinition{
{
Name: "id",
Type: "keyword",
},
},
},
specVersion: *semver3_0_0,
fail: true,
assertError: func(t *testing.T, err error) {
errs := err.(multierror.Error)
if assert.Len(t, errs, 1) {
assert.Contains(t, errs[0].Error(), `"nested.hostname" is undefined`)
}
},
},
} {
v := Validator{
disabledDependencyManagement: true,
enabledAllowedIPCheck: true,
allowedCIDRs: initializeAllowedCIDRsList(),
}

t.Run(test.key, func(t *testing.T) {
v := Validator{
Schema: []FieldDefinition{test.definition},
disabledDependencyManagement: true,
enabledAllowedIPCheck: true,
allowedCIDRs: initializeAllowedCIDRsList(),
specVersion: test.specVersion,
}

err := v.parseElementValue(test.key, test.definition, test.value, common.MapStr{})
if test.fail {
require.Error(t, err)
if test.assertError != nil {
test.assertError(t, err)
}
} else {
require.NoError(t, err)
}
Expand Down

0 comments on commit beabd87

Please sign in to comment.