11package validator
22
33import (
4+ "fmt"
45 goreflect "github.com/ralvarezdev/go-reflect"
6+ gostringscount "github.com/ralvarezdev/go-strings/count"
57 govalidatorfieldbirthdate "github.com/ralvarezdev/go-validator/struct/field/birthdate"
68 govalidatorfieldmail "github.com/ralvarezdev/go-validator/struct/field/mail"
9+ govalidatorfieldpassword "github.com/ralvarezdev/go-validator/struct/field/password"
710 govalidatormapper "github.com/ralvarezdev/go-validator/struct/mapper"
811 govalidatormapperparser "github.com/ralvarezdev/go-validator/struct/mapper/parser"
912 govalidatormappervalidation "github.com/ralvarezdev/go-validator/struct/mapper/validation"
@@ -29,7 +32,14 @@ type (
2932 )
3033 Birthdate (
3134 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 ,
3343 validations * govalidatormappervalidation.StructValidations ,
3444 )
3545 CreateValidateFn (
@@ -47,6 +57,20 @@ type (
4757 parser govalidatormapperparser.Parser
4858 validator Validator
4959 }
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+ }
5074)
5175
5276// NewDefaultService creates a new default validator service
@@ -113,15 +137,106 @@ func (d *DefaultService) Email(
113137// Birthdate validates the birthdate field
114138func (d * DefaultService ) Birthdate (
115139 birthdateField string ,
116- birthdate * time.Time ,
140+ birthdate time.Time ,
141+ options * BirthdateOptions ,
117142 validations * govalidatormappervalidation.StructValidations ,
118143) {
119- if birthdate == nil || birthdate .After (time .Now ()) {
144+ // Check if the birthdate is after the current time
145+ if birthdate .After (time .Now ()) {
120146 validations .AddFieldValidationError (
121147 birthdateField ,
122148 govalidatorfieldbirthdate .ErrInvalidBirthdate ,
123149 )
124150 }
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+ }
125240}
126241
127242// CreateValidateFn creates a validate function for the request body using the validator
0 commit comments