views:

171

answers:

1

Hello,

I'm using Microsoft Enterprise Validation. But in this case the test passes, even if i assign null value to that attribute. Why?

   [NotNullValidator(MessageTemplate = "Cannot be null!", Ruleset="validate_x")]
    [StringLengthValidator(10, RangeBoundaryType.Inclusive, 40, RangeBoundaryType.Inclusive, Ruleset="validate_x")]
    [RegexValidator(@"^[A-Z][a-z]*\s[A-Z][a-z]*$", MessageTemplate = "Not valid!", Ruleset="validate_x")]
    public string x
    {
        get;
        set;
    }

And in a test class:

    [TestMethod()]
    public void xTest()
    {
        MyBO target = new MyBO() { x = null };
        ValidationResults vr = Validation.Validate<MyBO>(target, "validate_x");
        Assert.IsTrue(vr.IsValid);
    }

So i got that this is valid, but it should not be. (x is null!) Any ideas?

Thanks

+1  A: 

When copying the code you supplied to a simple console application I see the IsValid property of the ValidationResults object become False. I think you're doing something wrong somewhere, but it is impossible so see this by just looking at your code. Here is the code I used:

public class MyBO
{
    [NotNullValidator(MessageTemplate = "Cannot be null!",
        Ruleset = "validate_x")]
    [StringLengthValidator(10, RangeBoundaryType.Inclusive, 40, 
        RangeBoundaryType.Inclusive, Ruleset = "validate_x")]
    [RegexValidator(@"^[A-Z][a-z]*\s[A-Z][a-z]*$",
        MessageTemplate = "Not valid!", Ruleset = "validate_x")]
    public string x { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        MyBO target = new MyBO() { x = null };
        ValidationResults vr = Validation.Validate<MyBO>(target, "validate_x");
        Console.WriteLine(vr.IsValid);
    }
}
Steven