forked from go-goyave/goyave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdates.go
107 lines (92 loc) · 2.43 KB
/
dates.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package validation
import (
"fmt"
"time"
)
func parseDate(date interface{}, format string) (time.Time, error) {
str, ok := date.(string)
if ok {
t, err := time.Parse(format, str)
if err == nil {
return t, err
}
return t, err
}
return time.Time{}, fmt.Errorf("Date is not a string so cannot be parsed")
}
func getDates(ctx *Context) ([]time.Time, error) {
dates := []time.Time{}
date, ok := ctx.Value.(time.Time)
if ok {
dates = append(dates, date)
for _, param := range ctx.Rule.Params {
_, other, _, exists := GetFieldFromName(param, ctx.Data)
if exists {
otherDate, ok := other.(time.Time)
if !ok {
t, err := parseDate(other, "2006-01-02")
if err != nil {
return dates, fmt.Errorf("Cannot parse date in other field")
}
otherDate = t
}
dates = append(dates, otherDate)
continue
}
t, err := parseDate(param, "2006-01-02T15:04:05")
if err != nil {
panic(err)
}
dates = append(dates, t)
}
return dates, nil
}
return dates, fmt.Errorf("Value is not a date")
}
func validateDate(ctx *Context) bool {
if len(ctx.Rule.Params) == 0 {
ctx.Rule.Params = append(ctx.Rule.Params, "2006-01-02")
}
t, err := parseDate(ctx.Value, ctx.Rule.Params[0])
if err == nil {
ctx.Value = t
return true
}
return false
}
func validateBefore(ctx *Context) bool {
dates, err := getDates(ctx)
return err == nil && dates[0].Before(dates[1])
}
func validateBeforeEqual(ctx *Context) bool {
dates, err := getDates(ctx)
return err == nil && (dates[0].Before(dates[1]) || dates[0].Equal(dates[1]))
}
func validateAfter(ctx *Context) bool {
dates, err := getDates(ctx)
return err == nil && dates[0].After(dates[1])
}
func validateAfterEqual(ctx *Context) bool {
dates, err := getDates(ctx)
return err == nil && (dates[0].After(dates[1]) || dates[0].Equal(dates[1]))
}
func validateDateEquals(ctx *Context) bool {
dates, err := getDates(ctx)
return err == nil && dates[0].Equal(dates[1])
}
func validateDateBetween(ctx *Context) bool {
dates, err := getDates(ctx)
return err == nil && (dates[0].After(dates[1]) || dates[0].Equal(dates[1])) && (dates[0].Before(dates[2]) || dates[0].Equal(dates[2]))
}
func validateDateBeforeNow(ctx *Context) bool {
if date, ok := ctx.Value.(time.Time); ok {
return date.Before(ctx.Now)
}
return false
}
func validateDateAfterNow(ctx *Context) bool {
if date, ok := ctx.Value.(time.Time); ok {
return date.After(ctx.Now)
}
return false
}