Ruleset="all", is this OK?

Topics: Validation Application Block
Oct 5, 2007 at 4:53 PM
I have (I think) implemented a facility to allow a ruleset="all" so that a given attribute can be used in all rulesets and saves duplication.

It seemed very easy to do and I'm concerned I may have broken things. Would the author of this code be in a position to say that what I have done is OK?

In the MetaDataValidatedElement there exists 2 referencs to ruleset. They seem to check if an Attribute should be returned for evaluation and I have tweaked them to always return if the ruleset on the attribute is all. It does seem to work in my testing but I have no real idea if this is likely to break anything else.


IEnumerable<IValidatorDescriptor> IValidatedElement.GetValidatorDescriptors()
{
if (this.memberInfo == null)
yield break;

foreach (ValidatorAttribute attribute in
this.memberInfo.GetCustomAttributes(typeof(ValidatorAttribute), false))
{
if (this.ruleset.Equals(attribute.Ruleset) || string.Compare(attribute.Ruleset, "all", true) == 0)
yield return attribute;
}
}

and

private static T ExtractValidationAttribute<T>(MemberInfo memberInfo, string ruleset)
where T : BaseValidationAttribute
{
if (memberInfo != null)
{
foreach (T attribute in Attribute.GetCustomAttributes(memberInfo, typeof(T), false))
{
if (ruleset.Equals(attribute.Ruleset) || string.Compare(attribute.Ruleset, "all", true) == 0)
{
return attribute;
}
}
}

return null;
}
Oct 6, 2007 at 1:53 PM
Hi,

The retrieval of the attributes for the actual validators looks fine. I'm a bit concerned with the retrieval of the validation attributes in ExtractValidationAttribute; only the first one is retrieved, and the retrieval order cannot be relied on.

I can think of arguments in favor and against usign the "all" ruleset with the validation modifiers. Against this, you may want all the "default" validation rules to be applied using the AND composition, but the "extra" validation rules using the OR composition. In favor, you avoid duplication. Probably a mixture of the "self validation" work around I replied to one of your earier posts and this can work in the different scenarios.

Fernando