when validating a controller using kotlin/spring, how would you validate that a date is after the other? I tried creating a data class and having an
Copy code
@AssertTrue
in the data class, but I'm getting an unused function error (🧵 )
xii
11/25/2021, 5:01 PM
example:
Copy code
@GetMapping("/test")
fun validTest(@PathVariable params: Params){
foo(params.from, <http://params.to|params.to>)
}
data class Params(val from: Instant, val to: Instant){
@AssertTrue("from should be before to")
fun areDatesValid(): Boolean {
return from.isBefore(to)
}
}
xii
11/25/2021, 5:02 PM
(id rather not create a new annotation for this)
j
Jacob
11/25/2021, 7:05 PM
Why are you getting an unused function error on a public function? Does the error go away if you use it in a unit test?
x
xii
11/25/2021, 7:22 PM
yes
t
Tom Hermann
11/30/2021, 10:57 PM
You were super close. The issue is that your @AssertTrue annotation was not on a function that conforms to the JavaBean spec (boolean accessors are expects to start with
is
or
get
)
I’d also mark your parameter object as @Valid, so validation is applied. You probably already have it, but you also need this dependency to get all the validation goodness: