Jaa


Object Validator

Class Name: ObjectValidator

Attribute Name: ObjectValidatorAttribute

Configuration tool name: Object Validator

Description

This validator causes validation to occur on an object reference. All validators defined for the object's type will be invoked, just as if the Validation.Validate method had been called on the object. If the object you want to validate is null, the validation is ignored. If the reference is to an instance of a type that is not compatible with the configured target's type, the validation fails. This validator is helpful for validating tree-structured data.

Properties

The following table lists the object validator properties. The actual property names shown in the configuration tools are listed in the table description.

Property

Description

MessageTemplate

Message Template - This property is a string containing template tokens that the validator replaces with values as it validates the target. Typically, it describes the validation result.

MessageTemplateResourceName

Template Resource Name - If you do not want to use the MessageTemplate property to hard-code a message template (perhaps for internationalization), you can use a template stored in the application resources. You must also specify a MessageTemplateResourceType value. If you include both a MessageTemplate value and a MessageTemeplateResourceName value, the MessageTemplate value takes precedence.

MessageTemplateResourceType

Template Resource Type - The resource type for the template you want to use. If you specify a MessageTemplateResourceName value, you must specify this value.

Name

Name – The name to use for this validator.

Tag

This property is a user-supplied string. Typically, it is used to sort or categorize validation results.

TargetRuleset

Target Ruleset This is the name of the rule set that will be applied to the object.

TypeName

Type Name – The fully qualified name of the type configuration element. This property cannot be edited.

ValidateActualType

Validate Actual Type- Indicates whether to validate based on the static type or the actual type.

Message Template Tokens.

If the message template contains tokens (for example, "{0}"), the validator will replace these tokens with values when the ValidationResult is created. The tokens supported by the object validator are listed in the following table.

Token

Meaning

{0}

This token represents the value of the object that is being validated. Although it can be useful to show the original value as a part of the validation message, you must be careful to avoid injection attacks by escaping any characters that can be used to attack the system that conveys the message to the user.

{1}

This token represents the key of the object that is being validated. When the validator is attached to a member of a type such as a property or a field, the key is set to the member name. When the validator is attached to an object, the key is null and the token is replaced by an empty string.

{2}

This token represents the tag that is specified on the validator instance. If no tag is supplied, the token is replaced by an empty string.

Example

The following example shows how to use the object validator with attributes to check that the Part object that is returned by the OrderedPart property meets the requirements of the Part class validator. The Part class validator checks that the name is fewer than 20 characters and that the manufacturer URL is well formed according to a particular regular expression pattern.

public class Part
{
  [StringLengthValidator(20)]
  string name;
  [RegexValidator(@"http://(www\.)?([^\.]+)\.com")]
  string manufacturerUrl;
  // ...
}

public class OrderLineItem
{
  [NotNullValidator]
  [ObjectValidator]
  public Part OrderedPart
  {
    get
    {
      return partList;
    }
  }
 // ...
}
'Usage
Public Class Part
  <StringLengthValidator(20)> _
  Dim name As String
  <RegexValidator("http://(www\.)?([^\.]+)\.com")> _
  Dim manufacturerUrl As String
  ' ...
End Class

Public Class OrderLineItem
  <NotNullValidator()> _
  <ObjectValidator()> _
  ReadOnly Property OrderedPart(ByVal partList As String)
    Get
      Return partList
    End Get
  End Property
  ' ...
End Class

Note

When validating a collection, the object validator and object collection validator are inheritance-aware. However, if the collection is recursive the object validator will raise an exception. To resolve this, set the ValidateActualType named parameter of the object validator attribute to true.

Differences between the Object Validator and the Factory-Created Validators

You can use an object validator instead of creating a validator for a type using the CreateValidator method of the ValidatorFactory. While the two approaches achieve the same result, there are some differences in their behavior:

  • If you do not specify a target type when you create an object validator programmatically, you can use it to validate any type. When you call the Validate method, you specify the target instance, and the object validator creates a type-specific validator for the type of the target instance. In contrast, the validator you obtain from a factory can only be used to validate instances of the type you specify when you obtain the validator. However, it can also be used to validate subclasses of the specified type, but it will use the rules defined for the specified target type.
  • The object validator will always use rules in configuration for the type of the target object, and attributes and self validation methods within the target instance. In contrast, you can use a specific factory class type to obtain validators that only validate the target instance using one type of rule source (in other words, just configuration rule sets, or just one type of attributes).
  • The object validator will acquire a type specific validator of the appropriate type each time you call the Validate method, even if you use the same instance of the object validator every time. In contrast, a validator obtained from one of the factory classes does not need to do this, and will offer improved performance.

You should generally consider using the factory approach for creating validators to validate objects to benefit from the increased flexibility and performance.

Note

When you use an object validator attribute within a class to validate a member such as a property, the validator will, by default, use the rules defined for the type of that property. However, if the property is defined as an interface or base type, you can specify that the object validator use the rules defined for the actual concrete type that the property contains by setting the ValidateActualType property of the attribute as shown here: [ObjectValidator(ValidateActualType = true)]