fix: allow special characters in email (#5327)

Co-authored-by: crazywoola <427733928@qq.com>
This commit is contained in:
Mitsuki Ogasahara
2024-06-17 22:32:59 +09:00
committed by GitHub
parent edffa5666d
commit 7305713b97
4 changed files with 29 additions and 5 deletions

View File

@@ -25,7 +25,7 @@ class TimestampField(fields.Raw):
def email(email):
# Define a regex pattern for email addresses
pattern = r"^[\w\.-]+@([\w-]+\.)+[\w-]{2,}$"
pattern = r"^[\w\.!#$%&'*+\-/=?^_`{|}~]+@([\w-]+\.)+[\w-]{2,}$"
# Check if the email matches the pattern
if re.match(pattern, email) is not None:
return email

View File

@@ -0,0 +1,25 @@
from libs.helper import email
def test_email_with_valid_email():
assert email("test@example.com") == "test@example.com"
assert email("TEST12345@example.com") == "TEST12345@example.com"
assert email("test+test@example.com") == "test+test@example.com"
assert email("!#$%&'*+-/=?^_{|}~`@example.com") == "!#$%&'*+-/=?^_{|}~`@example.com"
def test_email_with_invalid_email():
try:
email("invalid_email")
except ValueError as e:
assert str(e) == "invalid_email is not a valid email."
try:
email("@example.com")
except ValueError as e:
assert str(e) == "@example.com is not a valid email."
try:
email("()@example.com")
except ValueError as e:
assert str(e) == "()@example.com is not a valid email."