From c596d72a8bdb06b11137dfd1b35abc5b581eac7a Mon Sep 17 00:00:00 2001
From: Maximilian Moser <maximilian.moser@tuwien.ac.at>
Date: Thu, 13 Feb 2025 16:53:30 +0100
Subject: [PATCH] Allow secondary email address to be unset again

* setting an empty value in the settings form previously caused a
  validation error because the empty string is not a valid email address
---
 CHANGES.rst                         |  5 +++++
 invenio_config_tuw/users/schemas.py | 10 +++++++++-
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/CHANGES.rst b/CHANGES.rst
index a42ec92..18584b2 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -9,6 +9,11 @@ Changes
 =======
 
 
+Version <next>
+
+- Allow secondary email address to be removed again
+
+
 Version 2025.1.8 (released 2025-02-13)
 
 - Explicitly set calculated value for `THEME_SITEURL` in the config
diff --git a/invenio_config_tuw/users/schemas.py b/invenio_config_tuw/users/schemas.py
index 3134866..41e1040 100644
--- a/invenio_config_tuw/users/schemas.py
+++ b/invenio_config_tuw/users/schemas.py
@@ -15,7 +15,7 @@ from invenio_users_resources.services.schemas import (
     NotificationPreferences,
     UserProfileSchema,
 )
-from marshmallow import fields
+from marshmallow import fields, pre_load
 
 
 # profile
@@ -33,6 +33,14 @@ class TUWNotificationPreferencesSchema(NotificationPreferences):
 
     secondary_email = fields.Email()
 
+    @pre_load
+    def remove_empty_secondary_mail(self, data, **kwargs):
+        """Turn empty string for secondary emails into `None`."""
+        if not data.get("secondary_email"):
+            data.pop("secondary_email", None)
+
+        return data
+
 
 class TUWUserPreferencesSchema(UserPreferencesSchema):
     """User preferences schema with TU Wien extensions."""
-- 
GitLab