jakartaee / jakartaee/validation-spec

Array elements can't be validated

Open
#282 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
23
Forks
22
PR merge metrics
No merged PRs in 30d

Description

Hi,

I'm trying to validate each element of an array, rather than the size of said array.

According to [jls§9.7.4](https://docs.oracle.com/javase/specs/jls/se22/html/jls-9.html#jls-9.7.4), annotations can be used on arrays like this:

Type annotations can apply to an array type or any component type thereof ([§10.1](https://docs.oracle.com/javase/specs/jls/se22/html/jls-10.html#jls-10.1)). For example, assuming that A, B, and C are annotation interfaces meta-annotated with `@Target(ElementType.TYPE_USE)`, then given the field declaration:

@C int @A [] @B [] f;

`@A` applies to the array type int[][], `@B` applies to its component type int[], and `@C` applies to the element type int. For more examples, see [§10.2](https://docs.oracle.com/javase/specs/jls/se22/html/jls-10.html#jls-10.2).

So essentially, these two declarations are "equivalent" according to the JLS:

```java
private @NotEmpty List<@Size(min=1,max=3) String> myList;

private @Size(min=1,max=3) String @NotEmpty[] myArray;
```

It seems to me that currently it is only possible to validate the length of an array, but not validate each element inside this array. I also have the impression that the current behavior is different from what is described in the above JLS excerpt, and also different from how `List`s are validated:
* `@Size(min = 1, max = 3) String[]` validates the array length whereas it should in theory validate each `String` element
* `String @Size(min = 1, max = 3)[]` should validate the array length but it does nothing at the moment.

See this sample project for JUnit test cases to reproduce the problem: https://github.com/bjansen/arrays-validation

Contributor guide

Open the contributing guide

Research direction

Start with the linked arrays-validation sample project and its JUnit reproduction cases, then read the referenced JLS sections 9.7.4, 10.1, and 10.2. Compare the reported array behavior with the List examples and determine the intended type-use annotation semantics; done means the specification clearly resolves validation of array elements versus array length.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.