title: Javax Validation: Constraint Violations for Map
body: <p>I use Map for localized values with locale as a key and String as value. For required fields I need to check that at least required locales are set - or at least some value is set. I have implemented validation annotation to be used on such Map fields and a corresponding validator. The problem is, how can I report a missing value? The property path that is used in UI to bind field errors/values, goes wrong each time:</p>
  
  <pre><code>// Domain object:
  @LocalizationRequired
  private Map&lt;Locale, String&gt; field;
  
  
  // LocalizationRequiredValidator:
  public boolean isValid(Map&lt;Locale, String&gt; map, ConstraintValidatorContext context) {
      if (requiredLocales.isEmpty()) {
          // Check that there exists any not null value
      } else {
          context.disableDefaultConstraintViolation();
          boolean valid = true;
          for (Locale requiredLocale : requiredLocales) { 
              if (map.get(requiredLocale) == null) { // e.g. fi
                  valid = false;
                  context.buildConstraintViolationWithTemplate("LocalizationRequired")
                  // These end up in wrong property path:
                  // .addNode(requiredLocale) 
                  //    --&gt; field.fi
                  // .addNode("[" + requiredLocale + "]") 
                  //    --&gt; field.[fi]
                  // .addNode(null).addNode(requiredLocale).inIterable() 
                  //    --&gt; field.fi
                  // .addNode(null).addNode(null).inIterable().atKey(requiredLocale)
                  //   --&gt; field
                  .addConstraintViolation();
              }
          }
          return valid;
      }
  }
  </code></pre>
  
  <p>The correct path for this error is "field[fi]" but it appears I can only access indexed sub properties. In this case the object itself is indexed. I'm using Hibernate Validator.</p>