aboutsummaryrefslogtreecommitdiff
path: root/lib/Emails.Transactional.Plugin/src/Transactions/EmailTransactionValidator.cs
blob: d6642d88d761260aacf57bf0659f2bf317b54df9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* Copyright (c) 2023 Vaughn Nugent
* 
* Library: VNLib
* Package: Emails.Transactional
* File: EmailTransactionValidator.cs 
*
* EmailTransactionValidator.cs is part of Emails.Transactional which is part of the larger 
* VNLib collection of libraries and utilities.
*
* Emails.Transactional is free software: you can redistribute it and/or modify 
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 2 of the License,
* or (at your option) any later version.
*
* Emails.Transactional is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License 
* along with Emails.Transactional. If not, see http://www.gnu.org/licenses/.
*/

using FluentValidation;

using VNLib.Plugins.Extensions.Validation;


namespace Emails.Transactional.Transactions
{
    internal class EmailTransactionValidator : AbstractValidator<EmailTransaction>
    {
        public const int MAX_SUBJECT_LEN = 50;
        public EmailTransactionValidator()
        {
            //Catch to make sure user-id is set
            RuleFor(static t => t.UserId)
                .NotEmpty();
            //validate from addres/name
            RuleFor(static t => t.FromName)
                .NotEmpty()
                 .WithName("from_name")
                .AlphaNumericOnly()
                .WithName("from_name");
            RuleFor(static t => t.From)
                .NotEmpty()
                .EmailAddress()
                .WithName("from");

            //Rule names must be their json propery name, so clients can properly log errors
            //must include a template id
            RuleFor(static t => t.TemplateId)
                .NotEmpty()
                .MaximumLength(200)
                .WithName("template_id");

            //From address must not be empty
            RuleFor(static t => t.From)
                .NotEmpty()
                .EmailAddress();
            //Subject is required alpha num
            RuleFor(static t => t.Subject)
                .NotEmpty()
                .AlphaNumericOnly()
                .MaximumLength(MAX_SUBJECT_LEN)
                .WithName("subject");

            //To address must not be empty, and must be valid email addresses
            RuleFor(static t => t.ToAddresses)
                .NotEmpty()
                .ChildRules(static to => {
                        //Check keys (address)
                        to.RuleForEach(static b => b.Keys)
                    .NotEmpty()
                    .EmailAddress()
                    .WithName("to");

                        //Check values (names)
                        to.RuleForEach(static b => b.Values)
                    .NotEmpty()
                    .AlphaNumericOnly()
                    .WithName("to_name");
                })
                .WithName("to");
            //Check bcc addresses, allowed to be empty
            RuleFor(static t => t.BccAddresses)
                 .ChildRules(static bcc => {
                        //Check keys (address)
                    bcc.RuleForEach(static b => b.Keys)
                     .NotEmpty()
                     .EmailAddress()
                     .WithName("bcc");

                        //Check values (names)
                    bcc.RuleForEach(static b => b.Values)
                     .NotEmpty()
                     .AlphaNumericOnly()
                     .WithName("bcc_names");
                 })
                 .When(static t => t.BccAddresses != null)
                 .WithName("bcc");
            //Check cc, also allowed to be empty
            RuleFor(static t => t.CcAddresses)
                .ChildRules(static cc => {
                        //Check keys (names)
                    cc.RuleForEach(static b => b.Keys)
                    .NotEmpty()
                    .EmailAddress()
                    .WithName("cc");

                        //Check values (addresses)
                    cc.RuleForEach(static b => b.Values)
                    .NotEmpty()
                    .AlphaNumericOnly()
                    .WithName("cc_names");
                })
                .When(static t => t.BccAddresses != null)
                .WithName("cc");

            RuleFor(static t => t.ReplyTo)
                //Allow the reply to email to be empty, if its not, then validate the address
                .EmailAddress()
                .When(static t => t.BccAddresses != null)
                .WithName("reply_to");

            //Make sure a variable table is defined
            RuleFor(static t => t.Variables)
                .NotNull()
                .WithName("variables");
        }
    }
}