-->
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.  [ 6 posts ] 
Author Message
 Post subject: Conditional Validation
PostPosted: Thu Apr 10, 2008 10:11 pm 
Beginner
Beginner

Joined: Fri Nov 03, 2006 3:21 pm
Posts: 30
I don't find the part of conditional validation in this first draft.

How do you think in this JSR the problems discussed this topic:
http://forum.hibernate.org/viewtopic.php?t=979555

How to write these validations in this specification?

Code:
@NotNull
@Length(applyIf="this.userType == 'user'", min=4, max=20)
@Length(applyIf="this.userType == 'admin'", min=8, max=20)
public String getPassword() {
   return password;
}

-> password for user must have 4 chars or more (low security)
-> password for admin must have 8 chars or more (high security)

Code:
public Boolean isReceiveNews() {
return receiveNews;
}
public Boolean isReceiveAlerts() {
return receiveAlerts;
}
@NotNull(applyIf="value.receiveNews== true")
@NotNull(applyIf="value.receiveAlerts== true")
public String getMail() {
return mail;
}
@NotNull(applyIf="value.receiveAlerts== true")
public String getTelephone() {
return telephone;
}

-> the user can receive news and alerts
-> if he doesn't want to receive anything, no need to inform mail or phone
-> if he wants to receive news, needs at least inform your mail
-> if he wants to receive alerts, he must inform mail AND telephone


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 23, 2008 7:51 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Did you read http://in.relation.to/Bloggers/BeanValidationSneakPeekPartIIIGroupsAndPartialValidation and the example showing canbuy/oneclick etc.

This might not be satisfactory to you, but let's me know what you think and let's start from here.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 23, 2008 8:00 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Also you might want to join forces here http://forum.hibernate.org/viewtopic.php?t=985343

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 23, 2008 8:51 pm 
Beginner
Beginner

Joined: Fri Nov 03, 2006 3:21 pm
Posts: 30
You are referring to:

Code:
public class Account {

        @NotEmpty @Length(max=50) private String firstname;
        @Length(max=50) private String middleName;
        @NotEmpty @Length(max=50) private String lastname;
       
        @NotNull(groups="canbuy") @Phone private String cellPhone;

        @Valid @NotEmpty(groups="canbuy")
        private Set<Address> addresses;

        @Valid @NotNull(groups="canbuy")
        private Address defaultAddress;
       
        @Valid @NotEmpty(groups="canbuy")
        private Set<PaymentMethod> paymentMethod;

        @Valid @NotNull(groups="oneclick")
        private Address defaultPaymentMethod;

}


Code:
Set<InvalidConstraint> accountIssues = validator.validate(account);
if ( accountIssues.size() > 0 ) {
        //push that error list to the user
}
else {
        if ( validator.validate(account, "canbuy").size() == 0 ) {
                enableBuyButton();
                if ( validator.validate(account, "oneclick").size() == 0 ) {
                        enableOneClick();
                }
        }
}



I think this is verbose. The developer may have to write many if's ... I don't like nested ifs as in this example... but it is a personal opinion (style).... in large classes or complex situations we would have many if's to control the validations flow.

I like more of the approach that the framework of validation is totally responsible for the problem - validate.

Complete the validations of the bean using "external if" seems a work-around to solve the problem (lack of features).
This approach is similar to bean validation + external rules.

Making an analogy, if the Hibernate Core didn't have the WHERE clause, we would have to bring all the objects and use if to get the desired objects. This is possible but not necessary because the HQL is powerful to return what we want without the additional steps.

However this approach works.
I don't want to make any destructive criticism. I like the feature of groups and voted on it in the jira HV!

But I don't know if it would be better to resolve this problem.
That's my opinion.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 24, 2008 9:11 am 
Beginner
Beginner

Joined: Fri Nov 03, 2006 3:21 pm
Posts: 30
...more:

The group approach work with my first example. Rewriting:
Code:
@NotNull
@Length(groups="user", min=4, max=20)
@Length(groups="admin", min=8, max=20)
public String getPassword() {
   return password;
}


The java annotation has another problem to put more than one @Length in the same field, but that is another problem. Probably the right would be:
@LengthConstraints(
@Length(groups="user", min=4, max=20)
@Length(groups="admin", min=8, max=20)
)

I had ignored this limitation in examples...

But ignoring that the important thing is the group approach doesn't work with the second example. Rewriting:
Code:
public Boolean isReceiveNews() {
return receiveNews;
}
public Boolean isReceiveAlerts() {
return receiveAlerts;
}
@NotNull(groups={"news","alerts"})
public String getMail() {
return mail;
}
@NotNull(groups={"alerts"})
public String getTelephone() {
return telephone;
}


The groups are pre-defined in the code (fixed)... and validation conditional I suggested in the first post depends on the data of the bean.

Without evaluating the data of mybean.isReceiveNews() and mybean.isReceiveAlerts() I don't know if I have to call:

validator.validate(mybean, "news") or validator.validate(mybean, "alerts")

Then the groups approach is a validation conditional immutable / static.

The original proposal of the topic is a validation conditional "dynamic" based on the data. The groups approach not solve this because the groups and 'validate if' are pre-defined.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 24, 2008 10:53 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
If you guys can converge on something here that is elegant and simple that would be nice.

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.