Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use mail.ParseAddress to cover missing email validations #1395

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 11 additions & 1 deletion validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8727,6 +8727,16 @@ func TestEmail(t *testing.T) {
NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "", "", "email")

s = "[email protected]."
errs = validate.Var(s, "email")
NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "", "", "email")

s = "[email protected]."
errs = validate.Var(s, "email")
NotEqual(t, errs, nil)
AssertError(t, errs, "", "", "", "", "email")

s = ""
errs = validate.Var(s, "email")
NotEqual(t, errs, nil)
Expand Down Expand Up @@ -12080,7 +12090,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
Loading