November 03, 2007

Using Enterprise Library Validation attributes

Class

Purpose

Description/Example

NotNullValidator

Checks to ensure a supplied value is not null.

[NotNullValidator(Ruleset = "Default", MessageTemplateResourceName = "EMPLOYEE_ERR_EMPLOYEE_CODE_NULL", MessageTemplateResourceType = typeof(Properties.MyValidation))]

public virtual string EmployeeCode {

  1. You can use it to check null string or null object (entity)

  2. Advisable to use it with ValidatorComposition

ContainsCharactersValidator

Checks a string for the presence of the characters specified in the CharacterSet property.

(e.g. does not contain any of /\?<>”:)

StringLengthValidator

Ensures that the length of a string is within specified limits.

(e.g. string is at least 8 characters long)

If business entity is generating any non localized string then you have to replace it with following constant:

GLOBAL_VALIDATION_ERROR_STRING_LENGTH

And the associated message for US culture will be

"\"{0}\" must be between {3} and {5} characters"

Example – output for null Employee Code will be:

EmployeeCode must be between 1 and 25 characters

Note with {0} it displays property name as-is e.g. EmployeeCode without space in between. If you want property name specific then you need to add constant accordingly in your application resource file.

E.g.

MY_VALIDATION_ERROR_EMPLOYEE_CODE_STRING_LENGTH

And message for US culture will be -

Employee Code must be between 1 and 25 characters

RangeValidator

Checks whether a value falls within a specified range.

(e.g. must be from 10-20 or 1/1/1950 to 12/31/1999)

DateTimeRangeValidator

Lets you check whether a supplied DateTime object falls within a specified range.

RegexValidator

Checks whether a value matches the pattern specified by a regular expression.

(e.g. value is a valid e-mail address)

[RegexValidator(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", MessageTemplate = "Invalid Email.", Ruleset = "Default")]

public string EmailAddress {

RelativeDateTimeValidator

Checks whether a value falls within a specified date/time range.

(e.g. birth date is more than 18 years ago)

// Check for 18

[RelativeDateTimeValidator(18, DateTimeUnit.Year)]

Or

// check for 18 to 30

[RelativeDateTimeValidator(18, DateTimeUnit.Year, 30, DateTimeUnit.Year)]

EnumConversionValidator

Checks whether a string can be converted to an enum.

(e.g. string can be converted to a value in the Color enum type)

PropertyComparisonValidator

Compares the value of a property with another property.

(e.g. StartDate < EndDate)

In the below example the validation will succeed if the StartDate is less than the EndDate OR greater than DOB.

[ValidatorComposition(CompositionType.Or)]

[PropertyComparisonValidator("EndDate", ComparisonOperator.LessThan)]

[PropertyComparisonValidator("DOB", ComparisonOperator.GreaterThan)]

public DateTime StartDate {

get { return m_StartDate; }

set { m_StartDate = value; }

}

TypeConversionValidator

Checks whether a string can be converted to a specific type.

(e.g. string can be converted to a DateTime)

[DataMember]

private string mydate; [TypeConversionValidator(typeof(DateTime),MessageTemplate="Not a valid date",Ruleset="Default")]

public string MyDate {

DomainValidator

Checks whether a value matches one of a list of supplied values.

(e.g. must be one of state from {OH, MI, NY, IL})

ObjectValidator

Validates an object reference.

ValidatorComposition

Allows you to create a composite validator by combining one or many validators. You can specify either "And" or "Or" type validation through CompositionType enumeration.

In the below example the validation will succeed if the StartDate is less than the EndDate OR greater than DOB.

[ValidatorComposition(CompositionType.Or)]

[PropertyComparisonValidator("EndDate", ComparisonOperator.LessThan)]

[PropertyComparisonValidator("DOB", ComparisonOperator.GreaterThan)]

public DateTime StartDate {

get { return m_StartDate; }

set { m_StartDate = value; }

}

ObjectCollectionValidator

Ensures that an object is a collection of a given type, and invokes validation on each element in the collection.

§ All validation rules can be negated

E.g String Length must not be between 5 and 10 characters

[StringLengthValidator(5, 10, Ruleset = "Default", MessageTemplate = "Validation Failed",Negated=true)]

public virtual string SomeProp {