Hi, does the route specific plugin work inside a t...
# ktor
s
Hi, does the route specific plugin work inside a typesafe route?
I have the following code. Any idea why the validate block is not getting invoked on POST requests? I can see the plugin is getting installed.
Auth.Token
is a type safe resource.
Copy code
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>()
    ...

}
h
Shall the
validate
not come after the
install
block?
s
IIUC, it’s the RequestValidation plugin method and can access only inside the install block - https://ktor.io/docs/server-request-validation.html#validation-function
👍 1
a
The problem is that the
RequestValidation
plugin is installed within the route handler. To solve the problem, install the plugin within a route builder.
s
@Aleksei Tirman [JB] thanks..how does it work with type safe route..do you have an example ?
a
The following code works for me:
Copy code
fun 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)
}
s
that will be applied to all the routes..rt? .. i want route specific .say just for Auth Token and not for others
a
You can solve it the following way:
Copy code
routing {
    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())
            }
        }
    }
}
thank you color 1
s
> The problem is that the
RequestValidation
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.
a
I guess it's possible to prevent this from happening using the DSL markers. Can you please file an issue?
🆗 1