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
18 changes: 17 additions & 1 deletion internal/fields/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ func (v *Validator) parseElementValue(key string, definition FieldDefinition, va
if err := ensurePatternMatches(key, valStr, definition.Pattern); err != nil {
return err
}
case "date", "keyword", "text":
case "keyword", "text":
var valStr string
valStr, valid = val.(string)
if !valid {
Expand All @@ -384,6 +384,22 @@ func (v *Validator) parseElementValue(key string, definition FieldDefinition, va
if err := ensurePatternMatches(key, valStr, definition.Pattern); err != nil {
return err
}
case "date":
switch val := val.(type) {
case string:
if err := ensurePatternMatches(key, val, definition.Pattern); err != nil {
return err
}
valid = true
case float64:
// date as seconds or milliseconds since epoch
if definition.Pattern != "" {
return fmt.Errorf("numeric date in field %q, but pattern defined", key)
}
valid = true
default:
valid = false
}
case "ip":
var valStr string
valStr, valid = val.(string)
Expand Down
16 changes: 16 additions & 0 deletions internal/fields/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,22 @@ func Test_parseElementValue(t *testing.T) {
Pattern: "^[0-9]{4}(-[0-9]{2}){2}[T ][0-9]{2}(:[0-9]{2}){2}Z$",
},
},
{
key: "date as milliseconds",
value: float64(1420070400001),
definition: FieldDefinition{
Type: "date",
},
},
{
key: "date as milisecond with pattern",
value: float64(1420070400001),
definition: FieldDefinition{
Type: "date",
Pattern: "^[0-9]{4}(-[0-9]{2}){2}[T ][0-9]{2}(:[0-9]{2}){2}Z$",
},
fail: true,
},
{
key: "bad date",
value: "10 Oct 2020 3:42PM",
Expand Down