-->
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.  [ 39 posts ]  Go to page 1, 2, 3  Next
Author Message
 Post subject: Conditional validation
PostPosted: Sun Sep 16, 2007 6:59 pm 
Beginner
Beginner

Joined: Fri Nov 03, 2006 3:21 pm
Posts: 30
In the last month I considered and implemented a conditional validation for Hibernate Validator. What you they think about this idea?
The details, code and jar are in the JIRA:

http://opensource.atlassian.com/project ... owse/HV-38

As basic example, it would be possible to make this type of validation:
Code:
public Boolean isReceiveNews() {
return receiveNews;
}
@NotNull(applyIf="value.receiveNews == true")
public String getAdress() {
return adress;
}
@NotNull(applyIf="value.receiveNews == true")
public String getTelephone() {
return telephone;
}


All the validations could have one applyIf that accepted a EL...


Last edited by bruno.braga on Tue Sep 18, 2007 8:30 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 17, 2007 1:24 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I am personally not a big fan of that,
for static conditional validation, I like this approach
http://opensource.atlassian.com/project ... owse/HV-22

however, your solution works just fine with custom validation annotations.

If EL has to be introduced, then your example could be rewritten like

@El(el="receiveNews==true && address != null")
public class MyBean {
}

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 17, 2007 2:55 pm 
Beginner
Beginner

Joined: Fri Nov 03, 2006 3:21 pm
Posts: 30
HV-22 is good, and I had seen it before implementing this code (at the time I voted in it also).

But I think about HV-22 and HV-38 for different uses.

The HV-22 is for you to use same POJO in different forms (two/three contexts).
The HV-38 is for validating one form in different ways depending them information (data).

The HV-38 can be used in a great questionnaire, where a field can qualify the validation of several other fields. I do not see as to make this with context. It would be a great combination of contexts.

You understand what I am wanting to say?

HV-22 and HV-38 also can be used together!

Example:
Code:
@NotNull(context="register")
@NotNull(applyIf="value.type == '1'", context="search")
public String getName() {
   return name;
}


