From 35e9aba7cf3600f614e5fa29698df52f44f1c9db Mon Sep 17 00:00:00 2001 From: Surfoo Date: Fri, 4 Dec 2020 18:08:57 +0100 Subject: [PATCH] Fix email validation mail.ParseAddress return an reformated email if the format is not really an email (with a space after of before for example). A check was added to compare the input and the output from mail.ParseAddress. --- keywords_optional.go | 9 +++++---- testdata/draft2019-09/optional/format/email.json | 5 +++++ testdata/draft3/optional/format.json | 5 +++++ testdata/draft4/optional/format.json | 5 +++++ testdata/draft6/optional/format.json | 5 +++++ testdata/draft7/optional/format/email.json | 5 +++++ 6 files changed, 30 insertions(+), 4 deletions(-) diff --git a/keywords_optional.go b/keywords_optional.go index 76ba5d0..852ddfa 100644 --- a/keywords_optional.go +++ b/keywords_optional.go @@ -123,12 +123,13 @@ func isValidDate(date string) error { // representation as defined by RFC 5322, section 3.4.1 [RFC5322]. // https://tools.ietf.org/html/rfc5322#section-3.4.1 func isValidEmail(email string) error { - // if !emailPattern.MatchString(email) { - // return fmt.Errorf("invalid email Format") - // } - if _, err := mail.ParseAddress(email); err != nil { + res, err := mail.ParseAddress(email) + if err != nil { return fmt.Errorf("email address incorrectly Formatted: %s", err.Error()) } + if res.Address != email { + return fmt.Errorf("email address incorrectly Formatted: %s", email) + } return nil } diff --git a/testdata/draft2019-09/optional/format/email.json b/testdata/draft2019-09/optional/format/email.json index c837c84..910323c 100644 --- a/testdata/draft2019-09/optional/format/email.json +++ b/testdata/draft2019-09/optional/format/email.json @@ -12,6 +12,11 @@ "description": "an invalid e-mail address", "data": "2962", "valid": false + }, + { + "description": "an invalid e-mail address", + "data": " joe.bloggs@example.com", + "valid": false } ] } diff --git a/testdata/draft3/optional/format.json b/testdata/draft3/optional/format.json index 9864589..3cfd0e4 100644 --- a/testdata/draft3/optional/format.json +++ b/testdata/draft3/optional/format.json @@ -112,6 +112,11 @@ "description": "an invalid e-mail address", "data": "2962", "valid": false + }, + { + "description": "an invalid e-mail address", + "data": " joe.bloggs@example.com", + "valid": false } ] }, diff --git a/testdata/draft4/optional/format.json b/testdata/draft4/optional/format.json index 4bf4ea8..2c29c84 100644 --- a/testdata/draft4/optional/format.json +++ b/testdata/draft4/optional/format.json @@ -164,6 +164,11 @@ "description": "an invalid e-mail address", "data": "2962", "valid": false + }, + { + "description": "an invalid e-mail address", + "data": " joe.bloggs@example.com", + "valid": false } ] }, diff --git a/testdata/draft6/optional/format.json b/testdata/draft6/optional/format.json index 3dd265f..2672ed7 100644 --- a/testdata/draft6/optional/format.json +++ b/testdata/draft6/optional/format.json @@ -236,6 +236,11 @@ "description": "an invalid e-mail address", "data": "2962", "valid": false + }, + { + "description": "an invalid e-mail address", + "data": " joe.bloggs@example.com", + "valid": false } ] }, diff --git a/testdata/draft7/optional/format/email.json b/testdata/draft7/optional/format/email.json index c837c84..910323c 100644 --- a/testdata/draft7/optional/format/email.json +++ b/testdata/draft7/optional/format/email.json @@ -12,6 +12,11 @@ "description": "an invalid e-mail address", "data": "2962", "valid": false + }, + { + "description": "an invalid e-mail address", + "data": " joe.bloggs@example.com", + "valid": false } ] }