aboutsummaryrefslogtreecommitdiff
path: root/lib/VNLib.Plugins.Extensions.Validation/src/ValidatorExtensions.cs
blob: 63b4f076bb554def2aa22f63067fa01c85a99c20 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* Copyright (c) 2022 Vaughn Nugent
* 
* Library: VNLib
* Package: VNLib.Plugins.Extensions.Validation
* File: ValidatorExtensions.cs 
*
* ValidatorExtensions.cs is part of VNLib.Plugins.Extensions.Validation which is part of the larger 
* VNLib collection of libraries and utilities.
*
* VNLib.Plugins.Extensions.Validation 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.
*
* VNLib.Plugins.Extensions.Validation 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 VNLib.Plugins.Extensions.Validation. If not, see http://www.gnu.org/licenses/.
*/

using System;
using System.Collections;
using System.Text.RegularExpressions;

using FluentValidation;
using FluentValidation.Results;

namespace VNLib.Plugins.Extensions.Validation
{
    /// <summary>
    /// Defines extenstion methods for <see cref="IRuleBuilder{T, TProperty}"/>
    /// </summary>
    public static class ValidatorExtensions
    {
        public static readonly Regex PhoneRegex = new(@"^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$", RegexOptions.Compiled);

        public static readonly Regex AlphaRegx = new(@"[a-zA-Z]*", RegexOptions.Compiled);
        public static readonly Regex NumericRegx = new(@"[0-9]*", RegexOptions.Compiled);
        public static readonly Regex AlphaNumRegx = new(@"[a-zA-Z0-9]*", RegexOptions.Compiled);

        public static readonly Regex OnlyAlphaRegx = new(@"^[a-zA-Z\s]*$", RegexOptions.Compiled);
        public static readonly Regex OnlyNumericRegx = new(@"^[0-9]*$", RegexOptions.Compiled);
        public static readonly Regex OnlyAlphaNumRegx = new(@"^[a-zA-Z0-9\s]*$", RegexOptions.Compiled);

        public static readonly Regex PasswordRegx = new(@"^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$ %^&*-])", RegexOptions.Compiled);
        public static readonly Regex IllegalRegx = new(@"[\r\n\t\a\b\e\f|^~`<>{}]", RegexOptions.Compiled);
        public static readonly Regex SpecialCharactersRegx = new(@"[\r\n\t\a\b\e\f#?!@$%^&*\+\-\~`|<>\{}]", RegexOptions.Compiled);


        /// <summary>
        /// Gets a collection of Json-serializable validation errors
        /// </summary>
        /// <param name="result"></param>
        /// <returns>A collection of json errors to return to a user</returns>
        public static ICollection GetErrorsAsCollection(this ValidationResult result)
        {
            return result.Errors.ConvertAll(static err => new ValidationErrorMessage { ErrorMessage = err.ErrorMessage, PropertyName = err.PropertyName });
        }

        /// <summary>
        /// Tests the the property against <see cref="PhoneRegex"/> 
        /// to determine if the string matches the proper phone number form
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="builder"></param>
        /// <returns></returns>
        public static IRuleBuilderOptions<T, string> PhoneNumber<T>(this IRuleBuilder<T, string> builder)
        {
            return builder.Must(static phone => phone?.Length > 0 && PhoneRegex.IsMatch(phone))
                          .WithMessage("{PropertyValue} is not a valid phone number.");
        }
        /// <summary>
        /// Tests the the property against <see cref="PhoneRegex"/> 
        /// to determine if the string matches the proper phone number form, or allows emtpy strings
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="builder"></param>
        /// <returns></returns>
        public static IRuleBuilderOptions<T, string> EmptyPhoneNumber<T>(this IRuleBuilder<T, string> builder)
        {
            return builder.Must(static phone => phone == null || phone.Length == 0 || PhoneRegex.IsMatch(phone))
                          .WithMessage("{PropertyValue} is not a valid phone number.");
        }

