Validate Connect request messages with Buf protovalidate rules.
ProtoRequestValidation runs protovalidate against inbound request messages.
Unary POST and Connect GET validation failures throw ProtoRequestValidationException.
Configure Ktor StatusPages to choose their HTTP response.
syntax = "proto3";
package stricteliza.v1;
import "buf/validate/validate.proto";
message SayRequest {
string sentence = 1 [(buf.validate.field).string.max_len = 100];
}embeddedServer(CIO, port = 8080) {
install(Resources)
install(StatusPages) {
exception<ProtoRequestValidationException> { call, cause ->
call.respondBytes(
bytes = cause.toErrorJsonBytes(),
contentType = ContentType.Application.Json,
status = HttpStatusCode.BadRequest,
)
}
}
routing {
install(ContentNegotiation) {
connectJson()
}
install(ProtoRequestValidation)
strictElizaService(StrictElizaServiceHandler)
}
}.start(wait = true)The example maps invalid unary POST and Connect GET requests to HTTP 400 Connect JSON with
invalid_argument and violation details. The same StatusPages handler also receives validation
failures from REST protobuf routes that share the plugin. If REST routes need another response,
choose that response in the application's StatusPages handler.
Install ProtoRequestValidation only on routes that should validate protobuf requests.
Server-streaming and bidirectional handlers report validation failures in Connect end-stream frames.
Validation rules stay in the .proto file.