validation application block object validator not working with rulesets

1.1k views Asked by At

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

There are 1 answers

3
pradeeptp On

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")]

In my code I had wrongly declared it as follows

// Wrong syntax
[ObjectValidator(Ruleset = "RuleSetA")]