2

In my springboot app I'm using PATCH HTTP method to update an entity. Of course via DTO, not directly.

Since this is PATCH method I'm not all fields will be set and I want to validate (in terms of javax.validation.constraints package) only those fields that have value set (!=null).

So I have:

class SomeDTO {

   @Size(min = 5)
   String foo;
   @Min(2)
   Integer bar = 1; 

}

and need to validate bar field only, since foo has null value. What is the best mechanism for handling such requirements?

I'm using: 1.5.6.RELEASE

Opal
  • 81,889
  • 28
  • 189
  • 210
  • The Size validator won't generate any error if foo is null. – JB Nizet Apr 09 '18 at 19:39
  • @JBNizet, thanks. Is it also true for all other validators? – Opal Apr 09 '18 at 19:40
  • Yes. The basic principle is that null is always accepted. It allows more flexibility: if the user doesn't provide the information, it's accepted. If the user provides it, it must be valid. If it's mandatry, and must be valid, then you combine the validator with NotNull. – JB Nizet Apr 09 '18 at 19:41
  • @JBNizet, cool. I need to check this. Will do it on tomorrow, thanks! – Opal Apr 09 '18 at 19:42

0 Answers0