forked from cohesivestack/valgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidator_time_p.go
More file actions
281 lines (251 loc) · 9.22 KB
/
Copy pathvalidator_time_p.go
File metadata and controls
281 lines (251 loc) · 9.22 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
package valgo
import (
"time"
)
// ValidatorTimeP is a type that facilitates validation for time pointer variables.
// It retains a context that records details about the validation process.
type ValidatorTimeP struct {
context *ValidatorContext
}
// TimeP initializes a new ValidatorTimeP instance with the provided time pointer
// and optional name and title arguments for detailed error messages.
//
// Usage example:
//
// var myTime *time.Time
// v.TimeP(myTime, "start_time", "Start Time")
func TimeP(value *time.Time, nameAndTitle ...string) *ValidatorTimeP {
return &ValidatorTimeP{context: NewContext(value, nameAndTitle...)}
}
// Context retrieves the context associated with the validator.
func (validator *ValidatorTimeP) Context() *ValidatorContext {
return validator.context
}
// Not negates the result of the next validator function in the chain.
//
// Usage example:
//
// t := time.Now()
// Is(v.TimeP(&t).Not().Zero()).Valid() // Will return false since t is not a zero time.
func (validator *ValidatorTimeP) Not() *ValidatorTimeP {
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.TimeP(&t).Zero().Or().BeforeOrEqualTo(time.Now())).Valid()
func (validator *ValidatorTimeP) Or() *ValidatorTimeP {
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.TimeP(&t).Zero().OrElse().After(t1).Before(t2)).Valid()
func (validator *ValidatorTimeP) OrElse() *ValidatorTimeP {
validator.context.OrElse()
return validator
}
// EqualTo validates that the time pointer is equal to the specified time value.
//
// Usage example:
//
// t1 := time.Now()
// t2 := t1
// Is(v.TimeP(&t1).EqualTo(t2)).Valid() // Will return true.
func (validator *ValidatorTimeP) EqualTo(value time.Time, template ...string) *ValidatorTimeP {
validator.context.AddWithValue(
func() bool {
return validator.context.Value().(*time.Time) != nil && isTimeEqualTo(*(validator.context.Value().(*time.Time)), value)
},
ErrorKeyEqualTo, value, template...)
return validator
}
// After validates that the time pointer is after the specified time value.
//
// Usage example:
//
// t1 := time.Now()
// t2 := t1.Add(-time.Hour)
// Is(v.TimeP(&t1).After(t2)).Valid() // Will return true.
func (validator *ValidatorTimeP) After(value time.Time, template ...string) *ValidatorTimeP {
validator.context.AddWithValue(
func() bool {
return validator.context.Value().(*time.Time) != nil && isTimeAfter(*(validator.context.Value().(*time.Time)), value)
},
ErrorKeyAfter, value, template...)
return validator
}
// AfterOrEqualTo validates that the time pointer is after or equal to the specified time value.
//
// Usage example:
//
// t1 := time.Now()
// t2 := t1
// Is(v.TimeP(&t1).AfterOrEqualTo(t2)).Valid() // Will return true.
func (validator *ValidatorTimeP) AfterOrEqualTo(value time.Time, template ...string) *ValidatorTimeP {
validator.context.AddWithValue(
func() bool {
return validator.context.Value().(*time.Time) != nil && isTimeAfterOrEqualTo(*(validator.context.Value().(*time.Time)), value)
},
ErrorKeyAfterOrEqualTo, value, template...)
return validator
}
// Before validates that the time pointer is before the specified time value.
//
// Usage example:
//
// t1 := time.Now()
// t2 := t1.Add(time.Hour)
// Is(v.TimeP(&t1).Before(t2)).Valid() // Will return true.
func (validator *ValidatorTimeP) Before(value time.Time, template ...string) *ValidatorTimeP {
validator.context.AddWithValue(
func() bool {
return validator.context.Value().(*time.Time) != nil && isTimeBefore(*(validator.context.Value().(*time.Time)), value)
},
ErrorKeyBefore, value, template...)
return validator
}
// BeforeOrEqualTo validates that the time pointer is before or equal to the specified time value.
//
// Usage example:
//
// t1 := time.Now()
// t2 := t1
// Is(v.TimeP(&t1).BeforeOrEqualTo(t2)).Valid() // Will return true.
func (validator *ValidatorTimeP) BeforeOrEqualTo(value time.Time, template ...string) *ValidatorTimeP {
validator.context.AddWithValue(
func() bool {
return validator.context.Value().(*time.Time) != nil && isTimeBeforeOrEqualTo(*(validator.context.Value().(*time.Time)), value)
},
ErrorKeyBeforeOrEqualTo, value, template...)
return validator
}
// Between validates that the time pointer is between the specified minimum and maximum time values (inclusive).
//
// Usage example:
//
// t1 := time.Now()
// min := t1.Add(-time.Hour)
// max := t1.Add(time.Hour)
// Is(v.TimeP(&t1).Between(min, max)).Valid() // Will return true.
func (validator *ValidatorTimeP) Between(min time.Time, max time.Time, template ...string) *ValidatorTimeP {
validator.context.AddWithParams(
func() bool {
return validator.context.Value().(*time.Time) != nil && isTimeBetween(*(validator.context.Value().(*time.Time)), min, max)
},
ErrorKeyBetween,
map[string]any{"title": validator.context.title, "min": min, "max": max},
template...)
return validator
}
// Zero validates that the time pointer is pointing to a zero time value.
//
// Usage example:
//
// var t *time.Time
// Is(v.TimeP(t).Zero()).Valid() // Will return true as t is nil and thus pointing to a zero time.
func (validator *ValidatorTimeP) Zero(template ...string) *ValidatorTimeP {
validator.context.AddWithValue(
func() bool {
return validator.context.Value().(*time.Time) != nil && isTimeZero(*(validator.context.Value().(*time.Time)))
},
ErrorKeyZero, validator.context.Value(), template...)
return validator
}
// Passing allows for custom validation function to be applied on the time pointer.
//
// Usage example:
//
// t := time.Now()
// Is(v.TimeP(&t).Passing(func(v0 *time.Time) bool { return v0.After(time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)) })).Valid() // Custom validation.
func (validator *ValidatorTimeP) Passing(function func(v0 *time.Time) bool, template ...string) *ValidatorTimeP {
validator.context.AddWithValue(
func() bool {
return function(validator.context.Value().(*time.Time))
},
ErrorKeyPassing, validator.context.Value(), template...)
return validator
}
// InSlice validates that the time pointer is pointing to a time value present in the specified slice.
//
// Usage example:
//
// t := time.Now()
// validTimes := []time.Time{t, time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)}
// Is(v.TimeP(&t).InSlice(validTimes)).Valid() // Will return true.
func (validator *ValidatorTimeP) InSlice(slice []time.Time, template ...string) *ValidatorTimeP {
validator.context.AddWithValue(
func() bool {
return validator.context.Value().(*time.Time) != nil && isTimeInSlice(*(validator.context.Value().(*time.Time)), slice)
},
ErrorKeyInSlice, validator.context.Value(), template...)
return validator
}
// Nil validates that the time pointer is nil.
//
// Usage example:
//
// var t *time.Time
// Is(v.TimeP(t).Nil()).Valid() // Will return true as t is nil.
func (validator *ValidatorTimeP) Nil(template ...string) *ValidatorTimeP {
validator.context.AddWithValue(
func() bool {
return validator.context.Value().(*time.Time) == nil
},
ErrorKeyNil, validator.context.Value(), template...)
return validator
}
// NilOrZero validates that the time pointer is either nil or pointing to a zero time value.
//
// Usage example:
//
// var t *time.Time
// Is(v.TimeP(t).NilOrZero()).Valid() // Will return true as t is nil.
func (validator *ValidatorTimeP) NilOrZero(template ...string) *ValidatorTimeP {
validator.context.AddWithValue(
func() bool {
return validator.context.Value().(*time.Time) == nil || isTimeZero(*(validator.context.Value().(*time.Time)))
},
ErrorKeyNil, validator.context.Value(), template...)
return validator
}