Skip to content

Commit 317f26f

Browse files
authored
Support dates as milliseconds or seconds (#781)
1 parent 2c00e6b commit 317f26f

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

internal/fields/validate.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ func (v *Validator) parseElementValue(key string, definition FieldDefinition, va
374374
if err := ensurePatternMatches(key, valStr, definition.Pattern); err != nil {
375375
return err
376376
}
377-
case "date", "keyword", "text":
377+
case "keyword", "text":
378378
var valStr string
379379
valStr, valid = val.(string)
380380
if !valid {
@@ -384,6 +384,22 @@ func (v *Validator) parseElementValue(key string, definition FieldDefinition, va
384384
if err := ensurePatternMatches(key, valStr, definition.Pattern); err != nil {
385385
return err
386386
}
387+
case "date":
388+
switch val := val.(type) {
389+
case string:
390+
if err := ensurePatternMatches(key, val, definition.Pattern); err != nil {
391+
return err
392+
}
393+
valid = true
394+
case float64:
395+
// date as seconds or milliseconds since epoch
396+
if definition.Pattern != "" {
397+
return fmt.Errorf("numeric date in field %q, but pattern defined", key)
398+
}
399+
valid = true
400+
default:
401+
valid = false
402+
}
387403
case "ip":
388404
var valStr string
389405
valStr, valid = val.(string)

internal/fields/validate_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,22 @@ func Test_parseElementValue(t *testing.T) {
151151
Pattern: "^[0-9]{4}(-[0-9]{2}){2}[T ][0-9]{2}(:[0-9]{2}){2}Z$",
152152
},
153153
},
154+
{
155+
key: "date as milliseconds",
156+
value: float64(1420070400001),
157+
definition: FieldDefinition{
158+
Type: "date",
159+
},
160+
},
161+
{
162+
key: "date as milisecond with pattern",
163+
value: float64(1420070400001),
164+
definition: FieldDefinition{
165+
Type: "date",
166+
Pattern: "^[0-9]{4}(-[0-9]{2}){2}[T ][0-9]{2}(:[0-9]{2}){2}Z$",
167+
},
168+
fail: true,
169+
},
154170
{
155171
key: "bad date",
156172
value: "10 Oct 2020 3:42PM",

0 commit comments

Comments
 (0)