views:

78

answers:

1

Hello,

I'm having problems with [ObjectValidator]. So, i have:

public class UserBO
{
    public int ID
    {
        get;
        set;
    }

    [NotNullValidator(MessageTemplate = "Can't be null!")]
    [RegexValidator(@"[a-z]|[A-Z]|[0-9]*", MessageTemplate = "Must be valid!", Ruleset = "validate_username")]
    [StringLengthValidator(5, RangeBoundaryType.Inclusive, 25, RangeBoundaryType.Inclusive,  Ruleset = "validate_username")]
    public string username
    {
        get;
        set;
    }

and another class:

public class PersonBO
{
    public int ID
    {
        get;
        set;
    }

    [NotNullValidator(MessageTemplate="Can't be null!")]
    [ObjectValidator(MessageTemplate = "Must be valid!", Ruleset="validate_obj_user")]
    public UserBO User
    {
        get;
        set;
    }
...

Now can you tell me why the following test passes?

[TestMethod()]
public void PersonBOConstructorTest()
    {
        PersonBO target = new PersonBO()
            {
                User = new UserBO
                {
                    ID = 4,
                    username = "asd"
                }
            };
        ValidationResults vr = Validation.Validate<PersonBO>(target, "validate_obj_user");
        Assert.IsTrue(vr.IsValid);
    }

This should not be valid, because: User attribute (of UserBO type) contains username "asd" (3 characters), and i defined for it a StringLengthValidator (between 5 and 25 characters).. so why the test passes? that object is not valid I can't understand.

Thanks.

+1  A: 

You have to specify a ruleset in your ObjectValidator if you want rules from a set other than the default set applied.

[ObjectValidator("validate_username", MessageTemplate = "Must be valid!", Ruleset = "validate_obj_user")]

The above should work in this specific case. Alternatively, you could remove the ruleset parameter from the string length validator to leave it in the default set.

Scott Pedersen