Rules from another Class

Oct 26, 2009 at 4:42 PM

I would like to be able to generate validators not only from a particular type but also from other sources. So that for example a dtoMember could be validated using validators attributed to another types properties from the domain for example.

public class MemberDTO

      {

             public string Name { get; set;}   

public int Age { get; set; } 

        }

 

public class Member

        {

            [ValidateNonEmpty()]

            public string Name { get; set;}

            public int Age { get; set; } 

        }

I have achieved this by adding a few extenders to the ValidationInfo class which extract the rules from an additional type and append to the ValidationInfo's RuleSet.

 public static ValidationInfo AddMoreRulesFromType(this ValidationInfo val, Type modelType, bool PascalCaseProperties)

        {

            var ruleSet = ActiveRuleProviders.GetRulesForType(modelType);

            foreach(var key in ruleSet.Keys)

            {

                var propertyName = key;               

                foreach (var rule in ruleSet[key])

                {

                    val.AddRule(propertyName, rule);

                }

            }

            return val;

        }

 

Ideally i would like to be able add a custom attribute to the DTO class to automatically get rules from the domain class.

To do so i would like to extend the existing CastleValidatorRulesProvider to carry out the task of finding additional rules, but am unable to do it cleanly as the ValidatorRunner and IValidatorRegistry fields are private and are created in the constructor. Could i please submit a patch to add another constructor and make the fields protected or is there perhaps another way of achieving my ends?

 

 

 

 

Jan 19, 2010 at 6:48 AM

Hi philiphoy,

I have the problem too. I think CastleValidatorRulesProvider.ruleEmitters should be protected, so that we could inherit from CastleValidatorRulesProvider and emit rules for our custom castle validator.

Regards,

Wayne