suresh
10/06/2025, 6:04 AMsuresh
10/06/2025, 6:07 AMAuth.Token is a type safe resource.
import io.ktor.server.resources.*
post<Auth.Token> {
install(RequestValidation) {
println("Installing request validation..")
validate {
validation<User> { user ->
println("Validating request body: $user")
ValidationResult.Valid
}
}
}
val user = call.receive<User>()
...
}hallvard
10/06/2025, 6:09 AMvalidate not come after the install block?suresh
10/06/2025, 6:10 AMAleksei Tirman [JB]
10/06/2025, 6:39 AMRequestValidation plugin is installed within the route handler. To solve the problem, install the plugin within a route builder.suresh
10/06/2025, 7:03 AMAleksei Tirman [JB]
10/06/2025, 7:04 AMfun main(args: Array<String>) {
embeddedServer(Netty, port = 9000) {
install(Resources)
install(ContentNegotiation) {
json()
}
routing {
install(RequestValidation) {
println("Installing request validation..")
validate<User> { user ->
println("Validating request body: $user")
ValidationResult.Valid
}
}
post<Auth.Token> {
val user = call.receive<User>()
call.respondText(user.id.toString())
}
}
}.start(wait = true)
}suresh
10/06/2025, 7:06 AMAleksei Tirman [JB]
10/06/2025, 7:11 AMrouting {
resource<Auth.Token> {
install(RequestValidation) {
println("Installing request validation..")
validate<User> { user ->
println("Validating request body: $user")
ValidationResult.Valid
}
}
method(<http://HttpMethod.Post|HttpMethod.Post>) {
handle {
val user = call.receive<User>()
call.respondText(user.id.toString())
}
}
}
}suresh
10/06/2025, 3:20 PMRequestValidation plugin is installed within the route handler. To solve the problem, install the plugin within a route builder.
@Aleksei Tirman [JB]It’s easy to call inside the route handler and there is no warning or error. Is there any way to prevent this from the DSL side to make sure install() is available only on the route builder and not inside the handler, or is there a use case where we can install the plugin inside the handler? I mean it’s very easy to get into this trap.
By the way, thanks a lot for the help.Aleksei Tirman [JB]
10/10/2025, 6:41 AM