-->
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: Issue about InvalidConstraint
PostPosted: Tue Apr 01, 2008 11:23 am 
Regular
Regular

Joined: Tue Apr 01, 2008 11:10 am
Posts: 69
Hello,

First of all thanks for making this JSR, we've been using Hibernate
Validator for production work and it has almost all we want. Since this
JSR addresses the same problems with very similar solutions, we're very
happy about it.

We have issues with InvalidConstraint though because once we validate
our persistent entities, we have a hard time mapping validation errors
to UI fields.
To be clearer: suppose we have a "name" bean property which fails
validation, we want to put an error message in the UI next to our "name"
input field. We may be able to gather the name of the bean property from
the InvalidConstraint.getPropertyPath() but it's a field which requires
parsing in order to get information.

Now suppose we have decided to put bean class validation methods in the
bean itself, with several methods annotated with @AssertTrue. For those
validation errors we would like to be able to specify that the
validation is the entire bean, not just the property annotated with
@AssertTrue (which is just a placeholder for validation, not for a
value).

I think InvalidConstraint should include more information about the
error. Putting the validation metadata (the validation annotation which
caused the error) as well as the property type (field, method, class)
would certainly be useful. The InvalidConstraint.getPropertyPath()
should be defined in a way which does not require parsing.

For instance if getPropertyPath() would return "field1.field2" we should
be able to query the validation annotations (or constraints) that failed
on "field2", and whether "field1" is a method or a field, same for
field2.

If we have that we can also differenciate validations cases such as:

class Foo{
@NotNull
Bar bar;
}

@MyValidator
class Bar{
}

Where both a "NotNull" and "MyValidator" validation errors would be
reported with property path "bar" I think.

Thanks.

_________________
--
Stéphane Épardaud


Top
 Profile  
 
 Post subject: Re: Issue about InvalidConstraint
PostPosted: Wed Apr 02, 2008 5:59 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
FroMage wrote:
We have issues with InvalidConstraint though because once we validate
our persistent entities, we have a hard time mapping validation errors
to UI fields.
To be clearer: suppose we have a "name" bean property which fails
validation, we want to put an error message in the UI next to our "name"
input field. We may be able to gather the name of the bean property from
the InvalidConstraint.getPropertyPath() but it's a field which requires
parsing in order to get information.


I suspect some kind of EL engine can access the field for you. Generally speaking, some ui framework would probably like to add some sort of indirection between a property name and a field name (depending on the philosophy).

Quote:

Now suppose we have decided to put bean class validation methods in the
bean itself, with several methods annotated with @AssertTrue. For those
validation errors we would like to be able to specify that the
validation is the entire bean, not just the property annotated with
@AssertTrue (which is just a placeholder for validation, not for a
value).

