-->
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.  [ 5 posts ] 
Author Message
 Post subject: Unwrapped value validation discussion
PostPosted: Wed Jun 03, 2015 4:44 am 
Newbie

Joined: Wed Jun 03, 2015 3:44 am
Posts: 7
Hi guys!

I didn't find any topic on this new feature (@UnwrapValidatedValue), so here's one now. :)

I'll start with a bit special situation with which I'm having difficulties implementing:
I want to make null wrappers valid, but not null-valued wrappers. What?! Here's an example:

Code:
@NotNull
@UnwrapValidatedValue
private Wrapper<String> field;


Now, assume Wrapper<T> has a member "value" of type T and that there's an appropriate ValidatedValueUnwrapper. What this means, is that field.value should not be null. Implicitly it also means that field should not be null, but I do not want that. Of course I can unwrap nulls in handleValidatedValue to some String, but it's not a general solution as other constraints (like Size) may be defined, also there could be other types wrapped in Wrapper. So unless there's an "always valid object" for all constraints and types, this is not going to work. :)

The issue is many-fold, but generally speaking the main problem - I believe - is that I'm trying to validate both the wrapper and the wrapped value. So maybe I'm expecting too much, but I think in practice this issue only arises for the NotNull constraint.

Here's my exact use-case, if you're interested:

I'm developing a REST-like API, where to support the differentiation between not-defined fields and fields explicitly set to null in the POST-ed JSON objects, I introduced a wrapping: If the wrapper itself is null than the field was not set (undefined), if the field was set to null than the wrapper's "value" member is set to null. To understand why does it matter, consider a (partial) update: undefined fields should not be changed, while null-valued fields should be set to null. Now if a field has a NotNull constraint, it beeing undefined is still valid (in case of an update).


Top
 Profile  
 
 Post subject: Re: Unwrapped value validation discussion
PostPosted: Wed Jun 03, 2015 11:07 am 
Newbie

Joined: Wed Jun 03, 2015 3:44 am
Posts: 7
Update: I think I solved it by validating each property one-by-one on a class level, and in the case of an update I do not call validation on null properties. Seems to be working nicely. :) I'm still open for more simple solutions though!

peterlang wrote:
Hi guys!

I didn't find any topic on this new feature (@UnwrapValidatedValue), so here's one now. :)

I'll start with a bit special situation with which I'm having difficulties implementing:
I want to make null wrappers valid, but not null-valued wrappers. What?! Here's an example:

Code:
@NotNull
@UnwrapValidatedValue
private Wrapper<String> field;


Now, assume Wrapper<T> has a member "value" of type T and that there's an appropriate ValidatedValueUnwrapper. What this means, is that field.value should not be null. Implicitly it also means that field should not be null, but I do not want that. Of course I can unwrap nulls in handleValidatedValue to some String, but it's not a general solution as other constraints (like Size) may be defined, also there could be other types wrapped in Wrapper. So unless there's an "always valid object" for all constraints and types, this is not going to work. :)

The issue is many-fold, but generally speaking the main problem - I believe - is that I'm trying to validate both the wrapper and the wrapped value. So maybe I'm expecting too much, but I think in practice this issue only arises for the NotNull constraint.

Here's my exact use-case, if you're interested:

I'm developing a REST-like API, where to support the differentiation between not-defined fields and fields explicitly set to null in the POST-ed JSON objects, I introduced a wrapping: If the wrapper itself is null than the field was not set (undefined), if the field was set to null than the wrapper's "value" member is set to null. To understand why does it matter, consider a (partial) update: undefined fields should not be changed, while null-valued fields should be set to null. Now if a field has a NotNull constraint, it beeing undefined is still valid (in case of an update).


Top
 Profile  
 
 Post subject: Re: Unwrapped value validation discussion
PostPosted: Thu Jun 04, 2015 5:12 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
Hi,
good that you have found a solution.

I just wanted to point out that if you are using JDK 8 and the latest validator, this should work:

Code:
@NotNull
@UnwrapValidatedValue(false)
private Wrapper<@NotNull String> field;


This should apply the @NotNull validation to field and then to field.value.

Cheers,
Davide


Top
 Profile  
 
 Post subject: Re: Unwrapped value validation discussion
PostPosted: Thu Jun 04, 2015 3:41 pm 
Newbie

Joined: Wed Jun 03, 2015 3:44 am
Posts: 7
Hi,

Thanks for your input! Nice feature, somehow I missed it.

Though for now I'm using the version bundled with WildFly 8/9, which is 5.1.3.Final. (Actually earlier I tried to use a different version on a application level, but I failed with that. :))

Also, if I gathered it right from the documentation, type argument constraints only work with custom constraints for now? (http://docs.jboss.org/hibernate/validator/5.2/reference/en-US/html/chapter-bean-constraints.html#type-arguments-constraints) So in your example it would only work if the @NotNull used was my implementation?


Top
 Profile  
 
 Post subject: Re: Unwrapped value validation discussion
PostPosted: Fri Jun 05, 2015 5:03 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
Quote:
Also, if I gathered it right from the documentation, type argument constraints only work with custom constraints for now?


You are right. I forgot about that.

You will have to define your own constraint, like we did to test the feature: https://github.com/hibernate/hibernate-validator/blob/master/engine-jdk8-tests/src/test/java/org/hibernate/validator/testutil/constraints/NotNullTypeUse.java


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