views:

190

answers:

1

Howdy,

Ive been battling with this issue which I originally thought may have been to do with polymorphism/inheritance affecting my validation but ive narrowed it to this...

Here is the class structure..

public class Employee {

    [ObjectValidator(Ruleset = "A")]
    public EmployeeName Name { get; set; }

    public Employee()
    {
        Name = new EmployeeName();
    }
}

public class EmployeeName
{
    [StringLengthValidator(1,20,Ruleset = "A")]
    public string First { get; set; }

    public string Last { get; set; }

    public EmployeeName()
    {
        First = string.Empty;
        Last = string.Empty;
    }
}

The test:

[TestFixture]
public class ObjectValidationWithRulesets
{
    [Test]
    public void wont_validate_with_a_ruleset()
    {
        var person = new Employee()
        {
            Name = new EmployeeName()
            {
                First = string.Empty, 
                Last = string.Empty
            }
        };

        var ruleSetValidator =
            ValidationFactory.CreateValidator<Employee>("A");

        var validationResults = ruleSetValidator.Validate(person);

        Assert.That(!validationResults.IsValid,
            "Validation with rulsets failed");
    }
}

This test passes if I remove remove the Ruleset stuff. But once the ruleset is applied, I cant get the object to validate correctly.

Can anyone shed some light on this?

Cheers,

+1  A: 

I too had this problem, but I did not define targetRuleSet in the configuration file. I fixed the issue by correcting the way way I was declaring the ObjectValidator attribute . The correct syntax that worked for me is the following

// Correct
[ObjectValidator("RuleSetA", Ruleset = "RuleSetA")]

or

// Correct
[ObjectValidator("RuleSetA")]

In my code I had wrongly declared it as follows

// Wrong syntax
[ObjectValidator("RuleSetA")]
pradeeptp
Pradeeptp is right. For some strange reason the `ObjectValidatorAttribute` does not use the `Ruleset` property in its `DoCreateValidator` method, but only the value that was set through the `targetRuleset` constructor argument.
Steven
Steven, could you please confirm what you have edited is correct?. I can see that you have added " or // correct..." section in my original sample.
pradeeptp