Search code examples
javavalidationspring-boot

LocalDate field validation does not work on DTO


I'm doing some testing with field validation using a dto and annotating a LocalDate variable with @NotEmpty on a Spring boot api, however, I'm getting this error:

javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'javax.validation.constraints.NotEmpty' validating type 'java.time.LocalDate'. Check configuration for 'dateBirth'

The annotation library is javax.validation.constraints.NotEmpty, had tried with javax.validation.constraints.NotEmpty, however, so the annotation was marked as deprecated.

Declaration of the variable:

@NotEmpty(message = "Campo Data de Nascimento é obrigatório")
private LocalDate dateBirth;

Is it necessary to add some dependency in pom.xml or something?


Solution

  • Doc says @NotEmpty supports only type of CharSequence, Collection, Map, or Array, so LocalDate doesn't fall into any of these type. For null check you should just use a @NotNull as it validates against any type. There is also @Past that validates if a date is in the past (in your case dateBirth).