Validating the Key of a KeyValuePair property

Topics: Validation Application Block
May 9, 2012 at 6:19 PM

Hello, I have an object with a Nullable<KeyValuePair<Guid?, string>> property

public class Company
    public KeyValuePair<Guid?, string>? State { get; set; }

and I would like to apply the following validation:

  • The whole property is not null (this is not a problem)
  • The key of the KeyValuePair is not null

I can't use attributes or self validation, all the configuration should be handled through .config file

For the second item it seems I need to set up a ruleset applied to the KeyValuePair <S, T> type:

  • ValidatedType: KeyValuePair <Guid?, string>
  • ValidationTarget: Property (Key)
  • Not Null validator, etc

I was able to set up the validation on code:

NotNullValidator notNullValidator = new NotNullValidator();

Validator stateNotNullValidator = new PropertyValueValidator<Company>("State", notNullValidator);
Validator stateKeyNotNullValidator = new PropertyValueValidator<KeyValuePair<Guid, string>>("Key", notNullValidator);

Validator andValidator = new AndCompositeValidator(stateNotNullValidator, stateKeyNotNullValidator);

ValidationResults result = stateNotNullValidator.Validate(companyEntity);

But when I try to do the same with config app, I cannot select the KeyValuePair struct from the System.Collections.Generic namespace.

How can I do that? Would a custom validation be a better approach? if so, please point me in the right direction.

Thanks in advance for any help. 

May 10, 2012 at 5:21 AM

The PropertyValueValidator is not available for use in the configuration file.  If you must use configuration probably the easiest way to achieve what you want would be through a custom validator.

Randy Levy
Enterprise Library support engineer