I don`t know if Hibernate Validator would accept two @NotNull, but this is alone an example...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 17, 2007 3:03 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Yes I understand your point, but at that stage (esp the complex questionnaire), why not just writing a specific validator dedicated to the questionnaire. Sounds like just as simple and easier to read than those duplicated applyIf parameters

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 17, 2007 3:31 pm 
Beginner
Beginner

Joined: Fri Nov 03, 2006 3:21 pm
Posts: 30
Questionnaire was alone an example to be easy to explain. But simple things also need conditional validation.

applyIf does not make to duplicate annotation. In this my example was duplicate because of context…

But in normal situations it would be thus:
Code:
@NotNull(applyIf="value.type == '1'")
public String getName() {
   return name;
}

@NotNull(applyIf="value.type == '2'")
public String getFullName() {
   return fullName;
}

@NotNull(applyIf="value.type == '2' or value.type == '3'")
public String getParentName() {
   return fullName;
}


How you would solve this problem using context?
Exists some solution?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 01, 2007 9:27 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
As I said, since your case becomes more complex, why not write a bean level questionnaire validator?

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 01, 2007 2:47 pm 
Beginner
Beginner

Joined: Fri Nov 03, 2006 3:21 pm
Posts: 30
emmanuel wrote:
@El(el="receiveNews==true && address != null")
public class MyBean {
}


Emmanuel, its idea of generic EL for the Bean does not function in this my example.
The validation rules (in this in case) need to be for field, not for bean.
The field 'type' (my example) is used in some different cases and 'applyifs'.

Then I have that to write one custom annotation for each type of bean? :/ :/

The problem is complicated because I do not have ready situation to use this. I am making a tool (plugin for the Eclipse), and would like to use it to generate annotations of Hibernate Validator. Then the validations will be chosen by the user, dynamically... also the rules of when they will be you validate (applyIf). The user will go to choose everything in the tool, and the tool goes to generate some things, also to place annotations in the Beans.

We have that to make something flexible so that the people use, of easy form.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 19, 2007 4:38 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I don't understand why it's not possible
Use an
Code:
@Els {
  @EL(...) //for first field
  , @EL(...) //for secondfield etc
}
public class MyEntity

There is no need for a specific annotation per bean type

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 19, 2007 8:27 pm 
Beginner
Beginner

Joined: Fri Nov 03, 2006 3:21 pm
Posts: 30
Not work because this:

ONE:
emmanuel wrote:
@El(el="receiveNews==true && address != null")
public class MyBean {
}

This code could be rewritten to:
Quote:
@El(el="receiveNews==true && address != null", field="name")
@El(el="receiveNews==true", field="fullName")
public class MyBean {
}



TWO:
Unable to replace the code below using the annotation @EL:
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;
}

- The 'admin' user must have a more secure password, then has a @Length differently.
- Even adding the 'field' in @EL, it does not work for other examples that have two or more types of validations for the same field.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 20, 2007 11:51 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Why is 'field' useful for? I don't get it.

as for your second point, I'm not an EL expert but surely you can do something like

Code:
@El(el="userType=='user' && password.length > 4 && password.length < 8", message="password too weak")
public class User { ... }

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 21, 2007 10:00 am 
Beginner
Beginner

Joined: Fri Nov 03, 2006 3:21 pm
Posts: 30
Hi Emmanuel, owwww...

Are you thinking of using the @EL in a different way than I imagined. Sorry.

I thought the @EL to enable or not a validation.
The 'field' is to identify which validation should be enable or not (in this case the scope is for field).

If so we would have the problem that I wrote in the last post.

----------

But even using that their concept of @EL, it does not solve the problem.
You wrote my example in @EL ignoring the other annotations of Hibernate Validator and rewriting everything in EL (password.length > 4).

You only wrote this because I wrote examples with NotNull or Length.

But imagine that in place of these annotations (NotNull, Length) I put a more complicated (@CreditCardNumber, @Amex, @Visa, @EAN, ..., ...)? You would rewrites it in EL?

The EL should not be used to rewrite the validation. It should be used only to enable or not an annotation that already exists in Hibernate Validator.

You understand?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 23, 2007 3:15 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I understand but in this case requirements can go on and on for ever
Why not apply @MyValidation1 on field1 only if @MyValidation2 on field2 passes and @MyValidation3 on field3 fails, etc etc.

At some point, the line between simplicity and expressiveness has to be drawn.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 23, 2007 4:17 pm 
Beginner
Beginner

Joined: Fri Nov 03, 2006 3:21 pm
Posts: 30
emmanuel wrote:
Why not apply @MyValidation1 on field1 only if @MyValidation2 on field2 passes and @MyValidation3 on field3 fails, etc etc.


I don't understand...
@MyValidation1 = Custom Validation?

If it is we will return to some messages behind:
Quote:
Then I have that to write one custom annotation for each type of bean or field? :/ :/


But if MyValidation1 is a validation of Hibernate Validator as dynamically apply it? How to apply or not according to the data?

I believe you are suggesting create customized annotations. But beyond work, an annotation of Hiberante Validator not have access to all the data from the bean to validate it internally.

interface Validator:
Code:
public boolean isValid(Object value) { ...


value => this field value

I don't really understand what you mean...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 30, 2007 2:29 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
In my example, @MyValidation1 is either a built-in validator or a custom one.
What I am trying to say is that applyIf only solves a subset of the "dependency between validation rules" problem, and that's the reason why I am not really happy with it.
The generic solution, which I agree requires you to write a custom validator if you don't want the generic @El approach I gave, is to use a bean level validator. A bean level validator gets the bean instance passed to the isValid method (as opposed to a property level validator).

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 30, 2007 5:32 pm 
Beginner
Beginner

Joined: Fri Nov 03, 2006 3:21 pm
Posts: 30
hi Emmanuel =)

Okay, @MyValidation1 was then a Bean Validator. In that case we would have to work to create the custom validators for each case.......

I will make some basic questions to try to better understand the solution you are proposing:

1) the Hibernate already has support for Bean Validators? (I don't remember to have seen it). I think that does not yet exist, right?

2) There dependence between the @MyValidation1 and the Bean? How @MyValidation1 would have access to attributes of the Bean? Using the dependency or reflection?

3) If I change the Bean, I have to also change the @MyValidation1?

4) @MyValidation1 would replace the Validators of the fields (NotNull, Length, Mastercard) or would enable / disable these validations?

5) If @MyValidation1 replace the validation of fields, I would have to rewrite the rules of the @Length, @Credicard, ... again in the @MyValidation1?

6) If @MyValidation1 not replace the validation of fields, as I do for him enabling a single validation (@NotNull) and not enable another validation (@Length)?

emmanuel wrote:
applyIf only solves a subset of the "dependency between validation rules" problem

What problem the applyIf not solve?
You remember an example?
If there are any, we can try to improve the idea together.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 39 posts ]  Go to page 1, 2, 3  Next

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.