        /// <summary>
        /// Checks a string against <see cref="SpecialCharactersRegx"/>.
        /// If the string is null or empty, it is allowed.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="builder"></param>
        /// <returns></returns>
        public static IRuleBuilderOptions<T, string> SpecialCharacters<T>(this IRuleBuilder<T, string> builder)
        {
            return builder.Must(static str => str == null || !SpecialCharactersRegx.IsMatch(str))
                          .WithMessage("{PropertyName} contains illegal characters");
        }
        /// <summary>
        /// Checks a string against <see cref="Statics.IllegalChars"/>.
        /// If the string is null or empty, it is allowed.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="builder"></param>
        /// <returns></returns>
        public static IRuleBuilderOptions<T, string> IllegalCharacters<T>(this IRuleBuilder<T, string> builder)
        {
            return builder.Must(static str => str == null || !IllegalRegx.IsMatch(str))
                          .WithMessage("{PropertyName} contains illegal characters");
        }
        /// <summary>
        /// Makes sure a field contains at least 1 character a-Z
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="builder"></param>
        /// <returns></returns>
        public static IRuleBuilderOptions<T, string> Alpha<T>(this IRuleBuilder<T, string> builder)
        {
            return builder.Must(static str => str == null || AlphaRegx.IsMatch(str))
                          .WithMessage("{PropertyName} requires at least one a-Z character.");
        }
        /// <summary>
        /// Determines if all characters are only a-Z (allows whitespace)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="builder"></param>
        /// <returns></returns>
        public static IRuleBuilderOptions<T, string> AlphaOnly<T>(this IRuleBuilder<T, string> builder)
        {
            return builder.Must(static str => str == null || OnlyAlphaRegx.IsMatch(str))
                          .WithMessage("{PropertyName} can only be a alpha character from a-Z.");
        }
        /// <summary>
        /// Makes sure a field contains at least 1 numeral
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="builder"></param>
        /// <returns></returns>
        public static IRuleBuilderOptions<T, string> Numeric<T>(this IRuleBuilder<T, string> builder)
        {
            return builder.Must(static str => str == null || NumericRegx.IsMatch(str))
                          .WithMessage("{PropertyName} requires at least one number.");
        }
        /// <summary>
        /// Determines if all characters are only 0-9 (not whitespace is allowed)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="builder"></param>
        /// <returns></returns>
        public static IRuleBuilderOptions<T, string> NumericOnly<T>(this IRuleBuilder<T, string> builder)
        {
            return builder.Must(static str => str == null || OnlyNumericRegx.IsMatch(str))
                          .WithMessage("{PropertyName} can only be a number 0-9.");
        }
        /// <summary>
        /// Makes sure the field contains at least 1 alpha numeric character (whitespace included)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="builder"></param>
        /// <returns></returns>
        public static IRuleBuilderOptions<T, string> AlphaNumeric<T>(this IRuleBuilder<T, string> builder)
        {
            return builder.Must(static str => str == null || AlphaNumRegx.IsMatch(str))
                          .WithMessage("{PropertyName} must contain at least one alpha-numeric character.");
        }
        /// <summary>
        /// Determines if all characters are only alpha-numeric (whitespace allowed)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="builder"></param>
        /// <returns></returns>
        public static IRuleBuilderOptions<T, string> AlphaNumericOnly<T>(this IRuleBuilder<T, string> builder)
        {
            return builder.Must(static str => str == null || OnlyAlphaNumRegx.IsMatch(str))
                          .WithMessage("{PropertyName} can only contain alpha numeric characters.");
        }
        /// <summary>
        /// Tests the string against the password regular expression to determine if the 
        /// value meets the basic password requirements
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="builder"></param>
        /// <returns></returns>
        public static IRuleBuilderOptions<T, string> Password<T>(this IRuleBuilder<T, string> builder)
        {
            return builder.Must(static str => str == null || PasswordRegx.IsMatch(str))
                          .WithMessage("{PropertyName} does not meet password requirements.");
        }
        /// <summary>
        /// Defines a length validator on the current rule builder, but only for string properties.
        /// Validation will fail if the length of the string is outside of the specified range.
        /// The range is inclusive
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="builder"></param>
        /// <param name="lengthRange">The length range of the specified string</param>
        /// <returns></returns>
        public static IRuleBuilderOptions<T, string> Length<T>(this IRuleBuilder<T, string> builder, Range lengthRange)
        {
            return builder.Length(lengthRange.Start.Value, lengthRange.End.Value);
        }
    }
}