bean validation not working with kotlin (JSR 380)

Answer (Kotlin 1.3.70)

Make sure to compile the kotlin code with jvm target 1.8 or greater and enable this feature by providing the -Xemit-jvm-type-annotations when compiling.

For Spring Boot projects you only have to do the following changes (tested with Spring Boot 2.3.3 and Kotlin 1.4.0):

  1. In your pom set the following property:
  2. Add <arg>-Xemit-jvm-type-annotations</arg> to the kotlin-maven-plugin:

Sample Project

Jetbrains Release Notes

Workaround (pre Kotlin 1.3.70)

Rafal G. already pointed out that we could use a custom validator as a workaround. So here's some code:

The Annotation:

import javax.validation.Constraint
import javax.validation.Payload
import kotlin.annotation.AnnotationTarget.ANNOTATION_CLASS
import kotlin.annotation.AnnotationTarget.CONSTRUCTOR
import kotlin.annotation.AnnotationTarget.FIELD
import kotlin.annotation.AnnotationTarget.FUNCTION
import kotlin.annotation.AnnotationTarget.TYPE_PARAMETER
import kotlin.annotation.AnnotationTarget.VALUE_PARAMETER
import kotlin.reflect.KClass

@Constraint(validatedBy = [NoNullElementsValidator::class])
annotation class NoNullElements(
    val message: String = "must not contain null elements",
    val groups: Array<KClass<out Any>> = [],
    val payload: Array<KClass<out Payload>> = []

The ConstraintValidator:

import javax.validation.ConstraintValidator
import javax.validation.ConstraintValidatorContext

class NoNullElementsValidator : ConstraintValidator<NoNullElements, Collection<Any>> {
    override fun isValid(value: Collection<Any>?, context: ConstraintValidatorContext): Boolean {
        // null values are valid
        if (value == null) {
            return true
        return { it == null }

And finally the updated User class:

data class User(
    var roles: MutableSet<Role> = HashSet()

Altough validation works now, the resulting ConstrainViolation is slightly different. For example the elementType and propertyPath differs as you can see below.


The Java Version


The Kotlin Version

Source is available here:

Thanks again for your help Rafal G.

Try adding ? like this:

data class User(
    var roles: MutableSet<@NotNull Role?> = HashSet()

Then the kotlin compiler should realise roles could be null, and it might honor the validation, I know little about JSR380 so i'm just guessing though.