4

I would like to be able to validate the primitives inside a list and I was wondering if there's a standard way of doing it. For example validating the size of the Strings inside a list.

An example:

@Size(max = 64)  // validates that a string is max 64 chars.
private String name;

@Size(max = 64)  // validates that the list size is max 64 items.
private List<String> names;

// What I want:
@ValidateInsidePrimitives(
   @Size(max = 64)
)
private List<String> names;

What I've seen people do is wrap the primitives into an object do do this:

@Valid
private List<NameObj> names;

OR making custom annotations that validate a list of strings:

@MyCustomListStringSizeAnnotation(max = 64) 
private List<String> names;

OR using the java 8 new annotations placement like so:

private List<@Size(max = 64) String> names;

But I don't really like the first 2 approaches, and I CANNOT port my app to java 8.

It seems to me that the "@ValidateInsidePrimitives" I drafted above should work (after all the *.List annotations do take annotations) but I wasn't able to find something like that. Any ideas?

2
  • Did you tried Custom Constrains? docs.jboss.org/hibernate/validator/4.0.1/reference/en/html/… Commented Jun 9, 2015 at 20:14
  • I've stated above that that IS a possibility, I gave the example with "@MyCustomListStringSizeAnnotation(max = 64) ". And I've stated below that I don't like this approach. Commented Jun 9, 2015 at 21:01

2 Answers 2

2

The problem with @ValidateInsidePrimitives is that it cannot be implemented in a generic manner.

Annotation members (such as value()) can only have a specific other annotation type as type, but not java.lang.annotation.Annotation (that's the reason why there is a dedicated @List constraint for each BV built-in constraint). So you could implement @ValidateInsidePrimitives for @Size, but would have to do so for each other basic constraint to be supported.

You are better off implementing the custom @MyCustomListStringSizeAnnotation constraint if you cannot migrate to Java 8 yet.

Sign up to request clarification or add additional context in comments.

Comments

0

You can take advantage of Java 8 type annotations even if you cannot port your app to Java 8.

In particular, you can write a pluggable type-checker using the Checker Framework. Then, in your code you write the annotations in comments, like this:

List</*@Size(max=64)*/ String> 

The Checker Framework processes annotations in comments, just as if you had written them without the comments. However, an ordinary Java compiler just sees and ignores the comments, so your code continues to compile with any Java compiler.

At compile time, the Checker Framework runs your pluggable type-checker and enforces the semantics that you specified.

Note that the Checker Framework does type-checking to give you a compile-time guarantee about what values will ever be put in your list. It does not add run-time asserts to your code so that your code will raise an error if an undesired value is put in your list. If you want run-time checking, you will need to extend the Checker Framework or to use a different tool.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.