-
Notifications
You must be signed in to change notification settings - Fork 3
/
time.go
197 lines (157 loc) · 3.13 KB
/
time.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package validator
import (
"context"
"time"
"github.com/raoptimus/validator.go/v2/vtype"
)
type TimeFunc func() time.Time
type Time struct {
message string
formatMessage string
tooBigMessage string
tooSmallMessage string
format string
min TimeFunc
max TimeFunc
whenFunc WhenFunc
skipEmpty bool
skipError bool
}
func NewTime() *Time {
return &Time{
message: "Value is invalid",
formatMessage: "Format of the time value must be equal {format}",
tooBigMessage: "Time must be no greater than {max}.",
tooSmallMessage: "Time must be no less than {min}.",
format: time.RFC3339,
min: nil,
max: nil,
}
}
func (r *Time) WithMessage(message string) *Time {
rc := *r
rc.message = message
return &rc
}
func (r *Time) WithFormatMessage(message string) *Time {
rc := *r
rc.formatMessage = message
return &rc
}
func (r *Time) WithTooSmallMessage(message string) *Time {
rc := *r
rc.tooSmallMessage = message
return &rc
}
func (r *Time) WithTooBigMessage(message string) *Time {
rc := *r
rc.tooBigMessage = message
return &rc
}
func (r *Time) WithFormat(format string) *Time {
rc := *r
rc.format = format
return &rc
}
func (r *Time) WithMin(min TimeFunc) *Time {
rc := *r
rc.min = min
return &rc
}
func (r *Time) WithMax(max TimeFunc) *Time {
rc := *r
rc.max = max
return &rc
}
func (r *Time) When(v WhenFunc) *Time {
rc := *r
rc.whenFunc = v
return &rc
}
func (r *Time) when() WhenFunc {
return r.whenFunc
}
func (r *Time) setWhen(v WhenFunc) {
r.whenFunc = v
}
func (r *Time) SkipOnEmpty() *Time {
rc := *r
rc.skipEmpty = true
return &rc
}
func (r *Time) skipOnEmpty() bool {
return r.skipEmpty
}
func (r *Time) setSkipOnEmpty(v bool) {
r.skipEmpty = v
}
func (r *Time) SkipOnError() *Time {
rs := *r
rs.skipError = true
return &rs
}
func (r *Time) shouldSkipOnError() bool {
return r.skipError
}
func (r *Time) setSkipOnError(v bool) {
r.skipError = v
}
func (r *Time) ValidateValue(_ context.Context, value any) error {
v, valid := indirectValue(value)
if !valid {
return NewResult().WithError(NewValidationError(r.message))
}
vStr, okStr := toString(value)
vObj, okObj := v.(vtype.Time)
if !okStr && !okObj {
return NewResult().WithError(NewValidationError(r.message))
}
if okObj {
vStr = vObj.String()
}
vt, err := time.Parse(r.format, vStr)
if err != nil {
return NewResult().WithError(
NewValidationError(r.formatMessage).
WithParams(
map[string]any{
"format": r.format,
},
),
)
}
result := NewResult()
if r.min != nil {
minTime := r.min()
if vt.Before(minTime) {
result = result.WithError(
NewValidationError(r.tooSmallMessage).
WithParams(
map[string]any{
"min": minTime,
},
),
)
}
}
if r.max != nil {
maxTime := r.max()
if vt.After(maxTime) {
result = result.WithError(
NewValidationError(r.tooBigMessage).
WithParams(
map[string]any{
"max": maxTime,
},
),
)
}
}
if result.IsValid() {
if okObj {
*vObj.Time() = vt
}
return nil
}
return result
}