Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions baked_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"io/fs"
"net"
"net/mail"
"net/url"
"os"
"reflect"
Expand Down Expand Up @@ -1692,6 +1693,10 @@ func isE164(fl FieldLevel) bool {

// isEmail is the validation function for validating if the current field's value is a valid email address.
func isEmail(fl FieldLevel) bool {
_, err := mail.ParseAddress(fl.Field().String())
if err != nil {
return false
}
return emailRegex().MatchString(fl.Field().String())
}

Expand Down
16 changes: 15 additions & 1 deletion validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8727,6 +8727,20 @@ func TestEmail(t *testing.T) {
NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "", "", "email")

s = "mail@dotaftercom.com."
errs = validate.Var(s, "email")
NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "", "", "email")

s = "mail@dotaftercom.co.uk."
errs = validate.Var(s, "email")
NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "", "", "email")

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's include one more test case.

	s = "Foo Bar <foobar@example.com>"
	errs = validate.Var(s, "email")
	NotEqual(t, errs, nil)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, added this test as well

s = "Foo Bar <foobar@example.com>"
errs = validate.Var(s, "email")
NotEqual(t, errs, nil)

s = ""
errs = validate.Var(s, "email")
NotEqual(t, errs, nil)
Expand Down Expand Up @@ -12080,7 +12094,7 @@ func TestExcludedIf(t *testing.T) {

test11 := struct {
Field1 bool
Field2 *string `validate:"excluded_if=Field1 false"`
Field2 *string `validate:"excluded_if=Field1 false"`
}{
Field1: false,
Field2: nil,
Expand Down