mirror of
https://github.com/aljazceru/btcpayserver.git
synced 2025-12-18 22:44:29 +01:00
Improve email settings validation and UX (#3891)
This commit is contained in:
39
BTCPayServer/Validation/MailboxAddressValidator.cs
Normal file
39
BTCPayServer/Validation/MailboxAddressValidator.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Text.RegularExpressions;
|
||||
using MimeKit;
|
||||
|
||||
namespace BTCPayServer
|
||||
{
|
||||
/// <summary>
|
||||
/// Validate address in the format "Firstname Lastname <blah@example.com>" See rfc822
|
||||
/// </summary>
|
||||
public class MailboxAddressValidator
|
||||
{
|
||||
static ParserOptions _options;
|
||||
static MailboxAddressValidator()
|
||||
{
|
||||
_options = ParserOptions.Default.Clone();
|
||||
_options.AllowAddressesWithoutDomain = false;
|
||||
}
|
||||
public static bool IsMailboxAddress(string? str)
|
||||
{
|
||||
return TryParse(str, out _);
|
||||
}
|
||||
public static MailboxAddress Parse(string? str)
|
||||
{
|
||||
if (!TryParse(str, out var mb))
|
||||
throw new FormatException("Invalid mailbox address (rfc822)");
|
||||
return mb;
|
||||
}
|
||||
public static bool TryParse(string? str, [MaybeNullWhen(false)] out MailboxAddress mailboxAddress)
|
||||
{
|
||||
mailboxAddress = null;
|
||||
if (String.IsNullOrWhiteSpace(str))
|
||||
return false;
|
||||
return MailboxAddress.TryParse(_options, str, out mailboxAddress) && mailboxAddress is not null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user