1
1
package validator
2
2
3
3
import (
4
+ "fmt"
4
5
goreflect "github.com/ralvarezdev/go-reflect"
6
+ gostringscount "github.com/ralvarezdev/go-strings/count"
5
7
govalidatorfieldbirthdate "github.com/ralvarezdev/go-validator/struct/field/birthdate"
6
8
govalidatorfieldmail "github.com/ralvarezdev/go-validator/struct/field/mail"
9
+ govalidatorfieldpassword "github.com/ralvarezdev/go-validator/struct/field/password"
7
10
govalidatormapper "github.com/ralvarezdev/go-validator/struct/mapper"
8
11
govalidatormapperparser "github.com/ralvarezdev/go-validator/struct/mapper/parser"
9
12
govalidatormappervalidation "github.com/ralvarezdev/go-validator/struct/mapper/validation"
@@ -29,7 +32,14 @@ type (
29
32
)
30
33
Birthdate (
31
34
birthdateField string ,
32
- birthdate * time.Time ,
35
+ birthdate time.Time ,
36
+ options * BirthdateOptions ,
37
+ validations * govalidatormappervalidation.StructValidations ,
38
+ )
39
+ Password (
40
+ passwordField string ,
41
+ password string ,
42
+ options * PasswordOptions ,
33
43
validations * govalidatormappervalidation.StructValidations ,
34
44
)
35
45
CreateValidateFn (
@@ -47,6 +57,20 @@ type (
47
57
parser govalidatormapperparser.Parser
48
58
validator Validator
49
59
}
60
+
61
+ // BirthdateOptions is the birthdate options struct
62
+ BirthdateOptions struct {
63
+ MinimumAge int
64
+ MaximumAge int
65
+ }
66
+
67
+ // PasswordOptions is the password options struct
68
+ PasswordOptions struct {
69
+ MinimumLength int
70
+ MinimumSpecialCount int
71
+ MinimumNumbersCount int
72
+ MinimumCapsCount int
73
+ }
50
74
)
51
75
52
76
// NewDefaultService creates a new default validator service
@@ -113,15 +137,106 @@ func (d *DefaultService) Email(
113
137
// Birthdate validates the birthdate field
114
138
func (d * DefaultService ) Birthdate (
115
139
birthdateField string ,
116
- birthdate * time.Time ,
140
+ birthdate time.Time ,
141
+ options * BirthdateOptions ,
117
142
validations * govalidatormappervalidation.StructValidations ,
118
143
) {
119
- if birthdate == nil || birthdate .After (time .Now ()) {
144
+ // Check if the birthdate is after the current time
145
+ if birthdate .After (time .Now ()) {
120
146
validations .AddFieldValidationError (
121
147
birthdateField ,
122
148
govalidatorfieldbirthdate .ErrInvalidBirthdate ,
123
149
)
124
150
}
151
+
152
+ // Check if the birthdate options are nil
153
+ if options == nil {
154
+ return
155
+ }
156
+
157
+ // Check if the birthdate is before the minimum age
158
+ if options .MinimumAge > 0 {
159
+ if time .Now ().AddDate (- options .MinimumAge , 0 , 0 ).Before (birthdate ) {
160
+ validations .AddFieldValidationError (
161
+ birthdateField ,
162
+ fmt .Errorf (
163
+ govalidatorfieldbirthdate .ErrMinimumAge ,
164
+ options .MinimumAge ,
165
+ ),
166
+ )
167
+ }
168
+ }
169
+
170
+ // Check if the birthdate is after the maximum age
171
+ if options .MaximumAge > 0 {
172
+ if time .Now ().AddDate (- options .MaximumAge , 0 , 0 ).After (birthdate ) {
173
+ validations .AddFieldValidationError (
174
+ birthdateField ,
175
+ fmt .Errorf (
176
+ govalidatorfieldbirthdate .ErrMaximumAge ,
177
+ options .MaximumAge ,
178
+ ),
179
+ )
180
+ }
181
+ }
182
+ }
183
+
184
+ // Password validates the password field
185
+ func (d * DefaultService ) Password (
186
+ passwordField string ,
187
+ password string ,
188
+ options * PasswordOptions ,
189
+ validations * govalidatormappervalidation.StructValidations ,
190
+ ) {
191
+ // Check if the password length is less than the minimum length
192
+ if options .MinimumLength > 0 && len (password ) < options .MinimumLength {
193
+ validations .AddFieldValidationError (
194
+ passwordField ,
195
+ fmt .Errorf (
196
+ govalidatorfieldpassword .ErrMinimumLength ,
197
+ options .MinimumLength ,
198
+ ),
199
+ )
200
+ }
201
+
202
+ // Check if the password contains the minimum special characters
203
+ if options .MinimumSpecialCount > 0 {
204
+ if count := gostringscount .Special (password ); count < options .MinimumSpecialCount {
205
+ validations .AddFieldValidationError (
206
+ passwordField ,
207
+ fmt .Errorf (
208
+ govalidatorfieldpassword .ErrMinimumSpecialCount ,
209
+ options .MinimumSpecialCount ,
210
+ ),
211
+ )
212
+ }
213
+ }
214
+
215
+ // Check if the password contains the minimum numbers
216
+ if options .MinimumNumbersCount > 0 {
217
+ if count := gostringscount .Numbers (password ); count < options .MinimumNumbersCount {
218
+ validations .AddFieldValidationError (
219
+ passwordField ,
220
+ fmt .Errorf (
221
+ govalidatorfieldpassword .ErrMinimumNumbersCount ,
222
+ options .MinimumNumbersCount ,
223
+ ),
224
+ )
225
+ }
226
+ }
227
+
228
+ // Check if the password contains the minimum caps
229
+ if options .MinimumCapsCount > 0 {
230
+ if count := gostringscount .Caps (password ); count < options .MinimumCapsCount {
231
+ validations .AddFieldValidationError (
232
+ passwordField ,
233
+ fmt .Errorf (
234
+ govalidatorfieldpassword .ErrMinimumCapsCount ,
235
+ options .MinimumCapsCount ,
236
+ ),
237
+ )
238
+ }
239
+ }
125
240
}
126
241
127
242
// CreateValidateFn creates a validate function for the request body using the validator
0 commit comments