From 5867f9da403109ced67ee387d896f964fa21c42f Mon Sep 17 00:00:00 2001 From: Aleksandr Maus Date: Fri, 8 Nov 2024 08:48:57 -0500 Subject: [PATCH] Fix enabled: false validation (#2192) --- internal/fields/model.go | 1 + internal/fields/validate.go | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/internal/fields/model.go b/internal/fields/model.go index 9bd970d3a8..b724639554 100644 --- a/internal/fields/model.go +++ b/internal/fields/model.go @@ -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"` diff --git a/internal/fields/validate.go b/internal/fields/validate.go index 20cde32a53..97adc64c30 100644 --- a/internal/fields/validate.go +++ b/internal/fields/validate.go @@ -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) } @@ -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: