validations
parent
0f671de43c
commit
3b650cb099
|
@ -1,4 +1,5 @@
|
||||||
using AutoMapper;
|
using AutoMapper;
|
||||||
|
using Correo.Application.Extensions;
|
||||||
using Correo.Domain.Models;
|
using Correo.Domain.Models;
|
||||||
using Correo.PublishedLanguage.Commands;
|
using Correo.PublishedLanguage.Commands;
|
||||||
using Correo.PublishedLanguage.Events;
|
using Correo.PublishedLanguage.Events;
|
||||||
|
@ -35,6 +36,8 @@ namespace Correo.Application.CommandHandlers
|
||||||
if (emailMessage.From == null)
|
if (emailMessage.From == null)
|
||||||
emailMessage.From = new EmailMessage.MailAddress(_defaultSender.Value.Address, _defaultSender.Value.Name);
|
emailMessage.From = new EmailMessage.MailAddress(_defaultSender.Value.Address, _defaultSender.Value.Name);
|
||||||
|
|
||||||
|
emailMessage.Validate();
|
||||||
|
|
||||||
// send email
|
// send email
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|
|
@ -0,0 +1,37 @@
|
||||||
|
using Correo.Application.Utils;
|
||||||
|
using Correo.Domain.Models;
|
||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace Correo.Application.Extensions
|
||||||
|
{
|
||||||
|
internal static class ValidationExtensions
|
||||||
|
{
|
||||||
|
public static void Validate(this EmailMessage emailMessage)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(emailMessage.Subject))
|
||||||
|
throw new Exception("Subject is missing.");
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(emailMessage.Body))
|
||||||
|
throw new Exception("Body is missing.");
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(emailMessage.From.Address))
|
||||||
|
throw new Exception("'From' address is missing.");
|
||||||
|
|
||||||
|
if (!Validations.IsEmail(emailMessage.From.Address))
|
||||||
|
throw new Exception($"'From' email address '{emailMessage.From.Address}' is not valid.");
|
||||||
|
|
||||||
|
if (emailMessage.To.Count() == 0)
|
||||||
|
throw new Exception("'To' addresses are empty.");
|
||||||
|
|
||||||
|
if (emailMessage.To.Any(z => !Validations.IsEmail(z.Address)))
|
||||||
|
throw new Exception("There are invalid email addresses in 'To' list.");
|
||||||
|
|
||||||
|
if (emailMessage.Cc.Any(z => !Validations.IsEmail(z.Address)))
|
||||||
|
throw new Exception("There are invalid email addresses in 'Cc' list.");
|
||||||
|
|
||||||
|
if (emailMessage.Bcc.Any(z => !Validations.IsEmail(z.Address)))
|
||||||
|
throw new Exception("There are invalid email addresses in 'Bcc' list.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
|
namespace Correo.Application.Utils
|
||||||
|
{
|
||||||
|
internal static class Validations
|
||||||
|
{
|
||||||
|
public static bool IsEmail(string email)
|
||||||
|
{
|
||||||
|
var pattern = @"^[\w!#$%&'*+\-/=?\^_`{|}~]+(\.[\w!#$%&'*+\-/=?\^_`{|}~]+)*" + "@" + @"((([\-\w]+\.)+[a-zA-Z]{2,4})|(([0-9]{1,3}\.){3}[0-9]{1,3}))$";
|
||||||
|
var reg = new Regex(pattern);
|
||||||
|
var valid = reg.IsMatch(email);
|
||||||
|
return valid;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue