Ruleset scope

Topics: Validation Application Block
Oct 4, 2007 at 5:52 PM
What is the scope of a validation with no Ruleset specified?

It reads like they belong to an "anon" ruleset by default.

Sometimes I would like a rule with no ruleset to belong to ALL rulesets, so they get evaluated for every ruleset.

Is there any way of doing this so that I don't have to duplicate every rule for every ruleset on every property!

Oct 6, 2007 at 2:14 PM

This feature is not provided directly. You can work around this with a trick like this though:

public class DomainClass
private int field;

[RangeValidator(0, RangeBoundaryType.Inclusive, 0, RangeBoundaryType.Ignore, Ruleset = "default")]
[RangeValidator(0, RangeBoundaryType.Ignore, 10, RangeBoundaryType.Inclusive, Ruleset = "strict")]
public int Property
get { return field; }
set { field = value; }

[ObjectValidator("default", Ruleset = "strict")]
public DomainClass Self
get { return this; }

It's not a great solution, but should work. You will need to declare an ObjectValidator for each ruleset, but you wouldn't need to duplicate the actual rules.

Oct 8, 2007 at 11:52 AM

Sorry I'm being a bit dim, how does that work and how would I use it?

I have a class with 20 properties and 2 rulesets. On 18 of them the attributes are identical for both rulesets and on two of them they differ for each ruleset.

Oct 8, 2007 at 12:11 PM
Hi Cliff,

I'm sorry about the terseness of my reply. The idea is that you would define a ruleset with all the for all the shared validations; I called it "default" here.

The for each of the real rulesets, you would include an ObjectValidator configured for the "default" ruleset on the Self property. That would make all the validations in the "default" ruleset to be applied.

Hope this helps,