Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: [Validator] @Valid for custom type wrappers
PostPosted: Thu Mar 15, 2012 4:17 pm 
Newbie

Joined: Thu Mar 15, 2012 4:05 pm
Posts: 5
Hi,

Bean validation defines @Valid semantics for
Code:
Collection<T>, Map<K, V>
. Is it possible to define @Valid semantics for custom type wrappers, e.g. types popular in the functional world
Code:
Option<T>, Either<L, R>
or a custom
Code:
Holder<T>
?

For example, I want
Code:
@Valid Option<T>
to cascade validation to the wrapped value if it exists (Option<T> is a wrapper that contains either a value of type T or null).

The above looks similar to data binders which can be extended for custom types.

Thanks,
George.


Top
 Profile  
 
 Post subject: Re: [Validator] @Valid for custom type wrappers
PostPosted: Sat Mar 17, 2012 10:50 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 176
The @Valid annotation can be applied to collection typed fields or properties (lists, maps etc.) as well as to simple references to other objects. Taking your Holder class as example, you just have to annotate the wrapped object with @Valid:

Code:
public class Holder<T> {

    @Valid
    private T wrappedobject;

    //...
}


If then a Holder instance is validated (e.g. because it's annotated itself with @Valid) a recursive validation of the object graph and thus the wrapped value will be performed. The HV reference guide provides some more background information.

--Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
 Post subject: Re: [Validator] @Valid for custom type wrappers
PostPosted: Sat Mar 17, 2012 2:45 pm 
Newbie

Joined: Thu Mar 15, 2012 4:05 pm
Posts: 5
Your approach requires that I can modify the type's source code. What about stock types e.g. Guava's Optional<T> ?


Top
 Profile  
 
 Post subject: Re: [Validator] @Valid for custom type wrappers
PostPosted: Sat Mar 17, 2012 7:27 pm 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 176
You might try to mark the concerned reference as to be validated recursively using either an XML based mapping or a programmatically created constraint mapping using HV's constraint API. Using the latter approach you might mark the "reference" field of the Present class (sub-type of Guava's Optional type actually containing a value) like this:

Code:
ConstraintMapping mapping = new ConstraintMapping();
mapping.type( Present.class )
    .property( "reference", FIELD )
        .valid();

HibernateValidatorConfiguration config = Validation.byProvider( HibernateValidator.class ).configure();
config.addMapping( mapping );
Validator validator = config.buildValidatorFactory().getValidator();

--Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
 Post subject: Re: [Validator] @Valid for custom type wrappers
PostPosted: Sat Mar 17, 2012 10:03 pm 
Newbie

Joined: Thu Mar 15, 2012 4:05 pm
Posts: 5
Thanks for your reply. However, Present.class is private to Optional.class and I cannot access it in order to add the custom constraint. I can imagine this eventually working for Optional<T> using reflection or what else, but it's already becoming more difficult that it should be in my opinion, which leads me to my initial point:

Why not add an SPI that allows hibernate validator to be extended for custom types ? Validator core could even be re-factored against such an SPI (providing SPI implementations for collection, maps, java beans, method parameters i.e. tuples). I know this is hand-wavy and easier said than done but have you considered adding such an SPI ?

regards,
George.


Top
 Profile  
 
 Post subject: Re: [Validator] @Valid for custom type wrappers
PostPosted: Sun Mar 18, 2012 5:46 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 176
Hi,

I'm not yet completely understanding what such an SPI would do. What exactly do you mean by "allow HV to be extended for custom types"? Maybe you could write down some examplary (pseudo) code of the SPI and code based on it?

Thanks,

--Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
 Post subject: Re: [Validator] @Valid for custom type wrappers
PostPosted: Sun Mar 18, 2012 10:05 am 
Newbie

Joined: Thu Mar 15, 2012 4:05 pm
Posts: 5
The basic idea is to have a simple SPI:

Code:
public interface CompositeValidator<T> {

   void validate(T value, CompositeValidatorContext context);

}


When HV comes across a @Valid annotation it looks up a CompositeValidator<T> by the type annotated with @Valid and calls the validate() method. Validation across Optional<T> would be implemented as:

Code:
public class OptionalValidator implements CompositeValidator<Optional<?>> {

   // This is an internal HV factory
   private final CompositeValidatorProvider provider;

   // This is an annotations/type decriptor
   // For bean property / method parameter / list item / map entry / optional value etc
   private final Element element;

   public OptionalValidator(CompositeValidatorProvider provider, Element element) {
      this.provider = provider;
      this.element = element;
   }

   @Override
   public void validate(Optional<?> value, CompositeValidatorContext context) {
      if (value.isPresent()) {
         Element presentElement = element.getElementArgument(0);
         provider.get(presentElement).validate(value.get(), context);
      }
   }

}


As I said already, I think the above could also be used for Collection<T>, Map<K,V>, but I think it could be generalised for beans, method parameters as well.

Hope the above make sense.

regards,
George


Top
 Profile  
 
 Post subject: Re: [Validator] @Valid for custom type wrappers
PostPosted: Fri Mar 23, 2012 6:33 am 
Newbie

Joined: Thu Mar 15, 2012 4:05 pm
Posts: 5
Filed https://hibernate.onjira.com/browse/HV-565


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 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.