26 lines
773 B
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Text.RegularExpressions;
using FluentValidation;
using FluentValidation.Results;
using ValueOf;
namespace Customers.Api.Domain.Common;
public class EmailAddress : ValueOf<string, EmailAddress>
{
private static readonly Regex EmailRegex =
new("^[\\w!#$%&*+/=?`{|}~^-]+(?:\\.[\\w!#$%&*+/=?`{|}~^-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$",
RegexOptions.Compiled | RegexOptions.IgnoreCase);
protected override void Validate()
{
if (!EmailRegex.IsMatch(Value))
{
var message = $"{Value} is not a valid email address";
throw new ValidationException(message, new []
{
new ValidationFailure(nameof(EmailAddress), message)
});
}
}
}