title: Custom constraint validator not working when annotated bean used as a collection inside another bean
body: <p>I am creating rest service using Spring boot and to validate my DTOs, I am using java validation api.</p>
  
  <p>In some DTOs, I have to do cross field validation so I am using Custom validators.</p>
  
  <p>Here in my DTO, I have to check if both <strong>weight</strong> and <strong>weight_unit</strong> are present or both are absent. If only one of them is present then I must throw validation error.</p>
  
  <p>It works perfectly if I use this DTO in another DTO directly but fails when I use it as Collection of DTO. It throws <strong>org.springframework.beans.InvalidPropertyException</strong>.</p>
  
  <p>Custom Validator
  </p>
  
  <pre><code>@Target(ElementType.TYPE)
  @Retention(RetentionPolicy.RUNTIME)
  @Constraint(validatedBy = { ProductWeightValidatorImpl.class })
  public @interface ProductWeightValidator {
  
      String message() default "{com.inventory.validator.ProductWeightValidator.message}";
  
      Class&lt;?&gt;[] groups() default {};
  
      Class&lt;? extends Payload&gt;[] payload() default {};
  }
  </code></pre>
  
  <p>Custom Validator Implementation
  </p>
  
  <pre><code>public class ProductWeightValidatorImpl implements ConstraintValidator&lt;ProductWeightValidator, ProductDTO&gt; {
  
      @Override
      public void initialize(ProductWeightValidator constraintAnnotation) {
  
      }
  
      @Override
      public boolean isValid(ProductDTO product, ConstraintValidatorContext context) {
          return Objects.nonNull(product.getWeight()) &amp;&amp; StringUtil.nullOrEmpty(product.getWeightUnit()) || Objects.isNull(product.getWeight()) &amp;&amp; StringUtil.nonNullNonEmpty(product.getWeightUnit());
      }
  }
  </code></pre>
  
  <p>Bean on which validator is applied
  </p>
  
  <pre><code>@ProductWeightValidator(message = "weight and weight_unit must be provided together")
  public class ProductDTO extends BaseDTO {
  
      // other fields
  
      private Double weight;
  
      @JsonProperty("weight_unit")
      private String weightUnit;
  
      //getters and setters
  }
  </code></pre>
  
  <p>Other bean where this bean is used as collection
  </p>
  
  <pre><code>public class ProductListDTO extends BaseDTO {
  
      @Valid
      private List&lt;ProductDTO&gt; products = new ArrayList&lt;&gt;(0);
  
      //getters and setters
  
  }
  </code></pre>
  
  <p>Now when I pass only weight or weight_unit in request, it throws exception <strong>Invalid property 'products[]' of bean class [com.inventory.DTO.ProductListDTO]: Invalid index in property path 'products[]'</strong></p>