-->
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.  [ 8 posts ] 
Author Message
 Post subject: Constraint with metadata about property
PostPosted: Tue Apr 08, 2008 2:09 pm 
Beginner
Beginner

Joined: Fri Jan 14, 2005 7:47 am
Posts: 37
Location: Spain
It would be useful the option of receive metadata about property to validate in a constraint. That is, the next possibility would be fantastic:
Code:
public class RequiredConstraint implements Constraint<Required>, PropertyConstraint {

  public void initialize(Required constraint) {
     ...
  }
  public boolean isValid(Object value) {
    ..
  }

  public void setMetaProperty(Class containerClass, PropertyDescription property) {

  }

}


This is useful for:
- Obtaining the type, and do the validation in a way or another depend on type.
- Read other annotations of the same property. The the validation logic can depend on annotation combination.
- Using metadata from other part of the application that needs to be access from name of class/property.
- Doing validation based on the name of the property, or container class.

In OpenXava(http://www.openxava.org/) for example, the behaviour of @Required annotation is configurable by application, type and stereotype. We need to access to the metadata of the property in order to do the exact validation.

What do you think ?

Cheers
Javier Paniza


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

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
it would have to be isValid(object, metadata ...)
Otherwise you can have some multi threading issues I think, though I am not sure.

I am interested in the use cases behind your demand

Quote:
- Obtaining the type, and do the validation in a way or another depend on type.


You mean depending on the fact that it's a getter or a field? why does that matter? Java should have had a property feature anyway.

Quote:
- Read other annotations of the same property. The the validation logic can depend on annotation combination.

Hum, I kind of don't like that I think :) Can you give a concrete example?

Quote:
- Using metadata from other part of the application that needs to be access from name of class/property.

I don't understand.

Quote:
- Doing validation based on the name of the property, or container class.

Can you give an example where it is useful?

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 24, 2008 1:43 pm 
Beginner
Beginner

Joined: Fri Jan 14, 2005 7:47 am
Posts: 37
Location: Spain
emmanuel wrote:
it would have to be isValid(object, metadata ...)
Otherwise you can have some multi threading issues I think, though I am not sure.

Multi threading is not an problem because meta data is injected once. And metadata is static info. As in the case of initialize method.

Quote:
You mean depending on the fact that it's a getter or a field? why does that matter? Java should have had a property feature anyway.

Java already has a property feature based in convention. Even it has meta property classes, for example:
http://java.sun.com/j2se/1.5.0/docs/api ... iptor.html

But, if field versus getter is a big problem, injecting the Member is enough for me:
http://java.sun.com/j2se/1.5.0/docs/api ... ember.html


Quote:
Quote:
- Read other annotations of the same property. The the validation logic can depend on annotation combination.

Hum, I kind of don't like that I think :) Can you give a concrete example?

Of course:
Code:
@ManyToOne @Required
private EMail email;

@Required
private Email email;

In this case the @Required constraints does not behaves equals for a reference (@ManyToOne) and for a regular property.

Code:
@Hidden @Required
String code;

@Required
String code;

In this case the @Required constraint has a special behaviour for @Hidden members.

When you write concrete constraints (as build in constraint of Hibernate Validator) this is not an issue, even it could be bad. But when you write constraint with an higher level of abstraction (as @Required for example) this feature is needed.

Quote:
Quote:
- Using metadata from other part of the application that needs to be access from name of class/property.

I don't understand.

For example, we can have a properties as this:
Code:
@CreditLimit
private BigDecimal currentAmount;

@CreditLimit
private BigDecimal maxAmount;


And, in CreditLimit.properties we can have:
Code:
Customer.currentAmount=10000
Customer.maxAmount=30000
Partner.currentAmount=11000
Partner.maxAmount=35000

In addition of a properties file we also can use a xml file or even a database table.
That is we can configure the behaviour of the validation without recompile, and without redeploy. Application that can customize its logic in hot are VERY common, specially if you have your application deployed in several customers.

Quote:
Quote:
- Doing validation based on the name of the property, or container class.

Can you give an example where it is useful?

Yes.
For example:
Code:
@Required
String name;

The fact that this property is called 'name' means that is required at least 5 charecters in order to fulfil @Required constraint. We can regulate the semantic of the constraint based in the name of the member.
Really I don't like use this technique, but name convention are usual, remember the Java property feature itslef uses it.


I don't use my imagination for my proposals, instead I propose here things that already I am using for years.

Cheers


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 24, 2008 3:26 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
javierpaniza wrote:
Quote:
Quote:
- Read other annotations of the same property. The the validation logic can depend on annotation combination.

Hum, I kind of don't like that I think :) Can you give a concrete example?

Of course:
Code:
@ManyToOne @Required
private EMail email;

@Required
private Email email;

In this case the @Required constraints does not behaves equals for a reference (@ManyToOne) and for a regular property.

Code:
@Hidden @Required
String code;

@Required
String code;

In this case the @Required constraint has a special behaviour for @Hidden members.

When you write concrete constraints (as build in constraint of Hibernate Validator) this is not an issue, even it could be bad. But when you write constraint with an higher level of abstraction (as @Required for example) this feature is needed.

I am sorry, I still don't fully get you. How is @Required depending on other annotations? in your example. It seems that it depends on the type for sure but I don't see how @Hidden affects @Required.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 25, 2008 1:28 pm 
Beginner
Beginner

Joined: Fri Jan 14, 2005 7:47 am
Posts: 37
Location: Spain
Quote:
I am sorry, I still don't fully get you. How is @Required depending on other annotations? in your example. It seems that it depends on the type for sure but I don't see how @Hidden affects @Required.

The logic used in @Required for validation is not the same for a hidden member and for a non-hidden one.

The next example is extracted from a real application:
Code:
@Stereotype("MONEY") @Required
private BigDecimal unitPrice;


And in a validators.xml file we have:
Code:
   <required-validator>
      <validator-name name="NOT_NULL"/>
      <for-stereotype stereotype="MONEY"/>      
   </required-validator>

In this way, we declare that in our application we use the NOT_NULL validation for enforce @Required for properties annotated with stereotype "MONEY". Thus we can refine the validation policy by application.
But, this type of things is not possible if we cannot access to member metadata.

Do you think that injecting a java.lang.reflect.Member (java.lang.reflect.Field or java.lang.reflect.Method) object to the constraint can be useful ?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 02, 2008 7:45 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
So in your example what would be an alternative mapping that would force such flexibility?
Because for the poor user, you force him to set 2 annotations instead of one :)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 05, 2008 1:37 pm 
Beginner
Beginner

