https://kotlinlang.org logo
Title
e

Eric

05/12/2023, 2:41 PM
How to validate a class not under my control in a web flux controller? I'm implementing PATCH and the controller function accepts a
@RequestBody @ValidJsonPatch patch: JsonPatch
I've created the validation annotation and constraint validator:
@Constraint(validatedBy = [JsonPatchValidator::class])
annotation class ValidJsonPatch(
    val message: String = "",
    val groups: Array<KClass<*>> = [],
    val payload: Array<KClass<out Payload>> = [],
)

class JsonPatchValidator : ConstraintValidator<ValidJsonPatch, JsonPatch> {
    override fun isValid(value: JsonPatch, context: ConstraintValidatorContext): Boolean { ... }
}
But the validator never gets invoked. I've added
_implementation_("org.springframework.boot:spring-boot-starter-validation")
to my dependencies and standard jakarta validation annotations work. edit: scratch that... standard validation annotations are also not working.
j

Jacob

05/12/2023, 2:58 PM
might need to add an @Validated to the controller function param
e

Eric

05/12/2023, 3:04 PM
Nope. And sorry for the kotlin-not-kotlin Q. Realized that after.
Feels like I have to figure this out every time I get a new job. Just long enough to forget the solution.
sigh... just forgot the use-site target on the annotations