title: Validate List<Object> using Spring validator
body: <p>Question: how to set proper nested path for Errors for List target in validator to invoke other validator ?</p>
  <p>Code reference: <a href="https://stackoverflow.com/questions/12680730/validate-a-list-of-nested-objects-with-spring-validator/25317726">nested spring validator for nested object</a></p>
  <pre><code>public class AllergyDtoListValidator implements Validator {
  
    private AllergyDtoValidator allergyDtoValidator;
  
    @Override
    public boolean supports(Class&lt;?&gt; clazz) {
      return new TypeToken&lt;List&lt;AllergyDto&gt;&gt;() {
      }.getType().equals(clazz);
    }
  
    @Override
    public void validate(Object target, Errors errors) {
      allergyDtoValidator = new AllergyDtoValidator();
      List&lt;AllergyDto&gt; request = (List&lt;AllergyDto&gt;) target;
      for (int i = 0; i &lt; request.size(); i++) {
        // below line causes the exception
        errors.pushNestedPath(String.valueOf(i));
        ValidationUtils.invokeValidator(this.allergyDtoValidator, request.get(i), errors);
        errors.popNestedPath();
      }
    }
  }
  
  public class AllergyDtoValidator implements Validator {
  
    @Override
    public boolean supports(Class&lt;?&gt; clazz) {
      return AllergyDto.class.isAssignableFrom(clazz);
    }
  
    @Override
    public void validate(Object target, Errors errors) {
  
      AllergyDto request = (AllergyDto) target;
      log.info(&quot;Enter allergy validator {}&quot;, request.toString());
      try {
        // field allergyTypeCode
        if (request.getAllergyTypeCode() == null || request.getAllergyTypeCode().isEmpty()) {
          errors.rejectValue(&quot;allergyTypeCode&quot;, &quot;Cannot be null nor whitespace&quot;);
        } else if (request.getAllergyTypeCode().length() &gt; 8) {
          errors.rejectValue(&quot;allergyTypeCode&quot;, &quot;Exceed max length&quot;);
        }
        // field allergyCode
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, &quot;allergyCode&quot;, &quot;Cannot be null nor &quot;
            + &quot;whitespace&quot;);
        if (request.getAllergyCode() != null &amp;&amp; request.getAllergyCode().length() &gt; 8) {
          errors.rejectValue(&quot;allergyCode&quot;, &quot;Exceed max length&quot;);
        }
        // field allergyName
        if (request.getAllergyName() != null &amp;&amp; request.getAllergyName().length() &gt; 66) {
          errors.rejectValue(&quot;allergyName&quot;, &quot;Exceed max length&quot;);
        }
        // field remarks
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, &quot;remarks&quot;, &quot;Cannot be null nor whitespace&quot;);
        if (request.getRemarks() != null &amp;&amp; request.getRemarks().length() &gt; 255) {
          errors.rejectValue(&quot;remarks&quot;, &quot;Exceed max length&quot;);
        }
      } catch (Exception e) {
        log.info(&quot;Exception {}&quot;, e);
      }
      log.info(&quot;allergy errors {}&quot;, errors.toString());
  
    }
  
  }
  
  public class AllergyDto implements Serializable {
    private static final long serialVersionUID = 2319040437020676767L;
    @JsonProperty(&quot;allergy_type_code&quot;)
    private String allergyTypeCode;
    @JsonProperty(&quot;allergy_code&quot;)
    private String allergyCode;
    @JsonProperty(&quot;allergy_name&quot;)
    private String allergyName;
    @JsonProperty(&quot;remarks&quot;)
    private String remarks;
    // getters and setters
  
  }
  </code></pre>
  <p>when I run</p>
  <pre><code>// req is List&lt;AllergyDto&gt;
  allergyDtoListValidator.validate(req, bindingResult);
  </code></pre>
  <p>I got exception</p>
  <blockquote>
  <p>org.springframework.beans.NotReadablePropertyException: Invalid
  property '0' of bean class [java.util.ArrayList]: Bean property '0' is
  not readable or has an invalid getter method: Does the return type of
  the getter match the parameter type of the setter?    at
  org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:627)
  at
  org.springframework.beans.AbstractNestablePropertyAccessor.getNestedPropertyAccessor(AbstractNestablePropertyAccessor.java:843)
  at
  org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyAccessorForPropertyPath(AbstractNestablePropertyAccessor.java:820)
  at
  org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:615)
  at
  org.springframework.validation.AbstractPropertyBindingResult.getActualFieldValue(AbstractPropertyBindingResult.java:104)
  at
  org.springframework.validation.AbstractBindingResult.getFieldValue(AbstractBindingResult.java:228)
  at
  org.springframework.validation.ValidationUtils.rejectIfEmptyOrWhitespace(ValidationUtils.java:252)
  at
  org.springframework.validation.ValidationUtils.rejectIfEmptyOrWhitespace(ValidationUtils.java:191)
  at
  sg.gov.hpb.yhms.medical_service.validators.AllergyDtoValidator.validate(AllergyDtoValidator.java:32)
  at
  org.springframework.validation.ValidationUtils.invokeValidator(ValidationUtils.java:89)
  at
  org.springframework.validation.ValidationUtils.invokeValidator(ValidationUtils.java:56)
  at
  sg.gov.hpb.yhms.medical_service.validators.AllergyDtoListValidator.validate(AllergyDtoListValidator.java:32)</p>
  </blockquote>