Understanding Validators

The Validation Application Block provides several validation classes, which inherit from the abstract Validator class and these are called Validators. Each Validator class is associated with a specific data type; the Validator validates whether the input is valid or not. Validators can be associated with data types in several ways; association can be made through configuration, attributes, a combination of configuration, and attributes, and using self-validation. They can also be instantiated within our code without associating them with a data type. The concrete implementation of the Validator class holds the validation logic; the block also provides Validator<T>, which is a generic abstract class to validate the type represented by T. The Validation Application Block provides the following Validators.

Value Validators

Value Validators as the name suggests perform validations on the value of their respective data type. These are implemented using the abstract class ValueValidator<T>.

Validator Class

Description

StringLengthValidator

The StringLengthValidator class checks whether the length of the string is within the specified lower and upper bound range.

ContainsCharactersValidator

The ContainsCharactersValidator class checks whether an arbitrary string input contains any or all of the characters specified by the CharacterSet property.

DateTimeRangeValidator

DateTimeRangeValidator class validates whether a DateTime object is within the specified lower and upper bound range.

DomainValidator<T>

DomainValidator<T> class checks whether the input value is one of the specified values in the set of acceptable values specified as part of the Domain property.

EnumConversionValidator

EnumConversionValidator class checks whether the input string can be converted to a value of the enum type specified in the EnumType property.

NotNullValidator

NotNullValidator class checks that the value is not null.

PropertyComparisonValidator

PropertyComparisonValidator class compares the value to be verified with the value of the property on the target object property (PropertyToCompare) using the specified comparison operator (ComparisonOperator).

RangeValidator<T>

RangeValidator<T> class checks whether the value is within the specified lower and upper bound range. This generic implementation can be used with any type implementing the IComparable interface.

RegexValidator

RegexValidator class validates whether the value matches the pattern specified by a regular expression using System.Text.RegularExpressions.Regex.

RelativeDateTimeValidator

RelativeDateTimeValidator class verifies whether the DateTime value is within the specified lower and upper bound range using relative times and dates. Additionally the LowerUnit and UpperUnit properties set the unit of time for the respective lower and upper boundaries.

TypeConversionValidator

TypeConversionValidator class validates whether the input value string can be converted to the target type specified in the TargetType property.

Object Validators

Object Validators performs validations on an object reference. Object Validator and Object Collection Validator fall under this category.

Validator Class

Description

ObjectValidator

ObjectValidator class invokes all Validators defined for the object's type and causes validation to occur on an object reference. Validation is ignored if the object is null (C#) or Nothing (Visual Basic).

ObjectCollectionValidator

ObjectCollectionValidator class verifies whether the object is a collection of the specified type; validation is invoked for each object in the collection using the defined Validators.

Single Member Validators

Instead of validating the entire data type using the defined Validators, Single Member Validators gives us the flexibility to validate the individual members of types. The Validation Application Block provides three different Validators: FieldValueValidator, MethodReturnValueValidator, and PropertyValueValidator.

Validator Class

Description

FieldValueValidator<T>

FieldValueValidator<T> class provides the flexibility to validate a field of a type; the constructor accepts a field name and the Validator to validate the value of the field.

MethodReturnValueValidator<T>

MethodReturnValueValidator<T> class is similar to FieldValueValidator, instead of validating the field this validator accepts a method name and the Validator instance to validate the return value. MethodReturnValueValidator invokes the method and performs validation of the return value using the specified Validator.

PropertyValueValidator<T>

PropertyValueValidator<T> class validates the value of the specified property of a type; the constructor signature is same as for other Single Member Validators. This validator accepts a property name and the Validator type to be used to validate the value of the property.

Composite Validators

Composite Validators provide the flexibility to combine multiple Validators. This category consists of "And" and "Or" Composite Validators.

Validator Class

Description

AndCompositeValidator

The AndCompositeValidator class performs validation on all the specified Validators; only if all the Validators are valid will the outcome be valid. The constructor accepts a variable number of Validator objects as parameters.

OrCompositeValidator

Similar to AndCompositeValidator the OrCompositeValidator class also performs validation of all the specified Validators.

The following diagram lists the available Validator classes and the inheritance hierarchy:

Composite Validators
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.237.131