<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">2016-11-21 17:08 GMT+01:00 Emmanuel Bernard <span dir="ltr">&lt;<a href="mailto:emmanuel@hibernate.org" target="_blank">emmanuel@hibernate.org</a>&gt;</span>:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word"><br><div><blockquote type="cite"><div>On 7 Nov 2016, at 23:42, Hendrik Ebbers &lt;<a href="mailto:hendrik.ebbers@me.com" target="_blank">hendrik.ebbers@me.com</a>&gt; wrote:</div><div><div style="word-wrap:break-word"><br>And this is not the only example. We already have some classes in JavaSE that will end in such problems:<br>The basic Property class in JavaFX can be used like this:<br><br><font face="Courier New">private Property&lt;String&gt; <wbr>myStringProperty;</font><br><br>Based on the current approach we should add a constraint annotation like this:<br><font face="Courier New"><br>private Property&lt;@NotNull String&gt; myStringProperty;</font><br><br>But next to the property class JavaFX contains the class StringProperty that is a specific implementation of „Property&lt;String&gt;„. If we want to use this class we can not add a annotation to a generic type since we don’t have such a generic type. Here we need to do it this way:<br><br><font face="Courier New">@NotNull <br>private StringProperty <wbr>myStringProperty2;</font><br><br>And again we have 2 different approaches:<br><br><font face="Courier New">private Property&lt;@NotNull String&gt; myStringProperty;<br><br>@NotNull<br>private StringProperty <wbr>myStringProperty2;</font><br><br>Based on this I do not think that it’s a good idea to use the constraint annotation for generic types to validate the content of a wrapper.</div></div></blockquote><div><br></div><div>General comment on you entire email.</div><div><br></div><div>I think you are throwing the baby out with the bath water. Granted, more complicated cases don’t scale as nicely as we would hope but let’s get back tot he basics. 95%+ of this feature usage will be done for collections and maps. Based on this, I’d be a bit unhappy at giving up parameterized type use location for landing that annotations. This is sooooo natural.</div><div><br></div><div>Our job is to make the common case simple and ideally make it scale in a not too ugly way and not close doors to the most complex use cases. And if one thing has to break, we can drop some of the less common cases.</div></div></div></blockquote><div><br></div><div>+100. </div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word"><div><div>More specifically to this question, the proposal favored the use of parameterized type use but offers a backup plan for when the parameterized type use location is not present (like specialized subclasses of Java FX). Java FX is a clear cut as the wrapper is purely technical and all of the constraints do apply to the wrapped value.</div><div>But everywhere we have a parameterized type use, we should have the app developer make use of them instead of using the @ConstraintAppliesTo workaround.</div><div><br></div><br><blockquote type="cite"><div><div style="word-wrap:break-word"> To be true I do not have the ultimative solution for this problem. Currently I trying to add some metadata to the annotations to define it’s goal. Here is an example:<br><br><font face="Courier New">@NotEmpty<br>private List&lt;String&gt; names1;<br><br>@NotEmpty(applyTo = ConstraintsApplyTo.INTERNALS)<br>private List&lt;String&gt; names2;<br><br>@NotEmpty<br>@NotEmpty(applyTo = ConstraintsApplyTo.INTERNALS)<br>private List&lt;String&gt; names3;</font><br><br>Here the „applyTo“ value was added to the @NotEmpty annotation (like bean validation already does with the groups). By using this value a developer can specify if he wants to validate the value of the field or its internals. By doing so „names1“ is valid if the list contains at leas 1 element, „names2“ is valid if all elements in the list have at least a length of 1 (an empty list is allowed) and in „names3“ the list must contain at least one element and all elements must have a length &gt; 0.<br><br>A similar approach is already defined in the proposal and I understand why this is not the perfect solution since you need to define it whenever you do not want to valide a plain value:<br><br><font face="Courier New">@NotNull(applyTo = ConstraintsApplyTo.INTERNALS)<br>private StringProperty <wbr>myStringProperty2;</font><div><br></div><div>Extracting this information to an additional (optional) annotation will be a problem since this can end in such an expression:</div><div><br></div><div><span style="font-family:&quot;courier new&quot;">@NotEmpty</span></div><div><font face="Courier New">@ConstraintsApplyTo(CONTAINED_<wbr>VALUES)</font><br style="font-family:&quot;courier new&quot;"><span style="font-family:&quot;courier new&quot;">@NotEmpty</span><br style="font-family:&quot;courier new&quot;"><span style="font-family:&quot;courier new&quot;">private </span><span style="font-family:&quot;courier new&quot;">List&lt;String&gt; </span><span style="font-family:&quot;courier new&quot;">names3</span><span style="font-family:&quot;courier new&quot;">;</span></div><div><br></div><div>By doing so you can not define the same constraints type for the container and for the containing value. In the example above the same is working. </div><div><br></div></div></div></blockquote><div><br></div><div>We did explore per annotation location overriding. This is a question in the initial proposal. The reason it did not make the first cut is as you have seen:</div><div><br></div><div>- you need a lot of information on side that it not trivial to digest</div><div>- each existing constraint annotation will have to be changed to add these new attributes</div><div>    - on that front, the spec has declared that attribute names starting with “valid” are reserved for the spec, so your proposal would have to have attributes prefixed with “valid” not to break existing code</div><div>- at the end of the day, the feature is really for code using type use, so we are twisting it for edge cases</div><div><br></div><div>But I am very open to the idea of doing per annotation location overriding if people think that they are an important feature.</div><br><blockquote type="cite"><div><div style="word-wrap:break-word"><div>Next to the „applyTo“ value all the constraint annotations needs additional information to really validate all the given examples. In addition a plugin must be present that defines how we can get the internal value(s) of a class (in this case „StringProperty“).<br>Here are some code snippets that use additional information in the annotations to define the validation:<br><br><br><font face="Courier New">@NotEmpty<br>@NotEmpty(applyTo = ConstraintsApplyTo.INTERNALS)<br>@NotEmpty(applyTo = ConstraintsApplyTo.INTERNALS, applyToInternalDepth = 2)<br>@NotEmpty(applyTo = ConstraintsApplyTo.INTERNALS, applyToInternalDepth = 3)<br>private List&lt;List&lt;List&lt;<wbr>StringProperty&gt;&gt;&gt; spooky;<br><br>@NotEmpty(applyTo = ConstraintsApplyTo.INTERNALS, valueContainerClass = RealValue.class)<br>private RealValue&lt;String&gt; <wbr>myValue;</font><br><br>The „applyToInternalDepth“ value is used to define how often the validator should get the internal value (depth). In the given example „@NotEmpty(applyTo = ConstraintsApplyTo.<wbr>INTERNALS, applyToInternalDepth = 3)“ would validate the content of the „StringProperty“. In addition we need some information for the container if the container implements several interfaces. This could be defined in the annotation, too. In the example this is defined by the „valueContainerClass“ value.<br></div></div></div></blockquote><div><br></div><div>Hopefully we can converge on my idea not to have to add valueContainerClass (see the other emails). But you do need a way to identify the correct type parameter targeted. This is equivalent to the @ExtractedValue which today has typeParameterHost and either typeParameterIndex or typeParameterName to uniquely identify them.</div><div><br></div><div>On the depth approach, your example would be more easily rewritten as </div><div><br></div><div>    @NotEmpty List&lt;@NotEmpty List&lt;@NotEmpty List&lt;@NotEmpty StringProperty&gt;&gt;&gt; spooky;</div><div><br></div><div>which clarifies everything. But let’s assume we have a type ListOfListOfListOfStringProper<wbr>ty type. do we really want to support</div><div><br></div><div>@NotEmpty<br>@NotEmpty(applyTo = ConstraintsApplyTo.INTERNALS)<br>@NotEmpty(applyTo = ConstraintsApplyTo.INTERNALS, applyToInternalDepth = 2)<br>@NotEmpty(applyTo = ConstraintsApplyTo.INTERNALS, applyToInternalDepth = 3)<br>private <wbr>ListOfListOfListOfStringProper<wbr>ty spooky;</div><div><br></div><div>or even</div><div><br></div><div>@NotEmpty</div><div>// support nested levels by positioning ConstraintsApplyTo at the right level<br>private List&lt;@NotEmpty List&lt;@NotEmpty @NotEmpty(applyTo = ConstraintsApplyTo.CONTAINED_<wbr>VALUES) ListOfStringProperty&gt;&gt; spooky;</div><div><br></div><div>Or do we throw the towel and say that nested levels are not supported for non parameterized type use locations? What I am realizing now is that this option is available essentially to support containers that do not offer parameterized type. And in a previous email, I am questioning that use case.</div></div></div></blockquote><div><br></div><div>I&#39;m also not convinced of this. In a way it seems to me like breaking encapsulation.</div><div><br></div><div>When dealing with a type such as ListOfStringListsProperty, you don&#39;t know it&#39;s inner works from a variable declaration. Hence I&#39;m doubting that you should be allowed to apply constraints to them from the outside. It&#39;s getting more apparent when thinking of more real-world domain-specific types:</div><div><br></div><div>    public class CustomerContacts {</div><div>        List&lt;List&lt;String&gt;&gt; contacts; // one single inner list contains the contacts valid at a given time</div><div>    }</div><div><br></div><div>I don&#39;t think it makes sense to allow application of constraints &quot;from the outside&quot; when dealing with a property of CustomerContacts. It makes assumptions on the implementation of that type which is a dangerous thing to do. What if the implementation of CustomerContacts changes to be map-based? Existing constraint configurations given at the usage side would have to be adapted then.</div><div><br></div><div>Instead the constraints should be declared within CustomerContacts itself.</div><div><br></div><div>It&#39;s different for generic types. When dealing with a type like List&lt;List&lt;String&gt;&gt;, its &quot;inner workings&quot; are apparent to the user, i.e. it&#39;s reasonable to allow them to apply constraints to the elements of the inner list for instance.</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word"><div><blockquote type="cite"><div><div style="word-wrap:break-word"><div>Based on this a constraint annotation needs this additional fields:<br><br><font face="Courier New">ConstraintsApplyTo applyTo() default <wbr>ConstraintsApplyTo.VALUE;  // <wbr>VALUE or INTERNALS<br><br>int applyToInternalDepth() <wbr>default  1;<br><br>Class&lt;?&gt; valueContainerClass() default <wbr>Object.class;</font><br><br>As I said I don’t have a good solution :( But I think that based on the given issues simply adding annotations to the generic types isn’t the best idea.<br><br>I think it’s quite important to have support for all the wrapper classes. Validation collections and (JavaFX) properties will be a big benefit. <br></div></div></div></blockquote></div><br></div><br>______________________________<wbr>_________________<br>
beanvalidation-dev mailing list<br>
<a href="mailto:beanvalidation-dev@lists.jboss.org">beanvalidation-dev@lists.<wbr>jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/beanvalidation-dev" rel="noreferrer" target="_blank">https://lists.jboss.org/<wbr>mailman/listinfo/<wbr>beanvalidation-dev</a><br></blockquote></div><br></div></div>