Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions internal/fields/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,20 @@ import (

// FieldDefinition describes a single field with its properties.
type FieldDefinition struct {
Name string `yaml:"name"`
Description string `yaml:"description"`
Type string `yaml:"type"`
Value string `yaml:"value"` // The value to associate with a constant_keyword field.
AllowedValues AllowedValues `yaml:"allowed_values"`
Pattern string `yaml:"pattern"`
Unit string `yaml:"unit"`
MetricType string `yaml:"metric_type"`
External string `yaml:"external"`
Index *bool `yaml:"index"`
DocValues *bool `yaml:"doc_values"`
Fields FieldDefinitions `yaml:"fields,omitempty"`
MultiFields []FieldDefinition `yaml:"multi_fields,omitempty"`
Name string `yaml:"name"`
Description string `yaml:"description"`
Type string `yaml:"type"`
Value string `yaml:"value"` // The value to associate with a constant_keyword field.
AllowedValues AllowedValues `yaml:"allowed_values"`
ExpectedValues []string `yaml:"expected_values"`
Pattern string `yaml:"pattern"`
Unit string `yaml:"unit"`
MetricType string `yaml:"metric_type"`
External string `yaml:"external"`
Index *bool `yaml:"index"`
DocValues *bool `yaml:"doc_values"`
Fields FieldDefinitions `yaml:"fields,omitempty"`
MultiFields []FieldDefinition `yaml:"multi_fields,omitempty"`
}

func (orig *FieldDefinition) Update(fd FieldDefinition) {
Expand All @@ -46,6 +47,9 @@ func (orig *FieldDefinition) Update(fd FieldDefinition) {
if len(fd.AllowedValues) > 0 {
orig.AllowedValues = fd.AllowedValues
}
if len(fd.ExpectedValues) > 0 {
orig.ExpectedValues = fd.ExpectedValues
}
if fd.Pattern != "" {
orig.Pattern = fd.Pattern
}
Expand Down
13 changes: 8 additions & 5 deletions internal/fields/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ func (v *Validator) parseSingleElementValue(key string, definition FieldDefiniti
if err := ensurePatternMatches(key, valStr, definition.Pattern); err != nil {
return err
}
if err := ensureAllowedValues(key, valStr, definition.AllowedValues); err != nil {
if err := ensureAllowedValues(key, valStr, definition); err != nil {
return err
}
// Normal text fields should be of type string.
Expand All @@ -405,7 +405,7 @@ func (v *Validator) parseSingleElementValue(key string, definition FieldDefiniti
if err := ensurePatternMatches(key, valStr, definition.Pattern); err != nil {
return err
}
if err := ensureAllowedValues(key, valStr, definition.AllowedValues); err != nil {
if err := ensureAllowedValues(key, valStr, definition); err != nil {
return err
}
// Dates are expected to be formatted as strings or as seconds or milliseconds
Expand Down Expand Up @@ -540,9 +540,12 @@ func ensureConstantKeywordValueMatches(key, value, constantKeywordValue string)

// ensureAllowedValues validates that the document's field value
// is one of the allowed values.
func ensureAllowedValues(key, value string, allowedValues AllowedValues) error {
if !allowedValues.IsAllowed(value) {
return fmt.Errorf("field %q's value %q is not one of the allowed values (%s)", key, value, strings.Join(allowedValues.Values(), ", "))
func ensureAllowedValues(key, value string, definition FieldDefinition) error {
if !definition.AllowedValues.IsAllowed(value) {
return fmt.Errorf("field %q's value %q is not one of the allowed values (%s)", key, value, strings.Join(definition.AllowedValues.Values(), ", "))
}
if e := definition.ExpectedValues; len(e) > 0 && !common.StringSliceContains(e, value) {
return fmt.Errorf("field %q's value %q is not one of the expected values (%s)", key, value, strings.Join(e, ", "))
}
return nil
}
18 changes: 18 additions & 0 deletions internal/fields/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,24 @@ func Test_parseElementValue(t *testing.T) {
},
fail: true,
},
// expected values
{
key: "expected values",
value: "linux",
definition: FieldDefinition{
Type: "keyword",
ExpectedValues: []string{"linux", "windows"},
},
},
{
key: "not expected values",
value: "bsd",
definition: FieldDefinition{
Type: "keyword",
ExpectedValues: []string{"linux", "windows"},
},
fail: true,
},
// fields shouldn't be stored in groups
{
key: "host",
Expand Down