Validation always successful

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

Hi,

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?

 

Thanks

 

Jun 7, 2010 at 1: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.
entlib.support@Avanade.com

Jun 7, 2010 at 1: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.