Skip to content

Commit d2e3d77

Browse files
authored
Fix validation of nested fields (#279)
1 parent 50227cd commit d2e3d77

File tree

5 files changed

+44
-27
lines changed

5 files changed

+44
-27
lines changed

code/go/internal/validator/semantic/types.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,32 @@ func validateFields(pkgRoot string, validate validateFunc) ve.ValidationErrors {
4343
vErrs = append(vErrs, errors.Wrapf(err, `file "%s" is invalid: can't unmarshal fields`, fieldsFile))
4444
}
4545

46-
for _, u := range unmarshaled {
47-
errs := validate(fieldsFile, u)
46+
errs := validateNestedFields("", fieldsFile, unmarshaled, validate)
47+
if len(errs) > 0 {
48+
vErrs = append(vErrs, errs...)
49+
}
50+
}
51+
return vErrs
52+
}
53+
54+
func validateNestedFields(parent string, fieldsFile string, fields fields, validate validateFunc) ve.ValidationErrors {
55+
var result ve.ValidationErrors
56+
for _, field := range fields {
57+
if len(parent) > 0 {
58+
field.Name = parent + "." + field.Name
59+
}
60+
errs := validate(fieldsFile, field)
61+
if len(errs) > 0 {
62+
result = append(result, errs...)
63+
}
64+
if len(field.Fields) > 0 {
65+
errs := validateNestedFields(field.Name, fieldsFile, field.Fields, validate)
4866
if len(errs) > 0 {
49-
vErrs = append(vErrs, errs...)
67+
result = append(result, errs...)
5068
}
5169
}
5270
}
53-
return vErrs
71+
return result
5472
}
5573

5674
func listFieldsFiles(pkgRoot string) ([]string, error) {

code/go/internal/validator/semantic/validate_field_groups.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,5 @@ func validateFieldUnit(fieldsFile string, f field) errors.ValidationErrors {
2424
return errors.ValidationErrors{fmt.Errorf(`file "%s" is invalid: field "%s" can't have metric type property'`, fieldsFile, f.Name)}
2525
}
2626

27-
var vErrs errors.ValidationErrors
28-
for _, aField := range f.Fields {
29-
errs := validateFieldUnit(fieldsFile, aField)
30-
if len(errs) > 0 {
31-
vErrs = append(vErrs, errs...)
32-
}
33-
}
34-
return vErrs
27+
return nil
3528
}

code/go/internal/validator/semantic/validate_field_groups_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ func TestValidateFieldGroups_Bad(t *testing.T) {
3131

3232
errs := ValidateFieldGroups(pkgRoot)
3333
if assert.Len(t, errs, 3) {
34-
assert.Equal(t, fileError("data_stream/bar/fields/hello-world.yml", `field "bbb" can't have unit property'`), errs[0].Error())
35-
assert.Equal(t, fileError("data_stream/bar/fields/hello-world.yml", `field "eee" can't have unit property'`), errs[1].Error())
34+
assert.Equal(t, fileError("data_stream/bar/fields/hello-world.yml", `field "aaa.bbb" can't have unit property'`), errs[0].Error())
35+
assert.Equal(t, fileError("data_stream/bar/fields/hello-world.yml", `field "ddd.eee" can't have unit property'`), errs[1].Error())
3636
assert.Equal(t, fileError("data_stream/foo/fields/bad-file.yml", `field "fff" can't have metric type property'`), errs[2].Error())
3737
}
3838
}
Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
- name: example.agent.id
2-
type: keyword
3-
dimension: true
4-
- name: example.agent.call_count
5-
type: long
6-
metric_type: counter
7-
- name: example.agent.current_count
8-
type: long
9-
metric_type: gauge
10-
- name: example.agent.call_duration
11-
type: histogram
12-
metric_type: gauge
13-
dimension: true # This should fail, a histogram cannot be a dimension.
1+
- name: example
2+
type: group
3+
fields:
4+
- name: agent.id
5+
type: keyword
6+
dimension: true
7+
- name: agent.call_count
8+
type: long
9+
metric_type: counter
10+
- name: agent.current_count
11+
type: long
12+
metric_type: gauge
13+
- name: agent.call_duration
14+
type: histogram
15+
metric_type: gauge
16+
dimension: true # This should fail, a histogram cannot be a dimension.

versions/1/changelog.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
- description: Add kibana/csp-rule-template asset
1111
type: enhancement
1212
link: https://github.com/elastic/package-spec/pull/276
13+
- description: Fix validation of dimension fields inside objects
14+
type: bugfix
15+
link: https://github.com/elastic/package-spec/pull/279
1316
- version: 1.4.1
1417
changes:
1518
- description: ML model file name now matches the id of the model.

0 commit comments

Comments
 (0)