Share via


Domain 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: DomainValidator<T>

Attribute Name: DomainValidatorAttribute

Configuration tool name: Domain Validator

Description

This validator checks that a value is one of the specified values in a specified set. For example, it can check that a name is "Tom," "Dick," "Harry," or "George" or that an integer is 2, 3, 5, 7, or 11. If the set only contains one value, you can use this validator to check for equality.

Properties

The following table lists the Domain Validator properties.

Property

Description

Domain

The set of values that specifies the acceptable elements. The value that you want to validate must be of the same type as the values in the list. The compiler checks this requirement if you directly invoke the validator in your code. If you use attributes, the application block throws an exception at run time if the type is incorrect. If you use configuration, the configuration tool displays an error if you enter an incorrect type. This value is required.

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.

Negated

This is a Boolean property. If it is set to True, it changes the validator's behavior so that it will fail if the condition is met instead of when it is not met. The default is False.

Tag

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

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 Domain 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 code example shows how to use the validator with attributes to ensure that a value is within a specified list of values.

public class Product
{
  [DomainValidator("each", "dozen", "gross")]
  public string SalesUnit
  {
    get
    {
      return salesUnit;
    }
  }
  // ...
}
'Usage
Public Class Product
  <DomainValidator("each", "dozen", "gross")> _
  ReadOnly Property SalesUnit(ByVal _salesUnit As String)
    Get
      Return _salesUnit
    End Get
  End Property
End Class