Validation always successful

Topics: Validation Application Block
Jun 4, 2010 at 3:11 PM


I'm using Validation.ValidateFromConfiguration(user, "NU2"); to validate business objects.

But the rule-set NU2 doesn't exists in the config. Yet this doesn't fail, no exception, even IsValid is set to true. I've dug into the code and noticed that if the rule-set is not found it returns emptyValidator.

I don't think it's correct behaviour, it should throw an exception in that case or at least invalidate object. Is there a way to determine if given rule-set exists for given type of object?




Jun 7, 2010 at 2:36 AM

You could traverse the rulesets in the config using the following code:

IConfigurationSource configSource = ConfigurationSourceFactory.Create();
ValidationSettings settings = (ValidationSettings)configSource.GetSection(ValidationSettings.SectionName);
foreach (ValidatedTypeReference type in settings.Types)
      if (type.Name == typeNameOfTheObjectToValidate)
           if (!type.Rulesets.Contains("ruleSet"))
               //logic if rule set does not exist

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.

Jun 7, 2010 at 2:44 PM

Brilliant! Thanks Sarah.


Still I think there should be some sanity check, built-in in the library, otherwise a typo could be really difficult to spot.