Is it possible to create a JSR-303 constraint annotation that, applied to a list, signals invalidation on fields within elements within said list?
<p>Is it possible to do the following with JSR-303 constraint annotations?</p>

<p>Given the following bean definitions:</p>

<pre><code>public class Table {

    private List&lt;TableEntry&gt; entries;

    @NoDuplicates(path = "key")
    @Valid
    public List&lt;TableEntry&gt; getEntries() {
        return entries;
    }
    public void setEntries(List&lt;TableEntry&gt; entries) {
        this.entries = entries;
    }

}

public class TableEntry {

    private String key, value;

    @NotEmpty
    public String getKey() {
        return key;
    }
    public void setKey(String key) {
        this.key = key;
    }

    public String getValue() {
        return value;
    }
    public void setValue(String value) {
        this.value = value;
    }

}
</code></pre>

<p>@NoDuplicates would ensure that duplicate keys within entries signals invalidations on the corresponding keys.  I know it's possible to make this signal invalidation on entries itself, but I'm not sure what I want is possible.</p>

<p>Here's what I have so far:</p>

<pre><code>@Constraint(validatedBy = { NoDuplicates.Validator.class })
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
public @interface NoDuplicates {

    String message() default "{com.example.NoDuplicates.message}";
    Class&lt;?&gt;[] groups() default { };
    Class&lt;? extends Payload&gt;[] payload() default { };

    String path();

    @Named
    class Validator implements ConstraintValidator&lt;NoDuplicates, List&lt;?&gt;&gt; {

        private String path, message;

        @Override
        public void initialize(NoDuplicates noDuplicates) {
            path = noDuplicates.path();
            message = noDuplicates.message();
        }

        @Override
        public boolean isValid(List&lt;?&gt; elements, ConstraintValidatorContext context) {
            context.disableDefaultConstraintViolation();
            final Set&lt;Integer&gt; indicesOfDuplicates = indicesOfDuplicates(elements);
            for (final int index : indicesOfDuplicates) {
                reportDuplicationError(context, index);
            }
            return indicesOfDuplicates.isEmpty();
        }

        private Set&lt;Integer&gt; indicesOfDuplicates(List&lt;?&gt; elements) {
            // implementation
        }

        private void reportDuplicationError(ConstraintValidatorContext context, int index) {
            // What goes here?
        }

    }

}
</code></pre>

<p>My two main questions are:</p>

<ol>
<li>What, if anything, goes into the body of reportDuplicationError to signal invalidation on the name of the relevant entry (i.e. relative path "[${index}].name")?</li>
<li>Given a solution to question 1, is there any way to prevent NoDuplicates from signaling invalidation if the name of the relevant entry is already invalidated (in this case through the @NotEmpty, but preferably generally)?</li>
</ol>

<p>I realize both of these are possible through other validation methods, but I want to know whether it's possible through JSR-303 constraint annotations. Thank you.</p>
