Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate arrays of objects #1489

Merged
merged 5 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions internal/fields/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,20 @@ import (
"github.com/elastic/elastic-package/internal/multierror"
"github.com/elastic/elastic-package/internal/packages"
"github.com/elastic/elastic-package/internal/packages/buildmanifest"
"github.com/elastic/package-spec/v2/code/go/pkg/specerrors"
)

// EPF - Elastic Package Fields [validation]
const ArrayOfObjectsErrorCode = "EPF00001"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mrodm do we have a place where we are already defining Elastic Package errors?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, there is no definition of errors in Elastic Package. Until now, it was just getting the errors from package-spec validate method and return the errors filtered (if any).

Should this be added into a new internal/errors package ? Or maybe another naming to not have collisions with standard errors package (e.g. validationerrors? I don't like neither too much this).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed by now.


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"

arrayOfObjectsErr = specerrors.NewStructuredError(errors.New("array of objects not used as nested type can lead to unexpected results"), ArrayOfObjectsErrorCode)
)

// Validator is responsible for fields validation.
Expand Down Expand Up @@ -784,10 +791,16 @@ func (v *Validator) parseSingleElementValue(key string, definition FieldDefiniti
}
// Groups should only contain nested fields, not single values.
case "group":
switch val.(type) {
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)
errs = append(errs, arrayOfObjectsErr)
return errs
default:
return fmt.Errorf("field %q is a group of fields, it cannot store values", key)
}
Expand Down
57 changes: 48 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,52 @@ 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], arrayOfObjectsErr)
}
},
},
} {
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