Skip to content

Commit

Permalink
fixed IS_LIST_OF_EMAIL validator
Browse files Browse the repository at this point in the history
  • Loading branch information
mdipierro committed Nov 29, 2024
1 parent 4dda912 commit 136e501
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
18 changes: 13 additions & 5 deletions pydal/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -1354,19 +1354,27 @@ def __init__(self, error_message="Invalid emails: %s"):

def validate(self, value, record_id=None):
bad_emails = []
f = IS_EMAIL()
for email in re.findall(self.REGEX_NOT_EMAIL_SPLITTER, value):
f = IS_EMAIL()
if isinstance(value, str):
emails = re.findall(self.REGEX_NOT_EMAIL_SPLITTER, value)
else:
emails = value
for email in emails:
error = f(email)[1]
if error and email not in bad_emails:
bad_emails.append(email)
if bad_emails:
raise ValidationError(
self.translator(self.error_message) % ", ".join(bad_emails)
self.translator(self.error_message) % ";".join(bad_emails)
)
return value
return emails

def formatter(self, value, row=None):
return ", ".join(value or [])
if value is None:
return ""
if isinstance(value, str):
value = re.findall(self.REGEX_NOT_EMAIL_SPLITTER, value)
return "; ".join(value)


# URL scheme source:
Expand Down
12 changes: 8 additions & 4 deletions tests/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,18 +687,22 @@ def test_IS_EMAIL(self):
def test_IS_LIST_OF_EMAILS(self):
emails = ["localguy@localhost", "[email protected]"]
rtn = IS_LIST_OF_EMAILS()(",".join(emails))
self.assertEqual(rtn, (",".join(emails), None))
self.assertEqual(rtn, (emails, None))
rtn = IS_LIST_OF_EMAILS()(";".join(emails))
self.assertEqual(rtn, (";".join(emails), None))
self.assertEqual(rtn, (emails, None))
rtn = IS_LIST_OF_EMAILS()(" ".join(emails))
self.assertEqual(rtn, (" ".join(emails), None))
self.assertEqual(rtn, (emails, None))
emails.append("a")
rtn = IS_LIST_OF_EMAILS()(";".join(emails))
self.assertEqual(
rtn, ("localguy@localhost;[email protected];a", "Invalid emails: a")
)
rtn = IS_LIST_OF_EMAILS()("")
self.assertEqual(
rtn, ([], None)
)
rtn = IS_LIST_OF_EMAILS().formatter(["[email protected]", "[email protected]"])
self.assertEqual(rtn, "[email protected], [email protected]")
self.assertEqual(rtn, "[email protected]; [email protected]")

def test_IS_URL(self):
rtn = IS_URL()("http://example.com")
Expand Down

0 comments on commit 136e501

Please sign in to comment.