title: Dropwizard validate a field only on @POST
body: <p>I have these read-only fields which needs to be secure, like passwords. Say we have a user object:</p>
  
  <pre><code>public class User {
    @NotEmpty
    @Size(max = 100)
    private String name;
    @NotEmpty
    private String username;
    @NotEmpty
    @Email
    private String email;
    private String password;
  
    @JsonIgnore
    public String getPassword() {
      return password;
    }
  
    @JsonProperty
    public void setPassword(String password) {
      this.password = password;
    }
  }
  </code></pre>
  
  <p>So, this works nicely; as in I can get/post/put whatever I want, but I will never receive the password back. But I also want to make sure that when it's posted for the first time, password should not be empty. If I do </p>
  
  <pre><code>@NotEmpty
  private string password;
  </code></pre>
  
  <p>Then my PUT (edit) requests will fail with validation error if I don't want to change the password of this user.</p>
  
  <p><strong>There are two solutions I can think of:</strong></p>
  
  <p><strong>1-</strong> Inherit User class, create a special class only for POST which can have the @NotEmpty annotation over the getter.</p>
  
  <pre><code>public static class Create extends User {
    @NotEmpty
    @Override
    public String getPassword() {
      return password;
    }
  }
  </code></pre>
  
  <p>This should work generally but not for my codebase as it's already utilizing inheritance greatly on CRUD resources. I'd need to break and duplicate a lot of things for this approach.</p>
  
  <p><strong>2-</strong> Handle validation on the resource class:</p>
  
  <pre><code>public class UserResource {
  
    @POST
    public User createUser(User user) {
      if(user.getPassword().isEmpty()) {
        throw new ConstraintValidation....();
      }
    }
  }
  </code></pre>
  
  <p>Does the job, but not so pretty. Especially since I have 5-10 of these.</p>
  
  <p>Any other alternatives?</p>