speqtr commited on
Commit
b3fd53e
1 Parent(s): 39b9a1f

email validation

Browse files
Files changed (3) hide show
  1. introduck/routes.py +24 -2
  2. introduck/utils.py +34 -3
  3. requirements.txt +1 -0
introduck/routes.py CHANGED
@@ -5,6 +5,8 @@ from fastapi import FastAPI
5
  from gradio.routes import App as GradioApp
6
  from introduck.inference import extract_contacts_from_text
7
  from introduck.utils import dump_email_to_string
 
 
8
 
9
  _INTRO_SUBJECT_EXAMPLE: str = "Could you make an intro?"
10
  _INTRO_MESSAGE_EXAMPLE: str = """\
@@ -39,7 +41,17 @@ def _analyze_message(
39
  ) -> (pd.DataFrame, dict):
40
  default_outputs = None, None
41
 
42
- # TODO: validate sender and recipients
 
 
 
 
 
 
 
 
 
 
43
 
44
  if not subject:
45
  return default_outputs
@@ -77,7 +89,17 @@ def _validate_message(
77
  ) -> dict:
78
  errors: list[str] = []
79
 
80
- # ...
 
 
 
 
 
 
 
 
 
 
81
 
82
  if not subject:
83
  errors.append("- subject can't be empty;")
 
5
  from gradio.routes import App as GradioApp
6
  from introduck.inference import extract_contacts_from_text
7
  from introduck.utils import dump_email_to_string
8
+ from introduck.utils import validate_email
9
+ from introduck.utils import validate_multiple_emails
10
 
11
  _INTRO_SUBJECT_EXAMPLE: str = "Could you make an intro?"
12
  _INTRO_MESSAGE_EXAMPLE: str = """\
 
41
  ) -> (pd.DataFrame, dict):
42
  default_outputs = None, None
43
 
44
+ # validate sender email (if present)
45
+ if sender:
46
+ sender = validate_email(email=sender)
47
+ if not sender:
48
+ return default_outputs
49
+
50
+ # validate recipient emails (if present)
51
+ if recipients:
52
+ recipients = validate_multiple_emails(emails=recipients)
53
+ if not recipients:
54
+ return default_outputs
55
 
56
  if not subject:
57
  return default_outputs
 
89
  ) -> dict:
90
  errors: list[str] = []
91
 
92
+ # validate sender email (if present)
93
+ if sender:
94
+ sender = validate_email(email=sender)
95
+ if not sender:
96
+ errors.append("- sender email is not valid;")
97
+
98
+ # validate recipient emails (if present)
99
+ if recipients:
100
+ recipients = validate_multiple_emails(emails=recipients)
101
+ if not recipients:
102
+ errors.append("- one or more recipient emails are not valid;")
103
 
104
  if not subject:
105
  errors.append("- subject can't be empty;")
introduck/utils.py CHANGED
@@ -1,3 +1,7 @@
 
 
 
 
1
  def dump_email_to_string(data: dict) -> str:
2
  msg_from: str = data.get("from", "") or "*"
3
  msg_to: str = data.get("to", "") or "*"
@@ -13,6 +17,33 @@ def dump_email_to_string(data: dict) -> str:
13
  return msg
14
 
15
 
16
- def validate_email_address(data: str) -> bool:
17
- # TODO: https://pypi.org/project/email-validator/
18
- return False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from email_validator import EmailNotValidError
2
+ from email_validator import validate_email as validate_email_address
3
+
4
+
5
  def dump_email_to_string(data: dict) -> str:
6
  msg_from: str = data.get("from", "") or "*"
7
  msg_to: str = data.get("to", "") or "*"
 
17
  return msg
18
 
19
 
20
+ def validate_email(email: str) -> str:
21
+ if not email:
22
+ return ""
23
+
24
+ try:
25
+ validation = validate_email_address(email, check_deliverability=False)
26
+
27
+ # Take the normalized form of the email address for all logic beyond
28
+ # this point (especially before going to a database query where equality
29
+ # may not take into account Unicode normalization).
30
+ email = validation.email
31
+ except EmailNotValidError as e:
32
+ # Email is not valid. The exception message is human-readable.
33
+ email = ""
34
+ print(str(e))
35
+
36
+ return email
37
+
38
+
39
+ def validate_multiple_emails(emails: str) -> str:
40
+ if not emails:
41
+ return ""
42
+
43
+ for email in emails.split(sep=","):
44
+ email = validate_email(email=email.strip(" "))
45
+ if not email:
46
+ emails = ""
47
+ break
48
+
49
+ return emails
requirements.txt CHANGED
@@ -1,3 +1,4 @@
 
1
  fastapi[all]
2
  gradio==3.4.0 # keep in sync with version from README.md metadata
3
  pandas==1.5.0
 
1
+ email-validator==1.3.0
2
  fastapi[all]
3
  gradio==3.4.0 # keep in sync with version from README.md metadata
4
  pandas==1.5.0