forked from ucsbfinaid/Financial-Aid-Estimator-.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArgumentValidator.cs
363 lines (314 loc) · 14.2 KB
/
ArgumentValidator.cs
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
using System;
using System.Collections.Generic;
using Ucsb.Sa.FinAid.AidEstimation.EfcCalculation;
namespace Ucsb.Sa.FinAid.AidEstimation.Utility
{
public class ArgumentValidator
{
public List<ValidationError> Errors { get; private set; }
private const double MaxMoneyValue = 999999999;
private const double MinMoneyValue = -999999999;
public ArgumentValidator()
{
Errors = new List<ValidationError>();
}
/// <summary>
/// Attempts to parse the input into a boolean. If the parsing fails, a <see cref="ValidationError"/> is
/// generated (using the provided parameter and message) and added to the validator's list of errors
/// </summary>
/// <param name="input">Value to parse</param>
/// <param name="inputDisplayName">Display name for the value being parsed</param>
/// <param name="inputParameterName">Parameter name (identifiable key) for the value being parsed</param>
/// <returns>The parsed boolean, or "false" if parsing fails</returns>
public bool ValidateBoolean(
string input,
string inputDisplayName,
string inputParameterName
)
{
ValidateInputInfo(inputDisplayName, inputParameterName);
bool value;
// Provided?
if (input == null)
{
Errors.Add(GetNoValueError(inputDisplayName, inputParameterName));
return false; // Stop validation
}
// Parsing
if (!Boolean.TryParse(input, out value))
{
Errors.Add(GetConversionError(inputDisplayName, inputParameterName));
return false;
}
return value;
}
/// <summary>
/// Attempts to parse the input into a double value that lies within the maximum and minimum range
/// (0-999999999). If the parsing fails, a <see cref="ValidationError"/> is
/// generated (using the provided parameter and message) and added to the validator's list of errors
/// </summary>
/// <param name="input">Value to parse</param>
/// <param name="inputDisplayName">Display name for the value being parsed</param>
/// <param name="inputParameterName">Parameter name (identifiable key) for the value being parsed</param>
/// <returns>The parsed value, or "0" if parsing fails</returns>
public double ValidatePositiveMoneyValue(string input, string inputDisplayName, string inputParameterName)
{
ValidateInputInfo(inputDisplayName, inputParameterName);
double value;
// Provided?
if (String.IsNullOrEmpty(input))
{
Errors.Add(GetNoValueError(inputDisplayName, inputParameterName));
return 0; // Stop validation
}
// Parsing
if (!Double.TryParse(input, out value))
{
Errors.Add(GetConversionError(inputDisplayName, inputParameterName));
return 0; // Stop validation
}
// Maximum Range
if (value >= MaxMoneyValue)
{
Errors.Add(GetMoneyMaximumError(inputDisplayName, inputParameterName));
return 0; // Stop validation
}
// Minimum Range
if (value < 0)
{
Errors.Add(GetPositiveError(inputDisplayName, inputParameterName));
return 0; // Stop validation
}
return value;
}
/// <summary>
/// Attempts to parse the input into a double value that lies within the maximum and minimum range
/// (-999999999-999999999). If the parsing fails, a <see cref="ValidationError"/> is
/// generated (using the provided parameter and message) and added to the validator's list of errors
/// </summary>
/// <param name="input">Value to parse</param>
/// <param name="inputDisplayName">Display name for the value being parsed</param>
/// <param name="inputParameterName">Parameter name (identifiable key) for the value being parsed</param>
/// <returns>The parsed value, or "0" if parsing fails</returns>
public double ValidateMoneyValue(
string input,
string inputDisplayName,
string inputParameterName
)
{
ValidateInputInfo(inputDisplayName, inputParameterName);
double value;
// Provided?
if (String.IsNullOrEmpty(input))
{
Errors.Add(GetNoValueError(inputDisplayName, inputParameterName));
return 0; // Stop validation
}
// Parsing
if (!Double.TryParse(input, out value))
{
Errors.Add(GetConversionError(inputDisplayName, inputParameterName));
return 0; // Stop validation
}
// Maximum Range
if (value >= MaxMoneyValue)
{
Errors.Add(GetMoneyMaximumError(inputDisplayName, inputParameterName));
return 0; // Stop validation
}
// Minimum Range
if (value <= MinMoneyValue)
{
Errors.Add(GetMoneyMinimumError(inputDisplayName, inputParameterName));
return 0; // Stop validation
}
return value;
}
/// <summary>
/// Attempts to parse the input into a positive integer value (greater than 0).
/// If the parsing fails, a <see cref="ValidationError"/> is generated (using the provided parameter and
/// message) and added to the validator's list of errors
/// </summary>
/// <param name="input">Value to parse</param>
/// <param name="inputDisplayName">Display name for the value being parsed</param>
/// <param name="inputParameterName">Parameter name (identifiable key) for the value being parsed</param>
/// <returns>The parsed value, or "0" if parsing fails</returns>
public int ValidateNonZeroInteger(
string input,
string inputDisplayName,
string inputParameterName
)
{
ValidateInputInfo(inputDisplayName, inputParameterName);
int value;
// Provided?
if (String.IsNullOrEmpty(input))
{
Errors.Add(GetNoValueError(inputDisplayName, inputParameterName));
return 0; // Stop validation
}
// Parsing
if (!Int32.TryParse(input, out value))
{
Errors.Add(GetConversionError(inputDisplayName, inputParameterName));
return 0; // Stop validation
}
if (value <= 0)
{
Errors.Add(GetNonZeroError(inputDisplayName, inputParameterName));
return 0; // Stop validation
}
return value;
}
/// <summary>
/// Attempts to parse the input into a <see cref="MaritalStatus"/> enumeration value.
/// If the parsing fails, a <see cref="ValidationError"/> is generated (using the provided parameter
/// and message) and added to the validator's list of errors
/// </summary>
/// <param name="input">Value to parse</param>
/// <param name="inputDisplayName">Display name for the value being parsed</param>
/// <param name="inputParameterName">Parameter name (identifiable key) for the value being parsed</param>
/// <returns>The parsed value, or <see cref="MaritalStatus.None"/> if parsing fails</returns>
public MaritalStatus ValidateMaritalStatus(
string input,
string inputDisplayName,
string inputParameterName
)
{
ValidateInputInfo(inputDisplayName, inputParameterName);
MaritalStatus maritalStatus;
// Provided?
if (String.IsNullOrEmpty(input))
{
Errors.Add(GetNoValueError(inputDisplayName, inputParameterName));
return MaritalStatus.None; // Stop validation
}
// Parsing
if (input.Equals("single", StringComparison.CurrentCultureIgnoreCase))
{
maritalStatus = MaritalStatus.SingleSeparatedDivorced;
}
else if (input.Equals("married", StringComparison.CurrentCultureIgnoreCase))
{
maritalStatus = MaritalStatus.MarriedRemarried;
}
else
{
Errors.Add(GetConversionError(inputDisplayName, inputParameterName));
return MaritalStatus.None; // Stop validation
}
return maritalStatus;
}
/// <summary>
/// Attempts to parse the input into a <see cref="UnitedStatesStateOrTerritory"/> enumeration value.
/// If the parsing fails, a <see cref="ValidationError"/> is generated (using the provided parameter
/// and message) and added to the validator's list of errors
/// </summary>
/// <param name="input">Value to parse</param>
/// <param name="inputDisplayName">Display name for the value being parsed</param>
/// <param name="inputParameterName">Parameter name (identifiable key) for the value being parsed</param>
/// <returns>The parsed value, or <see cref="UnitedStatesStateOrTerritory.Other"/> if parsing fails</returns>
public UnitedStatesStateOrTerritory ValidateUnitedStatesStateOrTerritory(
string input,
string inputDisplayName,
string inputParameterName
)
{
ValidateInputInfo(inputDisplayName, inputParameterName);
// Provided?
if (String.IsNullOrEmpty(input))
{
Errors.Add(GetNoValueError(inputDisplayName, inputParameterName));
return UnitedStatesStateOrTerritory.Other;
}
// Parsing
try
{
return (UnitedStatesStateOrTerritory)Enum.Parse(typeof(UnitedStatesStateOrTerritory), input, true);
}
catch (Exception)
{
Errors.Add(GetConversionError(inputDisplayName, inputParameterName));
return UnitedStatesStateOrTerritory.Other;
}
}
/// <summary>
/// Attempts to parse the input into a <see cref="IncomeEarnedBy"/> enumeration value.
/// If the parsing fails, a <see cref="ValidationError"/> is generated (using the provided parameter
/// and message) and added to the validator's list of errors
/// </summary>
/// <param name="input">Value to parse</param>
/// <param name="inputDisplayName">Display name for the value being parsed</param>
/// <param name="inputParameterName">Parameter name (identifiable key) for the value being parsed</param>
/// <returns>The parsed value, or <see cref="IncomeEarnedBy.None"/> if parsing fails</returns>
public IncomeEarnedBy ValidateIncomeEarnedBy(
string input,
string inputDisplayName,
string inputParameterName
)
{
ValidateInputInfo(inputDisplayName, inputParameterName);
// Provided?
if (String.IsNullOrEmpty(input))
{
Errors.Add(GetNoValueError(inputDisplayName, inputParameterName));
return IncomeEarnedBy.None;
}
// Parsing
try
{
return (IncomeEarnedBy)Enum.Parse(typeof(IncomeEarnedBy), input, true);
}
catch (Exception)
{
Errors.Add(GetConversionError(inputDisplayName, inputParameterName));
return IncomeEarnedBy.None;
}
}
private static ValidationError GetNoValueError(string inputDisplayName, string inputParameterName)
{
return GetError("No value provided for {0}", inputDisplayName, inputParameterName);
}
private static ValidationError GetConversionError(string inputDisplayName, string inputParameterName)
{
return GetError("Invalid value for {0}", inputDisplayName, inputParameterName);
}
private static ValidationError GetMoneyMaximumError(string inputDisplayName, string inputParameterName)
{
return GetError("{0} must be less than $999,999,999.00", inputDisplayName, inputParameterName);
}
private static ValidationError GetMoneyMinimumError(string inputDisplayName, string inputParameterName)
{
return GetError("{0} must be more than -$999,999,999.00", inputDisplayName, inputParameterName);
}
private static ValidationError GetPositiveError(string inputDisplayName, string inputParameterName)
{
return GetError("{0} must be a positive number", inputDisplayName, inputParameterName);
}
private static ValidationError GetNonZeroError(string inputDisplayName, string inputParameterName)
{
return GetError("{0} must be greater than zero", inputDisplayName, inputParameterName);
}
private static ValidationError GetError(string errorMessage, string inputDisplayName, string inputParameterName)
{
if (String.IsNullOrEmpty(errorMessage))
{
throw new ArgumentException("No error message provided");
}
string message = String.Format(errorMessage, inputDisplayName);
ValidationError error = new ValidationError(inputParameterName, message);
return error;
}
private void ValidateInputInfo(string inputDisplayName, string inputParameterName)
{
if (String.IsNullOrEmpty(inputDisplayName))
{
throw new ArgumentException("No input display name provided");
}
if (String.IsNullOrEmpty(inputParameterName))
{
throw new ArgumentException("No input parameter name provided");
}
}
}
}