-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Same constraint violation in multiple groups
PostPosted: Thu Aug 28, 2008 8:47 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

I would like to continue a discussion I started with Emmanuel regarding the evaluation of groups and why InvalidConstraint.getGroups() returns an array of group names, instead of the single group name under which the validation failed. The reason was that it is possible that the same field/value gets validated in multiple groups.

Emmanuel gave this use case:
Quote:
Code:
for ( InvalidConstraint ic : validator.validate(Address.class, "first", "second") ) {
  System.out.println(ic.getGroups());
}

public class Address {
  @NotNull(groups={"default", "first", "second"}) private String title;
}

// should print
// [first, second]
// as both are wrong



Here are my questions:

Just for clarification first - in the above example validate will return a Set with one single element and InvalidConstraint.getGroups() on this element will return all the groups in which this constraint failed, right? In this case - first and second. I am asking, because one could also imagine that you get a Set with two InvalidConstraint instances. One failure per group.

For that reason I was asking before why InvalidConstraint.getGroups() is not replaced by InvalidConstraint.getFailingGroup(). I was under the impression that you get one InvalidConstraint per failure.

Quote:
This is only the canonical example, I believe @GgroupSequence makes these things possible without having to explicitly list (my implementation of it at least).


I am not so sure. The whole group sequence thing got me 'confused' in the first place. Let's change our example:
Code:
@GroupSequence(name="foo", sequence={"first", "second"})
public class Address {
   @NotNull(groups={"default", "first", "second"}) private String title;
}

validator.validate(Address.class, "foo");


Now the documentation says (3.4 - Group and group sequence):

"...if one of the groups processed in the sequence leads to one or more validation failures, the groups following in the sequence must not be processed..."


This means that in the above example I will only validate the group first, since it will lead to an error. second is not processed. So there is a semantic difference between:
Code:
validator.validate(Address.class, "foo");

and
Code:
validator.validate(Address.class, "first", "second");


I initally thought that GroupSequence was just a way to alias a list of groups. Please let me know if I still got something wrong.

My example raises another question which is related to BVAL-2. Say, I would have called the group sequence name default - should this work? The reason I am asking is another senetence out of the spec:

"Group sequences must not use constraint declaration group names as a name. In other words, constraint definition groups cannot contain one of the group sequence names."


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 28, 2008 9:48 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I don't think there is much value in returning the same constraint multiple time even if it belongs to several groups. The rule is one failure per constraint.

yes there is a difference between "foo" and "first", "second" as you explained in your example.

The use case I imagined was more complex

Code:
@GroupSequence(name="foo", sequence={"first", "second"})

@NotNull(groups={"default", "first", "second", "third"}) private String title;

validator.validate(Address.class, "foo", "third");


The spec allows an implementation to run first and third at the same time.
And if first succeed, second is run. This is done to reduce the number of time you do graph traversal.

You can name a sequence "default" but none of the constraints can have the default (or empty) group.

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.