From 136e50177e0626f7fac5effc5840d0664e83cc5e Mon Sep 17 00:00:00 2001 From: Massimo Di Pierro Date: Fri, 29 Nov 2024 12:59:49 -0800 Subject: [PATCH] fixed IS_LIST_OF_EMAIL validator --- pydal/validators.py | 18 +++++++++++++----- tests/validators.py | 12 ++++++++---- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/pydal/validators.py b/pydal/validators.py index f666bea68..211149a57 100644 --- a/pydal/validators.py +++ b/pydal/validators.py @@ -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: diff --git a/tests/validators.py b/tests/validators.py index 796786b6b..ecf7dd23f 100644 --- a/tests/validators.py +++ b/tests/validators.py @@ -687,18 +687,22 @@ def test_IS_EMAIL(self): def test_IS_LIST_OF_EMAILS(self): emails = ["localguy@localhost", "_Yosemite.Sam@example.com"] 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;_Yosemite.Sam@example.com;a", "Invalid emails: a") ) + rtn = IS_LIST_OF_EMAILS()("") + self.assertEqual( + rtn, ([], None) + ) rtn = IS_LIST_OF_EMAILS().formatter(["test@example.com", "dude@example.com"]) - self.assertEqual(rtn, "test@example.com, dude@example.com") + self.assertEqual(rtn, "test@example.com; dude@example.com") def test_IS_URL(self): rtn = IS_URL()("http://example.com")