Skip to content

Commit

Permalink
Fix enabled: false validation (#2192)
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksmaus authored Nov 8, 2024
1 parent 874d2ab commit 5867f9d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/fields/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type FieldDefinition struct {
MetricType string `yaml:"metric_type"`
External string `yaml:"external"`
Index *bool `yaml:"index"`
Enabled *bool `yaml:"enabled"`
DocValues *bool `yaml:"doc_values"`
Normalize []string `yaml:"normalize,omitempty"`
Fields FieldDefinitions `yaml:"fields,omitempty"`
Expand Down
13 changes: 13 additions & 0 deletions internal/fields/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,8 @@ func (v *Validator) validateScalarElement(key string, val any, doc common.MapStr
return fmt.Errorf(`field %q is used as array of objects, expected explicit definition with type group or nested`, key)
case couldBeMultifield(key, v.Schema):
return fmt.Errorf(`field %q is undefined, could be a multifield`, key)
case !isParentEnabled(key, v.Schema):
return nil // parent mapping is disabled
default:
return fmt.Errorf(`field %q is undefined`, key)
}
Expand Down Expand Up @@ -879,6 +881,17 @@ func couldBeMultifield(key string, fieldDefinitions []FieldDefinition) bool {
return true
}

// isParentEnabled returns true by default unless the parent field exists and enabled is set false
// This is needed in order to correctly validate the fields that should not be mapped
// because parent field mapping was disabled
func isParentEnabled(key string, fieldDefinitions []FieldDefinition) bool {
parent := findParentElementDefinition(key, fieldDefinitions)
if parent != nil && parent.Enabled != nil && !*parent.Enabled {
return false
}
return true
}

func isArrayOfObjects(val any) bool {
switch val := val.(type) {
case []map[string]any:
Expand Down

0 comments on commit 5867f9d

Please sign in to comment.