aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBobby <[email protected]>2022-09-10 20:29:09 -0400
committerBobby <[email protected]>2022-09-10 20:29:09 -0400
commit5085e5d09086766e2b3a55f6d9e2f797015f25d0 (patch)
tree7bc5f0dff1384867cf4d58efcc872543119bf727
parent6361141215b89942105203f91d34a2794d89a295 (diff)
downloadedify-5085e5d09086766e2b3a55f6d9e2f797015f25d0.tar.xz
edify-5085e5d09086766e2b3a55f6d9e2f797015f25d0.zip
Added email_rfc_5322 pattern
-rw-r--r--docs/built-in/index.rst14
-rw-r--r--src/edify/library/__init__.py1
-rw-r--r--src/edify/library/mail.py16
-rw-r--r--tests/test_email.py41
-rw-r--r--tests/test_phone.py2
5 files changed, 69 insertions, 5 deletions
diff --git a/docs/built-in/index.rst b/docs/built-in/index.rst
index 8d51c58..ff4a684 100644
--- a/docs/built-in/index.rst
+++ b/docs/built-in/index.rst
@@ -25,6 +25,20 @@ Then, call the ``email`` function with a string argument.
email('[email protected]') # returns True
email('hello') # returns False
+email_rfc_5322()
+-----------------
+
+The ``email_rfc_5322`` function verifies that a string is a valid email address according to the `RFC 5322 <https://tools.ietf.org/html/rfc5322>`_ standard which allows for the most complete validation. Usually, you should not use it because it is an overkill. In most cases apps are not able to handle all emails that this regex allows. The function takes a ``string`` argument which is supposed to be a valid email address. The function returns ``True`` if the string is a valid email address, and ``False`` otherwise.
+
+You can use the ``email_rfc_5322`` function as follows:
+
+.. code-block:: python
+
+ from edify.library import email_rfc_5322
+
+ email_rfc_5322('[email protected]') # returns True
+ email_rfc_5322('hello') # returns False
+
phone()
-------
diff --git a/src/edify/library/__init__.py b/src/edify/library/__init__.py
index 5411d36..e77c7f6 100644
--- a/src/edify/library/__init__.py
+++ b/src/edify/library/__init__.py
@@ -2,4 +2,5 @@
# Import everything from the library.
from .mail import email
+from .mail import email_rfc_5322
from .phone import phone_number
diff --git a/src/edify/library/mail.py b/src/edify/library/mail.py
index ecb0cf2..02f9484 100644
--- a/src/edify/library/mail.py
+++ b/src/edify/library/mail.py
@@ -1,6 +1,7 @@
import re
pattern = r"^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$" # noqa
+rfc_5322_pattern = "(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])" # noqa
def email(email: str) -> bool:
@@ -16,3 +17,18 @@ def email(email: str) -> bool:
return True
else:
return False
+
+
+def email_rfc_5322(email: str) -> bool:
+ """Checks if a string is a valid email address.
+
+ Args:
+ email (str): The string to check.
+ Returns:
+ bool: True if the string is a valid email address, False otherwise.
+ """
+
+ if re.match(rfc_5322_pattern, email):
+ return True
+ else:
+ return False
diff --git a/tests/test_email.py b/tests/test_email.py
index 2c90c52..cc28472 100644
--- a/tests/test_email.py
+++ b/tests/test_email.py
@@ -1,8 +1,7 @@
from edify.library import email
+from edify.library import email_rfc_5322
-
-def test():
- emails = [
+emails = [
@@ -26,7 +25,11 @@ def test():
"あいうえお@example.com",
- ]
+]
+
+
+def test_email():
+
expectations = [
True,
True,
@@ -54,3 +57,33 @@ def test():
]
for i in range(len(emails)):
assert email(emails[i]) == expectations[i]
+
+
+def test_email_rfc_5322():
+ expectations = [
+ True,
+ True,
+ True,
+ True,
+ True,
+ True,
+ True,
+ True,
+ True,
+ True,
+ True,
+ False,
+ False,
+ False,
+ False,
+ False,
+ False,
+ False,
+ False,
+ False,
+ False,
+ False,
+ False
+ ]
+ for i in range(len(emails)):
+ assert email_rfc_5322(emails[i]) == expectations[i]
diff --git a/tests/test_phone.py b/tests/test_phone.py
index 81168ab..8c95d60 100644
--- a/tests/test_phone.py
+++ b/tests/test_phone.py
@@ -10,7 +10,7 @@ def test():
"123 456 7890": True,
"+1 (123) 456-7890": True,
"+1 (123) 456 7890": True,
- "+1 (123) 456-7890": True,
+ "+1-(123)-456-7890": True,
"+102 (123) 456-7890": True,
"+91 (123) 456-7890": True,
"90122121": True,