Skip to content

Commit

Permalink
fix: email address validator
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Apr 24, 2021
1 parent 4e9c520 commit f7ed3a6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pkg/utils/emailvalidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var emailRegex = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z

// ValidateEmail validates email.
func ValidateEmail(e string) bool {
if len(e) < 3 && len(e) > 254 {
if len(e) < 3 || len(e) > 254 {
return false
}
return emailRegex.MatchString(e)
Expand Down
32 changes: 20 additions & 12 deletions pkg/utils/emailvalidator_test.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
package utils
package utils_test

import (
"testing"

"moul.io/sshportal/pkg/utils"
)

func TestValidateEmail(t *testing.T) {

goodEmail := "goodemail@email.com"
badEmail := "b@2323.22"

got := ValidateEmail(goodEmail)
if got == false {
t.Errorf("got1= %v; want true", got)
tests := []struct {
input string
expected bool
}{
{"goodemail@email.com", true},
{"b@2323.22", true},
{"b@2322.", false},
{"", false},
{"blah", false},
{"blah.com", false},
}

got2 := ValidateEmail(badEmail)
if got2 == false {
t.Errorf("got2= %v; want false", got2)
for _, test := range tests {
t.Run(test.input, func(t *testing.T) {
got := utils.ValidateEmail(test.input)
if got != test.expected {
t.Errorf("expected %v, got %v", test.expected, got)
}
})
}

}

0 comments on commit f7ed3a6

Please sign in to comment.