Skip to content

Commit dad363f

Browse files
Yury MiadzeletsInsei
Yury Miadzelets
authored andcommitted
added email validation for strings
1 parent 5f439ff commit dad363f

File tree

6 files changed

+39
-0
lines changed

6 files changed

+39
-0
lines changed

examples/translation/main.go

+6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ type Sender struct {
1818
SMTPPort string
1919
HTTPAddress string
2020
HTTPDestParam string
21+
Email string
22+
Emails []string
2123
Description *string
2224
Int int
2325
Id uuid.UUID
@@ -50,6 +52,8 @@ func main() {
5052
builder.String(&obj.SMTPHost).Trim().
5153
Regexp(regexp.MustCompile("^[a-zA-Z0-9.]+$"), str.WithRegexpLocaleKey(customRegexpLocaleKey))
5254
builder.UUID(&obj.Id).Required()
55+
builder.String(&obj.Email).Email()
56+
builder.StringSlice(&obj.Emails).Email()
5357
builder.String(&obj.Description).Trim()
5458
builder.StringSlice(&obj.Templates).Trim().
5559
Regexp(regexp.MustCompile("^[a-zA-Z0-9.]+$"), str.WithRegexpLocaleKey(customRegexpLocaleKey))
@@ -67,6 +71,8 @@ func main() {
6771
SMTPPort: uuid.New().String() + " ",
6872
HTTPAddress: uuid.New().String() + " ",
6973
HTTPDestParam: uuid.New().String() + " ",
74+
Email: "correct@email.com",
75+
Emails: []string{"correct@email.com", "incorrect.email.com"},
7076
Id: id,
7177
Int: 2,
7278
}

str/base.go

+12
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ const (
1616
requiredLocaleKey = "validation:string:Should be fulfilled"
1717
regexpLocaleKey = "validation:string:Doesn't match required regexp pattern"
1818
anyOfLocaleKey = "validation:string:Only %s values is allowed"
19+
emailLocaleKey = "validation:string:Should be email address"
20+
21+
emailRegexp = `^[^\s,@#$%^&*!()]+@([a-zA-Z0-9]+[.])+[a-zA-Z]{2,8}$`
1922
)
2023

2124
type baseConfigurator[T strPtr] struct {
@@ -93,6 +96,15 @@ func (i *baseConfigurator[T]) AnyOf(allowed ...string) BaseConfigurator {
9396
return i
9497
}
9598

99+
// Email checks is the string value is email.
100+
func (i *baseConfigurator[T]) Email() BaseConfigurator {
101+
i.c.Append(func(v T) bool {
102+
r := regexp.MustCompile(emailRegexp)
103+
return r.MatchString(*v)
104+
}, emailLocaleKey)
105+
return i
106+
}
107+
96108
// When allows for conditional validation logic to be applied to the string value.
97109
func (i *baseConfigurator[T]) When(whenFn func(ctx context.Context, value any) bool) BaseConfigurator {
98110
if whenFn == nil {

str/slice.go

+16
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,19 @@ func (s *StringSliceFieldConfigurator) Regexp(regexp *regexp.Regexp, opts ...Reg
5252
})
5353
return s
5454
}
55+
56+
func (s *StringSliceFieldConfigurator) Email() *StringSliceFieldConfigurator {
57+
s.Custom(func(ctx context.Context, h *shared.FieldCustomHelper, v []*any) []shared.Error {
58+
values := shared.UnsafeValigoSliceCast[string](v)
59+
var errs []shared.Error
60+
r := regexp.MustCompile(emailRegexp)
61+
for _, val := range values {
62+
if !r.MatchString(*val) {
63+
errs = append(errs, h.ErrorT(ctx, *val, emailLocaleKey))
64+
}
65+
}
66+
67+
return errs
68+
})
69+
return s
70+
}

str/types.go

+3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ type BaseConfigurator interface {
2828
// MinLen checks if the string length is not less than the given minimum length.
2929
MinLen(int) BaseConfigurator
3030

31+
// Email checks is the string is email address
32+
Email() BaseConfigurator
33+
3134
// When allows for conditional validation based on a given condition.
3235
When(whenFn func(ctx context.Context, value any) bool) BaseConfigurator
3336
}

translator/locales/en/data.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ validation:
66
"Cannot be shorter than %d characters": Cannot be shorter than %d characters
77
"Doesn't match required regexp pattern": Doesn't match required regexp pattern
88
"Only %s values is allowed": Only %s values is allowed
9+
"Should be email address": Should be email address
910
num:
1011
"Cannot be less than %v": Cannot be less than %v
1112
"Cannot be greater than %v": Cannot be greater than %v

translator/locales/ru/data.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ validation:
66
"Cannot be shorter than %d characters": Не может быть короче %d символов
77
"Doesn't match required regexp pattern": Не соответствует regexp шаблону
88
"Only %s values is allowed": Только %s значения разрешены
9+
"Should be email address": Должно быть электронным адресом
910
num:
1011
"Cannot be less than %v": Не может быть меньше %v
1112
"Cannot be greater than %v": Не может быть больше %v

0 commit comments

Comments
 (0)