Why did you do that (as opposed to a class level constraint?
Others have requested to be able to ahve multiple fields per constraint failure, this mught help in your case.

Quote:
I think InvalidConstraint should include more information about the
error. Putting the validation metadata (the validation annotation which
caused the error) as well as the property type (field, method, class)
would certainly be useful. The InvalidConstraint.getPropertyPath()
should be defined in a way which does not require parsing.

How should it be? I would welcome type safety.
Keep in mind that Incalidconstraint will probably need to be serializable, this limit what you want / can add.


Quote:

For instance if getPropertyPath() would return "field1.field2" we should
be able to query the validation annotations (or constraints) that failed
on "field2", and whether "field1" is a method or a field, same for
field2.


What is the use case you have in mind?

If we have that we can also differenciate validations cases such as:

Quote:

class Foo{
@NotNull
Bar bar;
}

@MyValidator
class Bar{
}

Where both a "NotNull" and "MyValidator" validation errors would be
reported with property path "bar" I think.

Thanks.


They both are differenciated as they do not come from the same object root. the uniqueness comes from the tuple propertypath / root object.

As you cna see, I am interested in the use cases that lead to your requests.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 05, 2008 9:25 am 
Regular
Regular

Joined: Tue Apr 01, 2008 11:10 am
Posts: 69
Why we did several @AssertTrue methods in one class as opposed to class-level validation? First the validation is very specific to each class, so abstracting it to another place would not allow us to reuse it anyways. The second reason is that we like to have our model defined in the same entity as its validation constraints. If we have 10 different types of class-level validation checks we cannot possibly reveal their entire purpose using an annotation name. Putting them each in @AssertTrue methods within the entity allows us first to have a different localised message for each validation check, and second to keep the validation logic close to the model so they stay in sync.


I'm sorry, I am going to answer to the rest, but I need to think it through some more :)

_________________
--
Stéphane Épardaud


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 05, 2008 9:50 am 
Regular
Regular

Joined: Tue Apr 01, 2008 11:10 am
Posts: 69
Re-reading the specs, I guess on page 25 (section 4.2) you mention putting a pointer to metadata. That would solve my problem.

If I get

public interface InvalidConstraint<T> {
...
// the element on which the constraint failed
ElementDescriptor invalidElementDescriptor;

// the constraint which failed
ConstraintDescriptor invalidConstraintDescriptor;
}

I can figure out whether one InvalidConstraint is a property failure or a class-level validation done in a @AssertTrue validation method. It takes some routing, but it is doable.

I guess we don't need the Validator<T> in there because we have T in InvalidConstraint.getBeanClass() so we can look it up, no?

Is there no scope in the JSR to describe validation exceptions that would be triggered by other layers such as JPA ?
It would be really great that if JPA triggers automatic validation the Exception type is specified. Actually since several frameworks might want to do automatic validation, I think this Exception API should be defined in this JSR 303. (InvalidStateException containing an array of InvalidConstraint would be good enough for me).

Also since other frameworks/layers are expected to automate this validation, how would be instruct them of the "groups" to validate? We can already specify orders for groups, but not which groups automatically I think...

_________________
--
Stéphane Épardaud


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 07, 2008 5:03 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
FroMage wrote:
Re-reading the specs, I guess on page 25 (section 4.2) you mention putting a pointer to metadata. That would solve my problem.

If I get

public interface InvalidConstraint<T> {
...
// the element on which the constraint failed
ElementDescriptor invalidElementDescriptor;

// the constraint which failed
ConstraintDescriptor invalidConstraintDescriptor;
}

I can figure out whether one InvalidConstraint is a property failure or a class-level validation done in a @AssertTrue validation method. It takes some routing, but it is doable.

I guess we don't need the Validator<T> in there because we have T in InvalidConstraint.getBeanClass() so we can look it up, no?


Why would you need the validator on a failure report?

Quote:
Is there no scope in the JSR to describe validation exceptions that would be triggered by other layers such as JPA ?
It would be really great that if JPA triggers automatic validation the Exception type is specified. Actually since several frameworks might want to do automatic validation, I think this Exception API should be defined in this JSR 303. (InvalidStateException containing an array of InvalidConstraint would be good enough for me).


It's an interesting question, we might push a proposed exception but fundamentally, each client can react ho it pleases him. If you think about JSF, you don't want an exception but a message in an html page.

Quote:
Also since other frameworks/layers are expected to automate this validation, how would be instruct them of the "groups" to validate? We can already specify orders for groups, but not which groups automatically I think...

This will most likely vary from one client framework to an other.
JSP would probably use the persistence.xml file, JSF use a tag, WebBeans use (maybe) an annotation.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 08, 2008 3:57 am 
Regular
Regular

Joined: Tue Apr 01, 2008 11:10 am
Posts: 69
emmanuel wrote:

Why would you need the validator on a failure report?



To differentiate our @AssertTrue methods which are really class-level validation.

emmanuel wrote:

It's an interesting question, we might push a proposed exception but fundamentally, each client can react ho it pleases him. If you think about JSF, you don't want an exception but a message in an html page.



True, but you could state it another way: if a framework cannot handle automated validation failure, it should raise XXXException containing ...
This way, JSF or JSP which would be able to reject forms which don't validate would not be bound to make exceptions whereas the JPA layer would generate some standard exception to handle. We're really struggling at the moment trying to catch Hibernate Validation exceptions and JPA validation exceptions (nullable, unique, etc) from a JAX-RS (JSR311) server endpoint and some sort of official unified exception would help.

emmanuel wrote:
FroMage wrote:
Also since other frameworks/layers are expected to automate this validation, how would be instruct them of the "groups" to validate? We can already specify orders for groups, but not which groups automatically I think...

This will most likely vary from one client framework to an other.
JSP would probably use the persistence.xml file, JSF use a tag, WebBeans use (maybe) an annotation.


Yes. makes sense, thanks :)

_________________
--
Stéphane Épardaud


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.