Reputation: 4101
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:
GET
$baseUrl?hwIds=-123,-456
(wrongly passes validation)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 :(@ConstraintsApplyTo(CONTAINED_VALUE)
annotation as found in this article (from 2017), however this doesn't seem to exist anymore.@field:
and @get:
to the contained value type, but that doesn't compile.@get:PositiveOrZero.List val txIds: List<@PositiveOrZero.List TransactionId>?
-- doesn't workThanks 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!
Upvotes: 3
Views: 420