Object Validator
Retired Content |
---|
This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist. |
The latest Enterprise Library information can be found at the Enterprise Library site. |
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.
Property |
Description |
---|---|
MessageTemplate |
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 |
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 MessageTemplateResourceTypeName value. If you include both a MessageTemplate value and a MessageTemeplateResourceName value, the MessageTemplate value takes precedence. |
MessageTemplateResourceTypeName |
The resource type for the template you want to use. If you specify a MessageTemplateResourceName value, you must specify this value. |
Tag |
This property is a user-supplied string. Typically, it is used to sort or categorize validation results. |
TargetRuleset |
This is the name of the rule set that will be applied to the object. |
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