Joined: Fri Jan 14, 2005 7:47 am
Posts: 37
Location: Spain
emmanuel wrote:
So in your example what would be an alternative mapping that would force such flexibility?

Code:
<required-validator>
      <validator-name name="NOT_ZERO"/>
      <for-stereotype stereotype="MONEY"/>     
</required-validator>

emmanuel wrote:
Because for the poor user, you force him to set 2 annotations instead of one :)

@Stereotype("MONEY") and @Required have different meaning, we can have:
Code:
@Required
private BigDecimal quantity;

@Stereotype("MONEY")
private BigDecimal discount;

@Stereotype("MONEY") @Required
private BigDecimal amount;

The behaviour of the constraint associated to @Required changes when the property to validate is @Stereotype("MONEY").

Anyways, this is only one of the multiple possibilities that access to metadata of the member can give us.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 10, 2008 12:58 pm 
Beginner
Beginner

Joined: Thu Jun 24, 2004 1:04 pm
Posts: 35
Location: Minnesota - USA
Quote:
The behaviour of the constraint associated to @Required changes when the property to validate is @Stereotype("MONEY").

Anyways, this is only one of the multiple possibilities that access to metadata of the member can give us.


I completely agree in terms of required-ness and data type being independent. I'm currently using a custom annotations like the following:

Code:
@Type(TypeEnum.SOCIAL_SECURITY_NUMBER)
@Required
Integer ssn;

@Type(TypeEnum.PHONE_NUMBER)
String phone;


I'm all for mechanisms in the API which support crawling out from underneath the normal validation scheme, into the larger body (metadata) of the code. The more non-invasive/optional flexibility we have, the better.

The other way I think about this... if the validation framework is going to use/find information X to decide to trigger a validation, then we should make sure we provide X to the custom validation, because it would be short-sighted to assume that the custom validation would not find X to be useful.

All of the context of the validation processing and execution should be available.


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