refactor(notification-outbox): extract EmailAddressValidator helper, drop orphaned using

This commit is contained in:
Joseph Doherty
2026-05-19 03:39:05 -04:00
parent 5e80f64cd8
commit 4b61e29e27
4 changed files with 41 additions and 32 deletions

View File

@@ -0,0 +1,38 @@
using MimeKit;
using ScadaLink.Commons.Entities.Notifications;
namespace ScadaLink.NotificationService;
/// <summary>
/// NS-008: Validates the sender and recipient email addresses before an SMTP
/// delivery is attempted, so a malformed address surfaces as a clean error
/// string rather than a <c>ParseException</c> escaping the delivery path.
/// <para>
/// Public so the central Notification Outbox's <c>EmailNotificationDeliveryAdapter</c>
/// can share this exact pre-send validation rather than carry a divergent copy.
/// </para>
/// </summary>
public static class EmailAddressValidator
{
/// <summary>
/// Validates the sender and recipient email addresses, returning a
/// human-readable error string if any is malformed, or null if all parse.
/// </summary>
public static string? ValidateAddresses(
string fromAddress, IReadOnlyList<NotificationRecipient> recipients)
{
if (!MailboxAddress.TryParse(fromAddress, out _))
{
return $"Invalid sender (from) email address: '{fromAddress}'";
}
var invalid = recipients
.Where(r => !MailboxAddress.TryParse(r.EmailAddress, out _))
.Select(r => r.EmailAddress)
.ToList();
return invalid.Count > 0
? $"Invalid recipient email address(es): {string.Join(", ", invalid)}"
: null;
}
}