Thursday, December 25, 2008

Enterprise Library VAB Validators (Part 1 of 2)

If you've been following my blog this week, you've probably read the post I made earlier about using the Microsoft Enterprise Library Validation Enterprise Block (VAB). I continue from where I left off by discussing the different types of validators available as a part of the VAB.

The different validators available with VAB are:

  • Not Null

  • Domain

  • String Length

  • Date Range

  • Contains Char

  • Range

  • Regular Expression

  • Property Comparison

  • Enum Conversion

  • Type Conversion

  • Relative Date Time

  • Object

  • Object Collection

  • Or Composite

  • And Composite



Let's start off by looking at the common attributes for each of the validators viz. Type, messageTemplate, lowerBound, lowerBoundType, upperBound, upperBoundType

Type

All of the validators have a type attribute which indicates which validator you are defining (Eg. NotNullValidator).

messageTemplate

A messageTemplate defines the validation message to be returned when validation fails. You can use tokens ({0}, {1}, ...) as placeholders, but the values represented by the tokens are validator-specific.

lowerBound, upperBound

These attributes define the minimum and maximum permissible values for range validators (Range, Date Range, Relative Date Time etc)

lowerBoundType, upperBoundType

The lowerBoundType and the upperBoundType indicate whether the value for the lowerBound and upperBound should be ignored (used when you only want to specify one of the two - either the minimum or the maximum value) and if a value is valid if it is equal to the boundary value (inclusive or exclusive).

negated

The negated property is used to turn a validator around so it says the data is invalid when the condition is true and vice-versa.


Now, moving on to the each of the 15 validators listed above.

Not Null Validator

The Not Null Validator can be used to ensure that an object reference is not null. You can't use this validator with a regular value type but you can use this with nullable value types.

Example:
<validator messageTemplate="validation error" type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.NotNullValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Not Null Validator" />

Domain Validator

The Domain Validator can be used to limit the values of a member to a list of values. If a member is null or un-initialized, the validator indicates that the data is invalid. To allow un-initialized (non-nullable) value types, you can simply use the default value for the value type (Eg. 0 for int).

Example:
<validator messageTemplate="" type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.DomainValidator`1[[System.Object, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], Microsoft.Practices.EnterpriseLibrary.Validation, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Domain Validator">
<domain>
<add name="DXB" />
<add name="SHJ" />
</domain>
</validator>

Notice the use of generics with the DomainValidator - the Enterprise Library Configuration tool simply uses the Object type.

String Length Validator

The String Length Validator, as the name indicates, validates a string to ensure that its length is within a specified range. If a string is null, it is invalid even if the lowerBound is set to 0 (inclusive lowerBountType).

Example:
<validator lowerBound="3" lowerBoundType="Inclusive" upperBound="3" upperBoundType="Inclusive" messageTemplate="" type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.StringLengthValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="String Length Validator" />

Date Range Validator

The Date Range Validator checks if a date field/member/method is within a specified date range.

Example:
<validator lowerBound="1950-01-01" lowerBoundType="Inclusive" upperBound="2000-12-31" upperBoundType="Inclusive" messageTemplate="" type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.DateTimeRangeValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Date Range Validator" />

Contains Char Validator

The Contains Char Validator checks if a string contains a particular character.

Example:
<validator characterSet="@" containsCharacter="Any" messageTemplate="" type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.ContainsCharactersValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Contains Characters Validator" />

Range Validator

The Range Validator is used to check if a numeric type is within the specified range.

Example:
<validator lowerBound="0" lowerBoundType="Exclusive" upperBound="" upperBoundType="Ignore" messageTemplate="" type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.RangeValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Range Validator" />

Regular Expression Validator

The Regular Expression Validator checks if a string matches the specified pattern.

Example:
<validator pattern="[A-Z0-9 ,\.]" options="IgnoreCase" messageTemplate="Upper case letters, numbers, comma and period allowed for address" type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.RegexValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Regex Validator" />

(Contd. in Part 2 of 2)

2 comments:

Nitin Reddy Katkam said...

Does anyone know if a NULL can be added into the list of values for a Domain Validator?

Nitin Reddy Katkam said...

I haven't verified whether the RangeValidator supports float and double, but I did get an exception.

I'll try to verify if the RangeValidator supports numeric types other than int (Int32) by looking up the source code, as the documentation doesn't seem to mention about the supported data types. Sometime later, I'll look up the source code to investigate further and will post my results.