Extracting this information to an additional (optional) annotation will be a problem since this can end in such an expression:
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.
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“).
Here are some code snippets that use additional information in the annotations to define the validation:
@NotEmpty
@NotEmpty(applyTo = ConstraintsApplyTo.INTERNALS)
@NotEmpty(applyTo = ConstraintsApplyTo.INTERNALS, applyToInternalDepth = 2)
@NotEmpty(applyTo = ConstraintsApplyTo.INTERNALS, applyToInternalDepth = 3)
private List<List<List<StringProperty>>> spooky;
@NotEmpty(applyTo = ConstraintsApplyTo.INTERNALS, valueContainerClass = RealValue.class)
private RealValue<String> myValue;The „applyToInternalDepth“ value is used to define how often the validator should get the internal value (depth). In the given example „@NotEmpty(applyTo = ConstraintsApplyTo.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.
Based on this a constraint annotation needs this additional fields:
ConstraintsApplyTo applyTo() default ConstraintsApplyTo.VALUE; // VALUE or INTERNALS
int applyToInternalDepth() default 1;
Class<?> valueContainerClass() default Object.class;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.
I think it’s quite important to have support for all the wrapper classes. Validation collections and (JavaFX) properties will be a big benefit.
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); }
}
I now that some of the code samples will not compile (for example several annotations of same type for element) but I think the code is more easy to read by doing it that way ;)
Cheers,
Hendrik
_______________________________________________
beanvalidation-dev mailing list
beanvalidation-dev@lists.jboss.orghttps://lists.jboss.org/mailman/listinfo/beanvalidation-dev