-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathValidate.cs
More file actions
294 lines (283 loc) · 10.2 KB
/
Copy pathValidate.cs
File metadata and controls
294 lines (283 loc) · 10.2 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
using BytecodeApi.Extensions;
using System.ComponentModel.DataAnnotations;
using System.Net.Mail;
using System.Text.RegularExpressions;
namespace BytecodeApi;
/// <summary>
/// Validates <see cref="string" /> objects against specific data type representations.
/// </summary>
public static class Validate
{
private static readonly Regex[] MacAddressRegex =
[
// 000000000000
new("^[0-9a-f]{12}$", RegexOptions.IgnoreCase),
// 00:00:00:00:00:00
new("^([0-9a-f]{2}:){5}[0-9a-f]{2}$", RegexOptions.IgnoreCase),
// 00-00-00-00-00-00
new("^([0-9a-f]{2}-){5}[0-9a-f]{2}$", RegexOptions.IgnoreCase),
// 0000.0000.0000
new("^([0-9a-f]{4}\\.){2}[0-9a-f]{4}$", RegexOptions.IgnoreCase),
// 000.000.000.000
new("^([0-9a-f]{3}\\.){3}[0-9a-f]{3}$", RegexOptions.IgnoreCase),
// 0000000000000000
new("^[0-9a-f]{16}$", RegexOptions.IgnoreCase),
// 00:00:00:00:00:00:00:00
new("^([0-9a-f]{2}:){7}[0-9a-f]{2}$", RegexOptions.IgnoreCase),
// 00-00-00-00-00-00-00-00
new("^([0-9a-f]{2}-){7}[0-9a-f]{2}$", RegexOptions.IgnoreCase),
// 0000.0000.0000.0000
new("^([0-9a-f]{4}\\.){3}[0-9a-f]{4}$", RegexOptions.IgnoreCase)
];
/// <summary>
/// Validates a <see cref="string" /> that only contains upper and lowercase letters.
/// </summary>
/// <param name="str">The <see cref="string" /> to be validated.</param>
/// <returns>
/// <see langword="true" />, if validation of <paramref name="str" /> succeeded;
/// otherwise, <see langword="false" />.
/// </returns>
public static bool OnlyLetters([NotNullWhen(true)] string? str)
{
return str?.All(char.IsLetter) == true;
}
/// <summary>
/// Validates a <see cref="string" /> that only contains numeric digits.
/// </summary>
/// <param name="str">The <see cref="string" /> to be validated.</param>
/// <returns>
/// <see langword="true" />, if validation of <paramref name="str" /> succeeded;
/// otherwise, <see langword="false" />.
/// </returns>
public static bool OnlyDigits([NotNullWhen(true)] string? str)
{
return str?.All(char.IsDigit) == true;
}
/// <summary>
/// Validates a <see cref="string" /> that only contains upper and lowercase letters as well as numeric digits.
/// </summary>
/// <param name="str">The <see cref="string" /> to be validated.</param>
/// <returns>
/// <see langword="true" />, if validation of <paramref name="str" /> succeeded;
/// otherwise, <see langword="false" />.
/// </returns>
public static bool AlphaNumeric([NotNullWhen(true)] string? str)
{
return str?.All(char.IsLetterOrDigit) == true;
}
/// <summary>
/// Validates a <see cref="string" /> that only contains upper and lowercase hexadecimal digits.
/// </summary>
/// <param name="str">The <see cref="string" /> to be validated.</param>
/// <returns>
/// <see langword="true" />, if validation of <paramref name="str" /> succeeded;
/// otherwise, <see langword="false" />.
/// </returns>
public static bool Hexadecimal([NotNullWhen(true)] string? str)
{
return str?.All(CharExtensions.IsHexadecimal) == true;
}
/// <summary>
/// Validates a <see cref="string" /> that is a base64 string, only containing the characters a-zA-Z0-9+/= and has the correct amount of padding characters.
/// </summary>
/// <param name="str">The <see cref="string" /> to be validated.</param>
/// <returns>
/// <see langword="true" />, if validation of <paramref name="str" /> succeeded;
/// otherwise, <see langword="false" />.
/// </returns>
public static bool Base64String([NotNullWhen(true)] string? str)
{
if (str == null)
{
return false;
}
else
{
bool padding = false;
int contentLength = 0;
int paddingLength = 0;
foreach (char c in str.Where(c => !c.IsWhiteSpace()))
{
if (c is >= 'a' and <= 'z' or >= 'A' and <= 'Z' or >= '0' and <= '9' or '+' or '/')
{
if (padding)
{
return false;
}
else
{
contentLength++;
}
}
else if (c == '=')
{
padding = true;
paddingLength++;
}
}
contentLength &= 3;
return
contentLength == 0 && paddingLength == 0 ||
contentLength == 2 && paddingLength == 2 ||
contentLength == 3 && paddingLength is 1 or 2;
}
}
/// <summary>
/// Validates a <see cref="string" /> that is an absolute file path.
/// </summary>
/// <param name="str">The <see cref="string" /> to be validated.</param>
/// <returns>
/// <see langword="true" />, if validation of <paramref name="str" /> succeeded;
/// otherwise, <see langword="false" />.
/// </returns>
public static bool Path([NotNullWhen(true)] string? str)
{
if (str.IsNullOrWhiteSpace())
{
return false;
}
else
{
string? root;
string? fileName;
try
{
root = System.IO.Path.GetPathRoot(str);
fileName = System.IO.Path.GetFileName(str);
}
catch (ArgumentException)
{
return false;
}
return
root?.Length >= 3 &&
root[0] is >= 'a' and <= 'z' or >= 'A' and <= 'Z' &&
root[1] == ':' &&
root[2] is '\\' or '/' &&
root.IndexOfAny(System.IO.Path.GetInvalidPathChars()) == -1 &&
(fileName == null || fileName.IndexOfAny(System.IO.Path.GetInvalidFileNameChars()) == -1);
}
}
/// <summary>
/// Validates a <see cref="string" /> that is a filename.
/// </summary>
/// <param name="str">The <see cref="string" /> to be validated.</param>
/// <returns>
/// <see langword="true" />, if validation of <paramref name="str" /> succeeded;
/// otherwise, <see langword="false" />.
/// </returns>
public static bool FileName([NotNullWhen(true)] string? str)
{
return !str.IsNullOrWhiteSpace() && str.IndexOfAny(System.IO.Path.GetInvalidFileNameChars()) == -1;
}
/// <summary>
/// Validates a <see cref="string" /> that is a web URL with http or https scheme.
/// </summary>
/// <param name="str">The <see cref="string" /> to be validated.</param>
/// <returns>
/// <see langword="true" />, if validation of <paramref name="str" /> succeeded;
/// otherwise, <see langword="false" />.
/// </returns>
public static bool Url([NotNullWhen(true)] string? str)
{
return Uri.TryCreate(str, UriKind.Absolute, out Uri? uri) && uri.Scheme is "http" or "https";
}
/// <summary>
/// Validates a <see cref="string" /> that is a website (with or without http(s):// or www.)
/// </summary>
/// <param name="str">The <see cref="string" /> to be validated.</param>
/// <returns>
/// <see langword="true" />, if validation of <paramref name="str" /> succeeded;
/// otherwise, <see langword="false" />.
/// </returns>
public static bool Website([NotNullWhen(true)] string? str)
{
if (str.IsNullOrWhiteSpace())
{
return false;
}
if (!str.Contains("://"))
{
str = $"http://{str}";
}
return
Uri.TryCreate(str, UriKind.Absolute, out Uri? uri) &&
Uri.CheckHostName(uri.Host) == UriHostNameType.Dns &&
uri.Scheme.ToLower() is "http" or "https" &&
uri.Host.TrimStartString("www.").Contains('.');
}
/// <summary>
/// Validates a <see cref="string" /> that is an email address.
/// </summary>
/// <param name="str">The <see cref="string" /> to be validated.</param>
/// <returns>
/// <see langword="true" />, if validation of <paramref name="str" /> succeeded;
/// otherwise, <see langword="false" />.
/// </returns>
public static bool EmailAddress([NotNullWhen(true)] string? str)
{
return
MailAddress.TryCreate(str, out MailAddress? mailAddress) &&
mailAddress.Address == str && // John Doe <foo@example.com> might still be parsed as valid email address!
mailAddress.Host.Contains('.');
}
/// <summary>
/// Validates a <see cref="string" /> that is an <see cref="System.Net.IPAddress" />. Both IPv4 and IPv6 values are validated.
/// </summary>
/// <param name="str">The <see cref="string" /> to be validated.</param>
/// <returns>
/// <see langword="true" />, if validation of <paramref name="str" /> succeeded;
/// otherwise, <see langword="false" />.
/// </returns>
public static bool IPAddress([NotNullWhen(true)] string? str)
{
return System.Net.IPAddress.TryParse(str, out _);
}
/// <summary>
/// Validates a <see cref="string" /> that is a MAC address in EUI-48 or EUI-64 format. When this method returns, a <see cref="byte" />[] is stored in <paramref name="result" />, representing the parsed address bytes.
/// <para>Allowed formats are: 000000000000, 00:00:00:00:00:00, 00-00-00-00-00-00, 000.000.000.000 and 0000.0000.0000 for EUI-48; and 0000000000000000, 00:00:00:00:00:00:00:00, 00-00-00-00-00-00-00-00 and 0000.0000.0000.0000 for EUI-64.</para>
/// </summary>
/// <param name="str">The <see cref="string" /> to be validated.</param>
/// <param name="result">When this method returns, contains the address bytes as a <see cref="byte" />[].</param>
/// <returns>
/// <see langword="true" />, if validation of <paramref name="str" /> succeeded;
/// otherwise, <see langword="false" />.
/// </returns>
public static bool MacAddress([NotNullWhen(true)] string? str, [NotNullWhen(true)] out byte[]? result)
{
if (str != null && MacAddressRegex.Any(regex => regex.IsMatch(str)))
{
result = Convert.FromHexString(str.ReplaceMultiple(null, ":", "-", "."));
return true;
}
else
{
result = null;
return false;
}
}
/// <summary>
/// Validates a <see cref="string" /> that is a phone number.
/// </summary>
/// <param name="str">The <see cref="string" /> to be validated.</param>
/// <returns>
/// <see langword="true" />, if validation of <paramref name="str" /> succeeded;
/// otherwise, <see langword="false" />.
/// </returns>
public static bool PhoneNumber([NotNullWhen(true)] string? str)
{
return str != null && new PhoneAttribute().IsValid(str.Replace("/", null));
}
/// <summary>
/// Validates a <see cref="string" /> that is a credit card number.
/// </summary>
/// <param name="str">The <see cref="string" /> to be validated.</param>
/// <returns>
/// <see langword="true" />, if validation of <paramref name="str" /> succeeded;
/// otherwise, <see langword="false" />.
/// </returns>
public static bool CreditCardNumber([NotNullWhen(true)] string? str)
{
return str != null && new CreditCardAttribute().IsValid(str);
}
}