Skip to content

Commit

Permalink
Support dates as milliseconds or seconds (#781)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsoriano authored Apr 5, 2022
1 parent 2c00e6b commit 317f26f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
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

0 comments on commit 317f26f

Please sign in to comment.