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

Fix validations based on fields imported from ECS #1452

Merged
merged 4 commits into from
Sep 18, 2023

Conversation

jsoriano
Copy link
Member

We inject fields from ECS on different scenarios: when generating documentation, when building packages, or when validating documents in tests.

On #1335 we did a refactor around this, to fix some validation issues, in a way that more code is reused between all these uses. After this change, validation used field definitions that include the already resolved external fields, but we weren't including there information that was used by validators, what included:

  • Validation of expected values.
  • Validation of allowed values.

So these validations haven't been executed since then.

Tests have been included to try to avoid regressions related to this in the future.

Fix #1439

@jsoriano jsoriano requested a review from a team September 13, 2023 16:28
@jsoriano jsoriano self-assigned this Sep 13, 2023
@@ -318,17 +333,28 @@ func transformImportedField(fd FieldDefinition) common.MapStr {
m["doc_values"] = *fd.DocValues
}

if len(fd.Normalize) > 0 {
m["normalize"] = fd.Normalize
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Moving this to the if below will effectively remove normalize from the built packages. But I think this is not needed there? 🤔

I could revert this change if we consider it risky.

Copy link
Contributor

Choose a reason for hiding this comment

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

If this field is not used afterwards by Elasticsearch, I think it could be moved to that new if block.

And IIRC , this function is just executed while doing comparing documents (testing) with external fields. In built packages, I guess it's not needed.

@jsoriano
Copy link
Member Author

/test

@elasticmachine
Copy link
Collaborator

💚 Build Succeeded

History

cc @jsoriano

@@ -318,17 +333,28 @@ func transformImportedField(fd FieldDefinition) common.MapStr {
m["doc_values"] = *fd.DocValues
}

if len(fd.Normalize) > 0 {
m["normalize"] = fd.Normalize
}
Copy link
Contributor

Choose a reason for hiding this comment

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

If this field is not used afterwards by Elasticsearch, I think it could be moved to that new if block.

And IIRC , this function is just executed while doing comparing documents (testing) with external fields. In built packages, I guess it's not needed.

Comment on lines +340 to +352
if options.IncludeValidationSettings {
if len(fd.Normalize) > 0 {
m["normalize"] = fd.Normalize
}

if len(fd.AllowedValues) > 0 {
m["allowed_values"] = fd.AllowedValues
}

if len(fd.ExpectedValues) > 0 {
m["expected_values"] = fd.ExpectedValues
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This means that these fields (normalize, allowed_values, expected_values) won't be added to each field definition when the package are built, is that right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, this is not used in Fleet or Elasticsearch.

Comment on lines +680 to +687
checkFn: func(t *testing.T, result []common.MapStr) {
require.Len(t, result, 1)
_, ok := result[0]["allowed_values"]
if !assert.True(t, ok) {
d, _ := json.MarshalIndent(result[0], "", " ")
t.Logf("expected to find allowed_values in %s", string(d))
}
},
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@@ -163,6 +163,8 @@ func CreateValidatorForDirectory(fieldsParentDir string, opts ...ValidatorOption

func createValidatorForDirectoryAndPackageRoot(fieldsParentDir string, finder packageRootFinder, opts ...ValidatorOption) (v *Validator, err error) {
v = new(Validator)
// In validator, inject fields with settings used for validation, such as `allowed_values`.
v.injectFieldsOptions.IncludeValidationSettings = true
Copy link
Contributor

Choose a reason for hiding this comment

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

If it is set to true here, every run of validation would use those fields, even in built packages ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Talked offline, and this should be just used for validators. Built packages do not use this one.

@jsoriano
Copy link
Member Author

test integrations

@elasticmachine
Copy link
Collaborator

Created or updated PR in integrations repostiory to test this vesrion. Check elastic/integrations#7817

Copy link
Contributor

@mrodm mrodm left a comment

Choose a reason for hiding this comment

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

Thanks!!

@jsoriano jsoriano merged commit cafa676 into elastic:main Sep 18, 2023
1 check passed
@jsoriano jsoriano deleted the test-load-ecs-schema branch September 18, 2023 19:00
bhapas pushed a commit to bhapas/elastic-package that referenced this pull request Sep 21, 2023
We inject fields from ECS on different scenarios: when generating documentation,
when building packages, or when validating documents in tests.

On elastic#1335 we did a refactor around this, to fix some validation issues, in a way that
more code is reused between all these uses. After this change, validation used field
definitions that include the already resolved external fields, but we weren't including
there information that was used by validators, what included validation of expected
and allowed values.

So these validations haven't been executed since then.

Tests have been included to try to avoid regressions related to this in the future.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Possible regression in field values validation
3 participants