Share via


Object Collection Validator

Class Name: ObjectCollectionValidator

Attribute Name: ObjectCollectionValidatorAttribute

Configuration tool name: Object Collection Validator

Description

This validator checks that the object is a collection of the specified type and then invokes validation on each element of the collection. If the object you want to validate is null, the validation is ignored. If the object you want to validate is not a collection, then the validation fails and the rule set is not applied. If there are elements in the collection that are of a different type than the one you specified for the object, then the validation for these elements fails but this does not affect validation for the other elements.

Properties

The following table lists the object collection 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 each element in the collection.

TargetType

Target Type - This property is the type of the object that you want to validate. You can either enter the type or you can select it with the Type Selector – System.Object dialog box.

TypeName

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

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 collection 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 collection validator with attributes to check that each part in a kit's part list meets the requirements of the Part type's validator. The Part type's validator checks that the name is fewer than 20 characters and that the manufacturer's 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 Kit
{
  [ObjectCollectionValidator(typeof(Part))]
  public Part[] PartList
  {
    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 Kit
  <ObjectCollectionValidator(GetType(Part))> _
  ReadOnly Property PartList(ByVal _partList As String)
    Get
      Return _partList
    End Get
  End Property
  ' ...
End Class

Note

When validating a collection, the object collection validator is inheritance-aware. However, if you specify the target type, an exception is raised if the collection is recursive. To resolve this, do not specify the target object type for the object collection validator. At run time, the validator will determine the appropriate type. This will incur a minor performance penalty.