[bv-dev] On the support for Optional (was Re: Support for constraints on container values (e.g. Collection<@Email String>))

Emmanuel Bernard emmanuel at hibernate.org
Mon Nov 21 08:32:43 EST 2016


> On 7 Nov 2016, at 23:42, Hendrik Ebbers <hendrik.ebbers at me.com> wrote:
> 
> I don’t think that support for Optional is that important since having a field of type Optional looks like an anti-pattern to me. Normally Optional should be used as a method return type and not as a type for a field. Supporting it in the bean validation might end in strange model classes. 
> 
> Example:
> 
> The following model looks good to me:
> 
> public class Model {
> 
> 	@NotNull
> 	private String name; 
> 
> 	public String getName() { return name; }
> 	
> 	public void setName(String name) { this.name = name; }
> 
> 	public Optional<String> name() { return Optional.ofNullable(name);}
> 
> }
> 
> On the other hand this looks like an anti-pattern to me:
> 
> public class Model {
> 
> 	@NotNull
> 	private Optional<String> name; 
> 
> 	public Optional<String> getName() { return name; }
> 	
> 	public void setName(String name) { this.name = Optional.ofNullable(name); }
> 
> }

Yes Optional on a property is deemed an anti pattern by the JDK team but since Bean Validation supports contraints on method parameters and return values, this is still a valid use case

Optional<@Email String> getUserEmail(@NotNull UUID userId);

Emmanuel

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/beanvalidation-dev/attachments/20161121/553e43bf/attachment-0001.html 


More information about the beanvalidation-dev mailing list