Hello all,

I recently started using the NHibernate Validator framework to provide
validation on the domain objects in an application I am writing. The
application does not have any database interaction; the validation is
entirely application level. Things are going well, with one exception:
I'm confused at the inconsistent behaviour of ValidatorEngine when
validating collection objects.

Consider the following code:

public class Foo
{

    [SomeValidator]
    public string Bar{get;set;}

}

public class Bar
{

    [Valid]
    List<Foo> Foos{get; private set;}

}


If I validate the Bar object - ValidatorEngine.Validate(Bar) - each
Foo in Bar.Foos will be validated. I consider this to be correct
behaviour.

However, suppose I perform validation directly against the Foo.Bars
property - ValidatorEngine.Validate(Bar.Foos) - the members in the
collection are not validated. I consider this behaviour to be
incorrect. Why should the validity of an object depend on the context
in which it is being validated?

When I work with collections, I like to create my own custom
collection object that derives from the .net framework generic
collection object; doing this lets me write class-level validation on
the collection which means that all the business rules for the
collection are encapsulated within the custom collection object. I
therefore do not have to worry about adding the business rules to each
reference I create to the collection. For example:

public class Foo
{

    [SomeValidator]
    public string Bar{get;set;}

}

//All business rules for the Bar collection are
//encapsulated on the BarCollection object
[UniqueMembers(Message = "There are duplicate Foos")]
[SomeOtherValidator]
public class FooCollection : List<Foo>
{
}

public class Baz1
{

    //Anywhere I use the FooCollection object, I
    //know the full set of business rules will be applied
    [Valid]
    FooCollection Foos{get; private set;}

}

public class Baz2
{

    //I can guarantee that the same validation rules
    //will be applied in this reference to a FooCollection
    [Valid]
    FooCollection Foos{get; private set;}

}


Again, we see inconsitent behaviour in the ValidatorEngine when
validating the objects. ValidatorEngine.Validate(Baz1) validates the
members in Baz1.Foos, but fails to run the UniqueMembers and
SomeOtherValidator validators on the FooCollection object. Conversely,
ValidatorEngine.Validate(Baz.Foos) runs the class-level validators on
FooCollection but fails to validate the members of the collection.

So, the ValidatorEngine never runs the full set of validation rules on
the FooCollection object. And, worse than that, it possible for a
single instance of an object to be both valid, and invalid, depending
on the context of the validation!

Am I missing something obvious here? This is a bug, surely?

I've only have a brief dig around in the code, but it seems that
ClassValidator.ChildInvalidValues(...) makes an assumption that a
collection will not have class level validators. Is there a good
reason for this? I'd like to get some other opinions on this before I
raise this as a bug.

Regards,

Phil.

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to