-
Notifications
You must be signed in to change notification settings - Fork 117
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
Changes from 2 commits
7dca9f8
7bb8e84
15a03a0
09dd49e
7ac09ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -22,18 +22,26 @@ import ( | |||
"github.com/cbroglie/mustache" | ||||
"gopkg.in/yaml.v3" | ||||
|
||||
"github.com/elastic/package-spec/v2/code/go/pkg/specerrors" | ||||
|
||||
"github.com/elastic/elastic-package/internal/common" | ||||
"github.com/elastic/elastic-package/internal/logger" | ||||
"github.com/elastic/elastic-package/internal/multierror" | ||||
"github.com/elastic/elastic-package/internal/packages" | ||||
"github.com/elastic/elastic-package/internal/packages/buildmanifest" | ||||
) | ||||
|
||||
// EPF - Elastic Package Fields [validation] | ||||
const ArrayOfObjectsErrorCode = "EPF00001" | ||||
|
||||
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. | ||||
|
@@ -783,11 +791,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, arrayOfObjectsErr) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think these errors could be filtered as it is the code. The methods that get the errors and filter are just for the errors from package-spec. This is in
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are right, this would need to be filtered at the moment of testing the package. This makes me wonder if this validation errors filtering fits here, or if this should be configured in tests. What do you think? I could continue without making this filterable by now. It would only affect packages using spec v3. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed by now. |
||||
} | ||||
if len(errs) == 0 { | ||||
return nil | ||||
} | ||||
return errs | ||||
default: | ||||
return fmt.Errorf("field %q is a group of fields, it cannot store values", key) | ||||
} | ||||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed by now.