What is the difference between objectToValidate and currentTarget?

Topics: Building and extending application blocks, General discussion, Validation Application Block
Sep 8, 2009 at 5:25 PM


I am looking in the source of the Validator class.  It has the public method Validate which looks like this (my emphasis):

     public void Validate(object target, ValidationResults validationResults)
if (null == validationResults)
throw new ArgumentNullException("validationResults");

DoValidate(target, target, null, validationResults);

The target object (the thing you want to validate) is passed on to DoValidate as both objectToValidate and currentTarget (as per the DoValidate signature below):

		public abstract void DoValidate(object objectToValidate,
object currentTarget,
string key,
ValidationResults validationResults);

Why is the same object passed twice?  I have not yet seen any obvious differential use in the implemented validators.  However, this must be for use some specific scenario(s).  Can anyone explain these scenarios or point me to some docs/web pages?




Sep 9, 2009 at 6:32 AM
Edited Sep 9, 2009 at 6:34 AM


I'm not really sure about this one. But as far as I've debugged, One difference that I've saw, is that the objectToValidate parameter holds the actual object that is being validated. While the currentTarget holds the object or instance wherein the objectToValidate value belongs. To explain it further, Consider a scenario that you are validating a class called "Person"(see below).

public class Person
    [StringLengthValidator(1, RangeBoundaryType.Inclusive, 20, RangeBoundaryType.Inclusive,
        MessageTemplate="Invalid Length")]
    public string Name { get; set; }
    [RangeValidator(18, RangeBoundaryType.Inclusive, 99, RangeBoundaryType.Inclusive, MessageTemplate="Invalid Age")]
    public int Age { get; set; }

As the VAB executes all the validation against the target,  the objectToValidate may contain the value for the property Name but the currentTarget will hold the instance of the Person class as reference (used in the ValidationResult).

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.

Sep 10, 2009 at 4:12 PM

Hi Valiant,

Thanks for the response.  I bit the bullet and wrote some tests against the VAB source to see this for myself and it all makes sense now.  When validating a property of a type (e.g. Name of Person) your validators (like NotNullValidator) are actually contained within an EntLib ValueAccessValidator.  This ValueAccessValidator is called with the Person instance as objectToValidate and currentTarget.  However, it then uses reflection to get the property value and then call the actual validator with the property value as objectToValidate and the Person instance as currentTarget. 

I have to say that the call to DoValidate from the ValueAccessValidator is a bit confusing as it passes objectToValidate as currentTarget like this:


this.valueValidator.DoValidate(value, objectToValidate, this.valueAccess.Key, validationResults);