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
261 lines (229 loc) · 8.14 KB
/
validator_time.go
File metadata and controls
261 lines (229 loc) · 8.14 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
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
}
// Introduces a logical OR in the chain of validation conditions, affecting the
// evaluation order and priority of subsequent validators. A value passes the
// validation if it meets any one condition following the Or() call, adhering to
// a left-to-right evaluation. This mechanism allows for validating against
// multiple criteria where satisfying any single criterion is sufficient.
// Example:
//
// // This validator will pass because the time is before or equal to time.Now().
// t := time.Now()
// isValid := v.Is(v.Time(t).Zero().Or().BeforeOrEqualTo(time.Now())).Valid()
func (validator *ValidatorTime) Or() *ValidatorTime {
validator.context.Or()
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
}