forked from cohesivestack/valgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidator_time.go
More file actions
302 lines (269 loc) · 9.55 KB
/
Copy pathvalidator_time.go
File metadata and controls
302 lines (269 loc) · 9.55 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package valgo
import (
"time"
)
func isTimeEqualTo(v0 time.Time, v1 time.Time) bool {
return v0.Equal(v1)
}
func isTimeAfter(v0 time.Time, v1 time.Time) bool {
return v0.After(v1)
}
func isTimeAfterOrEqualTo(v0 time.Time, v1 time.Time) bool {
return v0.After(v1) || v0.Equal(v1)
}
func isTimeBefore(v0 time.Time, v1 time.Time) bool {
return v0.Before(v1)
}
func isTimeBeforeOrEqualTo(v0 time.Time, v1 time.Time) bool {
return v0.Before(v1) || v0.Equal(v1)
}
func isTimeZero(v time.Time) bool {
return v.IsZero()
}
func isTimeBetween(v time.Time, min time.Time, max time.Time) bool {
return (v.After(min) || v.Equal(min)) && (v.Before(max) || v.Equal(max))
}
func isTimeInSlice(v time.Time, slice []time.Time) bool {
for _, _v := range slice {
if v.Equal(_v) {
return true
}
}
return false
}
// The `ValidatorTime` structure provides a set of methods to perform validation
// checks on time.Time values, utilizing Go's native time package.
type ValidatorTime struct {
context *ValidatorContext
}
// The Time function initiates a new `ValidatorTime` instance to validate a given
// time value. The optional name and title parameters can be used for enhanced
// error reporting. If a name is provided without a title, the name is humanized
// to be used as the title.
//
// For example:
//
// startTime := time.Now()
// v := ValidatorTime{}
// v.Time(startTime, "start_time", "Start Time")
func Time(value time.Time, nameAndTitle ...string) *ValidatorTime {
return &ValidatorTime{context: NewContext(value, nameAndTitle...)}
}
// The Context method returns the current context of the validator, which can
// be utilized to create custom validations by extending this validator.
func (validator *ValidatorTime) Context() *ValidatorContext {
return validator.context
}
// The Not method inverts the boolean value associated with the next validator
// method. This can be used to negate the check performed by the next validation
// method in the chain.
//
// For example:
//
// // Will return false because Not() inverts the boolean value of the Zero() function
// startTime := time.Now()
// Is(v.Time(startTime).Not().Zero()).Valid()
func (validator *ValidatorTime) Not() *ValidatorTime {
validator.context.Not()
return validator
}
// Or introduces a logical OR boundary in the current validator chain.
//
// Or groups adjacent validation fragments into a single OR-group that is
// evaluated left-to-right until one fragment succeeds. The OR-group succeeds
// if any fragment succeeds; it fails only if all fragments fail.
//
// Precedence: the OR-group is evaluated as a unit before the implicit AND
// that continues the chain. For example:
//
// A.Or().B.C == (A OR B) AND C
//
// Error reporting: if the OR-group fails, the error message for that group is
// a single message composed by joining the failing fragments' messages using
// the localized OR list format.
//
// Example:
//
// // Passes because time is Zero (Zero() OR BeforeOrEqualTo(time.Now())).
// t := time.Time{}
// isValid := v.Is(v.Time(t).Zero().Or().BeforeOrEqualTo(time.Now())).Valid()
func (validator *ValidatorTime) Or() *ValidatorTime {
validator.context.Or()
return validator
}
// OrElse introduces a logical OR boundary with a cut (short-circuit) in the
// validator chain.
//
// OrElse behaves like Or for building an OR-group, but with an additional rule:
// if the left side (a single fragment, or the entire OR-group accumulated to
// the left of OrElse) succeeds, validation stops and no fragments to the right
// of OrElse are evaluated.
//
// This is primarily used to express "accept X, otherwise validate the rest"
// without repeating X across multiple OR fragments.
//
// Precedence: OrElse still participates in OR-grouping precedence. For example:
//
// A.OrElse().B.C == A OR (B AND C) (with a cut if A succeeds)
//
// Error reporting: if the OR-group fails, its message is composed the same way
// as Or (localized OR list join).
//
// Example:
//
// // If time is Zero, the chain succeeds and After/Before are not evaluated.
// // Otherwise, time must be After(t1) AND Before(t2).
// t := time.Time{}
// t1 := time.Now().Add(-time.Hour)
// t2 := time.Now().Add(time.Hour)
// isValid := v.Is(v.Time(t).Zero().OrElse().After(t1).Before(t2)).Valid()
func (validator *ValidatorTime) OrElse() *ValidatorTime {
validator.context.OrElse()
return validator
}
// The EqualTo method validates if the time value is equal to another given time
// value. It uses the equality (`==`) operator from Go for the comparison.
//
// For example:
//
// timeA := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)
// timeB := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)
// Is(v.Time(timeA).EqualTo(timeB)).Valid()
func (validator *ValidatorTime) EqualTo(value time.Time, template ...string) *ValidatorTime {
validator.context.AddWithValue(
func() bool {
return isTimeEqualTo(validator.context.Value().(time.Time), value)
},
ErrorKeyEqualTo, value, template...)
return validator
}
// The After method checks if the time value is after a specified time.
//
// For example:
//
// startTime := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)
// endTime := time.Date(2023, 1, 1, 1, 0, 0, 0, time.UTC)
// Is(v.Time(endTime).After(startTime)).Valid()
func (validator *ValidatorTime) After(value time.Time, template ...string) *ValidatorTime {
validator.context.AddWithValue(
func() bool {
return isTimeAfter(validator.context.Value().(time.Time), value)
},
ErrorKeyAfter, value, template...)
return validator
}
// The AfterOrEqualTo method checks if the time value is either after or equal to
// a specified time.
//
// For example:
//
// timeA := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)
// timeB := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)
// Is(v.Time(timeA).AfterOrEqualTo(timeB)).Valid()
func (validator *ValidatorTime) AfterOrEqualTo(value time.Time, template ...string) *ValidatorTime {
validator.context.AddWithValue(
func() bool {
return isTimeAfterOrEqualTo(validator.context.Value().(time.Time), value)
},
ErrorKeyAfterOrEqualTo, value, template...)
return validator
}
// The Before method checks if the time value is before a specified time.
//
// For example:
//
// startTime := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)
// endTime := time.Date(2023, 1, 1, 1, 0, 0, 0, time.UTC)
// Is(v.Time(startTime).Before(endTime)).Valid()
func (validator *ValidatorTime) Before(value time.Time, template ...string) *ValidatorTime {
validator.context.AddWithValue(
func() bool {
return isTimeBefore(validator.context.Value().(time.Time), value)
},
ErrorKeyBefore, value, template...)
return validator
}
// The BeforeOrEqualTo method checks if the time value is either before or equal to
// a specified time.
//
// For example:
//
// timeA := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)
// timeB := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)
// Is(v.Time(timeA).BeforeOrEqualTo(timeB)).Valid()
func (validator *ValidatorTime) BeforeOrEqualTo(value time.Time, template ...string) *ValidatorTime {
validator.context.AddWithValue(
func() bool {
return isTimeBeforeOrEqualTo(validator.context.Value().(time.Time), value)
},
ErrorKeyBeforeOrEqualTo, value, template...)
return validator
}
// The Between method verifies if the time value falls within a given time range, inclusive.
//
// For example:
//
// minTime := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)
// maxTime := time.Date(2023, 1, 1, 12, 0, 0, 0, time.UTC)
// checkTime := time.Date(2023, 1, 1, 6, 0, 0, 0, time.UTC)
// Is(v.Time(checkTime).Between(minTime, maxTime)).Valid()
func (validator *ValidatorTime) Between(min time.Time, max time.Time, template ...string) *ValidatorTime {
validator.context.AddWithParams(
func() bool {
return isTimeBetween(validator.context.Value().(time.Time), min, max)
},
ErrorKeyBetween,
map[string]any{"title": validator.context.title, "min": min, "max": max},
template...)
return validator
}
// The Zero method verifies if the time value is a zero time, which means it hasn't
// been initialized yet.
//
// For example:
//
// zeroTime := time.Time{}
// Is(v.Time(zeroTime).Zero()).Valid()
func (validator *ValidatorTime) Zero(template ...string) *ValidatorTime {
validator.context.AddWithValue(
func() bool {
return isTimeZero(validator.context.Value().(time.Time))
},
ErrorKeyZero, validator.context.Value(), template...)
return validator
}
// The Passing method allows for custom validation logic by accepting a function
// that returns a boolean indicating whether the validation passed or failed.
//
// For example:
//
// checkTime := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)
// Is(v.Time(checkTime).Passing(func(t time.Time) bool {
// return t.Year() == 2023
// })).Valid()
func (validator *ValidatorTime) Passing(function func(v0 time.Time) bool, template ...string) *ValidatorTime {
validator.context.AddWithValue(
func() bool {
return function(validator.context.Value().(time.Time))
},
ErrorKeyPassing, validator.context.Value(), template...)
return validator
}
// The InSlice method validates if the time value is found within a provided slice
// of time values.
//
// For example:
//
// timeA := time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)
// timeB := time.Date(2023, 1, 1, 1, 0, 0, 0, time.UTC)
// timeSlice := []time.Time{timeA, timeB}
// checkTime := time.Date(2023, 1, 1, 1, 0, 0, 0, time.UTC)
// Is(v.Time(checkTime).InSlice(timeSlice)).Valid()
func (validator *ValidatorTime) InSlice(slice []time.Time, template ...string) *ValidatorTime {
validator.context.AddWithValue(
func() bool {
return isTimeInSlice(validator.context.Value().(time.Time), slice)
},
ErrorKeyInSlice, validator.context.Value(), template...)
return validator
}