kotlin data class + bean validation jsr 303

Viewed 31121

I'm trying to get Kotlin working with jsr 303 validation on a spring-data-rest project.

Given the following data class declarartion :

@Entity data class User(
    @Id 
    @GeneratedValue(strategy = javax.persistence.GenerationType.AUTO)
    var id: Long? = null,

    @Size(min=5, max=15)
    val name: String
)

The @Size annotation has no effect here, making me able to save a user with a name of 1 character.
It works well when executing the very same example but in a Java class instead of Kotlin.

This makes me think of a Kotlin problem.

Thanks in advance for you help !

2 Answers

Use the @get or @field targets for validation annotations. Annotations with the target @param(first default) and @property are not supported.

e.g:

From @NotEmpty To @field:NotEmpty

data class Student(
    @field:NotEmpty @field:Size(min= 2, message = "Invalid field") var name: String? = ""
)

GL

Related