/
ValidationAttribute.cs
490 lines (441 loc) · 22.9 KB
/
ValidationAttribute.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;
namespace System.ComponentModel.DataAnnotations
{
/// <summary>
/// Base class for all validation attributes.
/// <para>Override <see cref="IsValid(object, ValidationContext)" /> to implement validation logic.</para>
/// </summary>
/// <remarks>
/// The properties <see cref="ErrorMessageResourceType" /> and <see cref="ErrorMessageResourceName" /> are used to
/// provide
/// a localized error message, but they cannot be set if <see cref="ErrorMessage" /> is also used to provide a
/// non-localized
/// error message.
/// </remarks>
public abstract class ValidationAttribute : Attribute
{
#region Member Fields
private string? _errorMessage;
private Func<string>? _errorMessageResourceAccessor;
private string? _errorMessageResourceName;
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)]
private Type? _errorMessageResourceType;
private volatile bool _hasBaseIsValid;
private string? _defaultErrorMessage;
#endregion
#region All Constructors
/// <summary>
/// Default constructor for any validation attribute.
/// </summary>
/// <remarks>
/// This constructor chooses a very generic validation error message.
/// Developers subclassing ValidationAttribute should use other constructors
/// or supply a better message.
/// </remarks>
protected ValidationAttribute()
: this(() => SR.ValidationAttribute_ValidationError)
{
}
/// <summary>
/// Constructor that accepts a fixed validation error message.
/// </summary>
/// <param name="errorMessage">A non-localized error message to use in <see cref="ErrorMessageString" />.</param>
protected ValidationAttribute(string errorMessage)
: this(() => errorMessage)
{
}
/// <summary>
/// Allows for providing a resource accessor function that will be used by the <see cref="ErrorMessageString" />
/// property to retrieve the error message. An example would be to have something like
/// CustomAttribute() : base( () => MyResources.MyErrorMessage ) {}.
/// </summary>
/// <param name="errorMessageAccessor">The <see cref="Func{T}" /> that will return an error message.</param>
protected ValidationAttribute(Func<string> errorMessageAccessor)
{
// If null, will later be exposed as lack of error message to be able to construct accessor
_errorMessageResourceAccessor = errorMessageAccessor;
}
#endregion
#region Internal Properties
/// <summary>
/// Sets the default error message string.
/// This message will be used if the user has not set <see cref="ErrorMessage"/>
/// or the <see cref="ErrorMessageResourceType"/> and <see cref="ErrorMessageResourceName"/> pair.
/// This property was added after the public contract for DataAnnotations was created.
/// It is internal to avoid changing the DataAnnotations contract.
/// </summary>
internal string? DefaultErrorMessage
{
set
{
_defaultErrorMessage = value;
_errorMessageResourceAccessor = null;
CustomErrorMessageSet = true;
}
}
#endregion
#region Protected Properties
/// <summary>
/// Gets the localized error message string, coming either from <see cref="ErrorMessage" />, or from evaluating the
/// <see cref="ErrorMessageResourceType" /> and <see cref="ErrorMessageResourceName" /> pair.
/// </summary>
protected string ErrorMessageString
{
get
{
SetupResourceAccessor();
return _errorMessageResourceAccessor!();
}
}
/// <summary>
/// A flag indicating whether a developer has customized the attribute's error message by setting any one of
/// ErrorMessage, ErrorMessageResourceName, ErrorMessageResourceType or DefaultErrorMessage.
/// </summary>
internal bool CustomErrorMessageSet { get; private set; }
/// <summary>
/// A flag indicating that the attribute requires a non-null
/// <see cref="ValidationContext" /> to perform validation.
/// Base class returns false. Override in child classes as appropriate.
/// </summary>
public virtual bool RequiresValidationContext => false;
#endregion
#region Public Properties
/// <summary>
/// Gets or sets the explicit error message string.
/// </summary>
/// <value>
/// This property is intended to be used for non-localizable error messages. Use
/// <see cref="ErrorMessageResourceType" /> and <see cref="ErrorMessageResourceName" /> for localizable error messages.
/// </value>
public string? ErrorMessage
{
// If _errorMessage is not set, return the default. This is done to preserve
// behavior prior to the fix where ErrorMessage showed the non-null message to use.
get => _errorMessage ?? _defaultErrorMessage;
set
{
_errorMessage = value;
_errorMessageResourceAccessor = null;
CustomErrorMessageSet = true;
// Explicitly setting ErrorMessage also sets DefaultErrorMessage if null.
// This prevents subsequent read of ErrorMessage from returning default.
if (value == null)
{
_defaultErrorMessage = null;
}
}
}
/// <summary>
/// Gets or sets the resource name (property name) to use as the key for lookups on the resource type.
/// </summary>
/// <value>
/// Use this property to set the name of the property within <see cref="ErrorMessageResourceType" />
/// that will provide a localized error message. Use <see cref="ErrorMessage" /> for non-localized error messages.
/// </value>
public string? ErrorMessageResourceName
{
get => _errorMessageResourceName;
set
{
_errorMessageResourceName = value;
_errorMessageResourceAccessor = null;
CustomErrorMessageSet = true;
}
}
/// <summary>
/// Gets or sets the resource type to use for error message lookups.
/// </summary>
/// <value>
/// Use this property only in conjunction with <see cref="ErrorMessageResourceName" />. They are
/// used together to retrieve localized error messages at runtime.
/// <para>
/// Use <see cref="ErrorMessage" /> instead of this pair if error messages are not localized.
/// </para>
/// </value>
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)]
public Type? ErrorMessageResourceType
{
get => _errorMessageResourceType;
set
{
_errorMessageResourceType = value;
_errorMessageResourceAccessor = null;
CustomErrorMessageSet = true;
}
}
#endregion
#region Private Methods
/// <summary>
/// Validates the configuration of this attribute and sets up the appropriate error string accessor.
/// This method bypasses all verification once the ResourceAccessor has been set.
/// </summary>
/// <exception cref="InvalidOperationException"> is thrown if the current attribute is malformed.</exception>
private void SetupResourceAccessor()
{
if (_errorMessageResourceAccessor == null)
{
string? localErrorMessage = ErrorMessage;
bool resourceNameSet = !string.IsNullOrEmpty(_errorMessageResourceName);
bool errorMessageSet = !string.IsNullOrEmpty(_errorMessage);
bool resourceTypeSet = _errorMessageResourceType != null;
bool defaultMessageSet = !string.IsNullOrEmpty(_defaultErrorMessage);
// The following combinations are illegal and throw InvalidOperationException:
// 1) Both ErrorMessage and ErrorMessageResourceName are set, or
// 2) None of ErrorMessage, ErrorMessageResourceName, and DefaultErrorMessage are set.
if ((resourceNameSet && errorMessageSet) || !(resourceNameSet || errorMessageSet || defaultMessageSet))
{
throw new InvalidOperationException(
SR.ValidationAttribute_Cannot_Set_ErrorMessage_And_Resource);
}
// Must set both or neither of ErrorMessageResourceType and ErrorMessageResourceName
if (resourceTypeSet != resourceNameSet)
{
throw new InvalidOperationException(
SR.ValidationAttribute_NeedBothResourceTypeAndResourceName);
}
// If set resource type (and we know resource name too), then go setup the accessor
if (resourceNameSet)
{
SetResourceAccessorByPropertyLookup();
}
else
{
// Here if not using resource type/name -- the accessor is just the error message string,
// which we know is not empty to have gotten this far.
// We captured error message to local in case it changes before accessor runs
_errorMessageResourceAccessor = () => localErrorMessage!;
}
}
}
private void SetResourceAccessorByPropertyLookup()
{
Debug.Assert(_errorMessageResourceType != null);
Debug.Assert(!string.IsNullOrEmpty(_errorMessageResourceName));
var property = _errorMessageResourceType
.GetProperty(_errorMessageResourceName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.DeclaredOnly);
if (property != null)
{
var propertyGetter = property.GetMethod;
// We only support internal and public properties
if (propertyGetter == null || (!propertyGetter.IsAssembly && !propertyGetter.IsPublic))
{
// Set the property to null so the exception is thrown as if the property wasn't found
property = null;
}
}
if (property == null)
{
throw new InvalidOperationException(SR.Format(SR.ValidationAttribute_ResourceTypeDoesNotHaveProperty,
_errorMessageResourceType.FullName,
_errorMessageResourceName));
}
if (property.PropertyType != typeof(string))
{
throw new InvalidOperationException(SR.Format(SR.ValidationAttribute_ResourcePropertyNotStringType,
property.Name,
_errorMessageResourceType.FullName));
}
_errorMessageResourceAccessor = () => (string)property.GetValue(null, null)!;
}
#endregion
#region Protected & Public Methods
/// <summary>
/// Formats the error message to present to the user.
/// </summary>
/// <remarks>
/// The error message will be re-evaluated every time this function is called.
/// It applies the <paramref name="name" /> (for example, the name of a field) to the formatted error message, resulting
/// in something like "The field 'name' has an incorrect value".
/// <para>
/// Derived classes can override this method to customize how errors are generated.
/// </para>
/// <para>
/// The base class implementation will use <see cref="ErrorMessageString" /> to obtain a localized
/// error message from properties within the current attribute. If those have not been set, a generic
/// error message will be provided.
/// </para>
/// </remarks>
/// <param name="name">The user-visible name to include in the formatted message.</param>
/// <returns>The localized string describing the validation error</returns>
/// <exception cref="InvalidOperationException"> is thrown if the current attribute is malformed.</exception>
public virtual string FormatErrorMessage(string name) =>
string.Format(CultureInfo.CurrentCulture, ErrorMessageString, name);
/// <summary>
/// Gets the value indicating whether or not the specified <paramref name="value" /> is valid
/// with respect to the current validation attribute.
/// <para>
/// Derived classes should not override this method as it is only available for backwards compatibility.
/// Instead, implement <see cref="IsValid(object, ValidationContext)" />.
/// </para>
/// </summary>
/// <remarks>
/// The preferred public entry point for clients requesting validation is the <see cref="GetValidationResult" />
/// method.
/// </remarks>
/// <param name="value">The value to validate</param>
/// <returns><c>true</c> if the <paramref name="value" /> is acceptable, <c>false</c> if it is not acceptable</returns>
/// <exception cref="InvalidOperationException"> is thrown if the current attribute is malformed.</exception>
/// <exception cref="NotImplementedException">
/// is thrown when neither overload of IsValid has been implemented
/// by a derived class.
/// </exception>
public virtual bool IsValid(object? value)
{
if (!_hasBaseIsValid)
{
// track that this method overload has not been overridden.
_hasBaseIsValid = true;
}
// call overridden method.
// The IsValid method without a validationContext predates the one accepting the context.
// This is theoretically unreachable through normal use cases.
// Instead, the overload using validationContext should be called.
return IsValid(value, null!) == ValidationResult.Success;
}
/// <summary>
/// Protected virtual method to override and implement validation logic.
/// <para>
/// Derived classes should override this method instead of <see cref="IsValid(object)" />, which is deprecated.
/// </para>
/// </summary>
/// <param name="value">The value to validate.</param>
/// <param name="validationContext">
/// A <see cref="ValidationContext" /> instance that provides
/// context about the validation operation, such as the object and member being validated.
/// </param>
/// <returns>
/// When validation is valid, <see cref="ValidationResult.Success" />.
/// <para>
/// When validation is invalid, an instance of <see cref="ValidationResult" />.
/// </para>
/// </returns>
/// <exception cref="InvalidOperationException"> is thrown if the current attribute is malformed.</exception>
/// <exception cref="NotImplementedException">
/// is thrown when <see cref="IsValid(object, ValidationContext)" />
/// has not been implemented by a derived class.
/// </exception>
protected virtual ValidationResult? IsValid(object? value, ValidationContext validationContext)
{
if (_hasBaseIsValid)
{
// this means neither of the IsValid methods has been overridden, throw.
throw NotImplemented.ByDesignWithMessage(
SR.ValidationAttribute_IsValid_NotImplemented);
}
var result = ValidationResult.Success;
// call overridden method.
if (!IsValid(value))
{
string[]? memberNames = validationContext.MemberName is { } memberName
? new[] { memberName }
: null;
result = new ValidationResult(FormatErrorMessage(validationContext.DisplayName), memberNames);
}
return result;
}
/// <summary>
/// Tests whether the given <paramref name="value" /> is valid with respect to the current
/// validation attribute without throwing a <see cref="ValidationException" />
/// </summary>
/// <remarks>
/// If this method returns <see cref="ValidationResult.Success" />, then validation was successful, otherwise
/// an instance of <see cref="ValidationResult" /> will be returned with a guaranteed non-null
/// <see cref="ValidationResult.ErrorMessage" />.
/// </remarks>
/// <param name="value">The value to validate</param>
/// <param name="validationContext">
/// A <see cref="ValidationContext" /> instance that provides
/// context about the validation operation, such as the object and member being validated.
/// </param>
/// <returns>
/// When validation is valid, <see cref="ValidationResult.Success" />.
/// <para>
/// When validation is invalid, an instance of <see cref="ValidationResult" />.
/// </para>
/// </returns>
/// <exception cref="InvalidOperationException"> is thrown if the current attribute is malformed.</exception>
/// <exception cref="ArgumentNullException">When <paramref name="validationContext" /> is null.</exception>
/// <exception cref="NotImplementedException">
/// is thrown when <see cref="IsValid(object, ValidationContext)" />
/// has not been implemented by a derived class.
/// </exception>
public ValidationResult? GetValidationResult(object? value, ValidationContext validationContext)
{
ArgumentNullException.ThrowIfNull(validationContext);
var result = IsValid(value, validationContext);
// If validation fails, we want to ensure we have a ValidationResult that guarantees it has an ErrorMessage
if (result != null)
{
if (string.IsNullOrEmpty(result.ErrorMessage))
{
var errorMessage = FormatErrorMessage(validationContext.DisplayName);
result = new ValidationResult(errorMessage, result.MemberNames);
}
}
return result;
}
/// <summary>
/// Validates the specified <paramref name="value" /> and throws <see cref="ValidationException" /> if it is not.
/// <para>
/// The overloaded <see cref="Validate(object, ValidationContext)" /> is the recommended entry point as it
/// can provide additional context to the <see cref="ValidationAttribute" /> being validated.
/// </para>
/// </summary>
/// <remarks>
/// This base method invokes the <see cref="IsValid(object)" /> method to determine whether or not the
/// <paramref name="value" /> is acceptable. If <see cref="IsValid(object)" /> returns <c>false</c>, this base
/// method will invoke the <see cref="FormatErrorMessage" /> to obtain a localized message describing
/// the problem, and it will throw a <see cref="ValidationException" />
/// </remarks>
/// <param name="value">The value to validate</param>
/// <param name="name">The string to be included in the validation error message if <paramref name="value" /> is not valid</param>
/// <exception cref="ValidationException">
/// is thrown if <see cref="IsValid(object)" /> returns <c>false</c>.
/// </exception>
/// <exception cref="InvalidOperationException"> is thrown if the current attribute is malformed.</exception>
public void Validate(object? value, string name)
{
if (!IsValid(value))
{
throw new ValidationException(FormatErrorMessage(name), this, value);
}
}
/// <summary>
/// Validates the specified <paramref name="value" /> and throws <see cref="ValidationException" /> if it is not.
/// </summary>
/// <remarks>
/// This method invokes the <see cref="IsValid(object, ValidationContext)" /> method
/// to determine whether or not the <paramref name="value" /> is acceptable given the
/// <paramref name="validationContext" />.
/// If that method doesn't return <see cref="ValidationResult.Success" />, this base method will throw
/// a <see cref="ValidationException" /> containing the <see cref="ValidationResult" /> describing the problem.
/// </remarks>
/// <param name="value">The value to validate</param>
/// <param name="validationContext">Additional context that may be used for validation. It cannot be null.</param>
/// <exception cref="ValidationException">
/// is thrown if <see cref="IsValid(object, ValidationContext)" />
/// doesn't return <see cref="ValidationResult.Success" />.
/// </exception>
/// <exception cref="InvalidOperationException"> is thrown if the current attribute is malformed.</exception>
/// <exception cref="NotImplementedException">
/// is thrown when <see cref="IsValid(object, ValidationContext)" />
/// has not been implemented by a derived class.
/// </exception>
public void Validate(object? value, ValidationContext validationContext)
{
ArgumentNullException.ThrowIfNull(validationContext);
ValidationResult? result = GetValidationResult(value, validationContext);
if (result != null)
{
// Convenience -- if implementation did not fill in an error message,
throw new ValidationException(result, this, value);
}
}
#endregion
}
}