Skip to content

Commit

Permalink
clean up the notes about ObjectInstance
Browse files Browse the repository at this point in the history
  • Loading branch information
tgharold committed Mar 10, 2020
1 parent 632377c commit 7dc63b9
Showing 1 changed file with 21 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,14 @@ namespace RecursiveDataAnnotationsValidation
{
public class RecursiveDataAnnotationValidator : IRecursiveDataAnnotationValidator
{
/* Notes:
*
* Background information of why we don't use ValidationContext.ObjectInstance here, even though it is tempting.
*
* https://jeffhandley.com/2009-10-17/validator
*
* It’s important to note that for cross-field validation, relying on the ObjectInstance comes with a caveat.
* It’s possible that the end user has entered a value for a property that could not be set—for instance
* specifying “ABC” for a numeric field. In cases like that, asking the instance for that numeric property
* will of course not give you the “ABC” value that the user has entered, thus the object’s other properties
* are in an indeterminate state. But even so, we’ve found that it’s extremely valuable to provide this object
* instance to the validation attributes.
*
* See also:
*
* https://github.com/dotnet/corefx/blob/8b04d0a18a49448ff7c8ee63239cd6d2a2be7e14/src/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/ValidationContext.cs
*
*/

public bool TryValidateObjectRecursive(
object obj,
object obj, // see Note 1
ValidationContext validationContext,
List<ValidationResult> validationResults
)
{
return TryValidateObjectRecursive(
obj,
obj,
validationResults,
validationContext.Items
);
Expand Down Expand Up @@ -160,5 +141,24 @@ private bool TryValidateObjectRecursive(

return result;
}

/* Note 1:
*
* Background information of why we don't use ValidationContext.ObjectInstance here, even though it is tempting.
*
* https://jeffhandley.com/2009-10-17/validator
*
* It’s important to note that for cross-field validation, relying on the ObjectInstance comes with a caveat.
* It’s possible that the end user has entered a value for a property that could not be set—for instance
* specifying “ABC” for a numeric field. In cases like that, asking the instance for that numeric property
* will of course not give you the “ABC” value that the user has entered, thus the object’s other properties
* are in an indeterminate state. But even so, we’ve found that it’s extremely valuable to provide this object
* instance to the validation attributes.
*
* See also:
*
* https://github.com/dotnet/corefx/blob/8b04d0a18a49448ff7c8ee63239cd6d2a2be7e14/src/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/ValidationContext.cs
*
*/
}
}

0 comments on commit 7dc63b9

Please sign in to comment.