Custom Bean Validation (JSR-303) Annotations

Ok, I misspoke. The annotation was being picked up just fine, I was just missing some fields. Custom annotations deriving from javax.validation.Constraint are expected to have the following methods:

  • String message()
  • Class<?> groups
  • Class<? extends Payload> payload()

I was able to get it to work by providing these fields to the kotlin annotation’s constructor:

@MustBeDocumented
@Constraint(validatedBy = [])
@Target(
    AnnotationTarget.FUNCTION, AnnotationTarget.FIELD, AnnotationTarget.ANNOTATION_CLASS,
    AnnotationTarget.PROPERTY_GETTER
)
@Retention(AnnotationRetention.RUNTIME)
@Pattern(regexp = "[012345]")
@ReportAsSingleViolation
annotation class ExampleCustomAnnotation(
    val message: String = "foo",
    val groups: Array<KClass<out Any>> = [],
    val payload: Array<KClass<out Any>> = []
)

1 Like