Skip to content

Commit de27754

Browse files
authored
Merge pull request #39 from privacybydesign/case-sensitive
Make email validation case insensitive
2 parents 5ba7d8d + 38db19a commit de27754

2 files changed

Lines changed: 19 additions & 19 deletions

File tree

backend/internal/validators/email_validator.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import (
88
type EmailValidator struct{}
99

1010
// ParseAndValidateEmailAddress checks whether the provided input is valid.
11-
// It accepts RFC-5322 formatted emailaddresses (i.e. "John Doe <john.doe@example.com>"), but only in lowercase.
11+
// It accepts RFC-5322 formatted emailaddresses (i.e. "John Doe <john.doe@example.com>").
12+
// Any uppercase characters in the address are normalized to lowercase.
1213
// It returns a boolean indicating validity, a parsed emailaddress (if valid) or an error message key (if invalid).
1314
// If valid, the second return value contains the parsed address from the RFC-5322 format (e.g. "john.doe@example.com").
1415
// If invalid, an error message is returned in the third return value.
@@ -29,11 +30,7 @@ func (v *EmailValidator) ParseAndValidateEmailAddress(email string) (bool, *stri
2930
err := "error_email_format"
3031
return false, nil, &err
3132
} else {
32-
// We only accept lowercase emailaddresses
33-
if strings.ToLower(addr.Address) != addr.Address {
34-
err := "error_email_format_lowercase"
35-
return false, nil, &err
36-
}
37-
return true, &addr.Address, nil
33+
normalized := strings.ToLower(addr.Address)
34+
return true, &normalized, nil
3835
}
3936
}

backend/internal/validators/email_validator_test.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,23 +62,26 @@ func Test_ParseAndValidateEmailAddress_Given_EmailAddressesWithQuotesOrIpDomains
6262
}
6363
}
6464

65-
func Test_ParseAndValidateEmailAddress_Given_AddressWithUppercase_Should_ReturnError(t *testing.T) {
65+
func Test_ParseAndValidateEmailAddress_Given_AddressWithUppercase_Should_NormalizeToLowercase(t *testing.T) {
6666
ev := EmailValidator{}
6767

68-
testCases := []string{
69-
"John Doe <John.Doe@Example.com>", // full name with angle brackets
70-
" John.Doe@Example.com", // leading whitespaces
71-
"\t\tJohn.Doe@Example.com", // leading tabs
72-
"John.Doe@Example.com ", // trailing whitespaces
73-
"John.Doe@Example.com\t", // trailing tabs
74-
"John.Doe+tag@Example.com", // with plus tag
68+
testCases := []struct {
69+
input string
70+
expected string
71+
}{
72+
{"John Doe <John.Doe@Example.com>", "john.doe@example.com"},
73+
{" John.Doe@Example.com", "john.doe@example.com"},
74+
{"\t\tJohn.Doe@Example.com", "john.doe@example.com"},
75+
{"John.Doe@Example.com ", "john.doe@example.com"},
76+
{"John.Doe@Example.com\t", "john.doe@example.com"},
77+
{"John.Doe+tag@Example.com", "john.doe+tag@example.com"},
7578
}
7679

7780
for _, tc := range testCases {
78-
valid, parsedAddress, err := ev.ParseAndValidateEmailAddress(tc)
81+
valid, parsedAddress, err := ev.ParseAndValidateEmailAddress(tc.input)
7982

80-
require.False(t, valid)
81-
require.Nil(t, parsedAddress)
82-
require.Equal(t, "error_email_format_lowercase", *err)
83+
require.True(t, valid)
84+
require.Nil(t, err)
85+
require.Equal(t, tc.expected, *parsedAddress)
8386
}
8487
}

0 commit comments

Comments
 (0)