Jakarta / Spring Validation of Contained Values in Kotlin

227 Views Asked by At

I'm running into an issue validating values via a Spring Boot 3 REST API.

More specifically, I need to validate the contained values within a List Param.

Here's my API:

@GetMapping
fun getHelloWorld(
    @Valid queryParams: HelloWorldQueryParams,
): ResponseEntity<HelloWorldResponse> = with(queryParams) { ...

Here is my queryparam class:

@Validated
data class HelloWorldQueryParams(
    @field:Positive val hwId: Long,     // works!
    val hwIds: List<@Positive Long,>?,  // doesn't work :(
)

As you can see above:

  • @Positive annotation doesn't work on the contained values in the hwIds list when querying such as: GET $baseUrl?hwIds=-123,-456 (wrongly passes validation)
  • whereas the vanilla case works: GET $baseUrl?hwId=-123 works (correctly returns 400 Bad request)

What I've tried:

  • @get:Valid val hwIds: List<@Positive Long,>? -- doesn't work :(
  • @get:Valid val hwIds: List<@Valid @Positive Long,>? -- doesn't work :(
  • Tried finding the @ConstraintsApplyTo(CONTAINED_VALUE) annotation as found in this article (from 2017), however this doesn't seem to exist anymore.
  • Tried adding @field: and @get: to the contained value type, but that doesn't compile.
  • @get:PositiveOrZero.List val txIds: List<@PositiveOrZero.List TransactionId>? -- doesn't work

Thanks for any help getting a vanilla solution to this! Would be nice to not have to write my own validator, but any input on why this doesn't work, what's available (or not), or any history for this feature with kotlin and the validation libraries would be very much appreciated!

0

There are 0 best solutions below