Protobuffers validation disables Scala code generation

As is our custom at Improving, we want to use protoc-gen-validate to write validation annotations to our protobuffers messages. We find this makes the semantic terms of an API much more comprehensible. This is supported for Scala via ScalaPB Validation and per its recommendations we’ve written a configuration function for sbt, like this:

  def protoValidation(proj: Project): Project = {
    proj.settings(
      Compile / PB.targets := Seq(
        scalapb.validate.preprocessor() -> (Compile / sourceManaged).value / "scalapb",
        scalapb.gen() -> (Compile / sourceManaged).value / "scalapb",
        scalapb.validate.gen() -> (Compile / sourceManaged).value / "scalapb"
      ),
      libraryDependencies ++= Seq(
        "com.thesamet.scalapb" %% "scalapb-validate-core" % scalapb.validate.compiler.BuildInfo.version % "protobuf",
      )
    )
  }

However, there seems to be a conflict with the protoc plugins used with Kalix. When we apply the above sbt config function then Kalix does not generate the expected Scala code (Main.scala, for example) and when we don’t apply this function, it does generate that code. This is annoying, of course.

I haven’t found any documentation on kalix.io suggesting how to use protoc-gen-validate with Kalix nor any discussion here. So, I’m asking the Kalix community to be brilliant and show me how to do this. I think it would be highly useful if the Kalix Java/Scala SDK supported validation directly so the above sbt configuration wasn’t even necessary. protoc-gen-validate supports Java without any need for scalapb’s code generator.

Kalix is using Akka gRPC. When you add settings with := you’ll override the PB.targets added by the sbt-akka-grpc plugin. Instead, you’ll need to append the validation generators. There are docs for adding validation to an akka-grpc project. I haven’t tried this out on a Kalix project, but that should demonstrate the approach.

Yes, makes sense to support validation directly in the Kalix SDKs.

Existing issue tracking adding/documenting support with the protobuf SDK:s: Support Envoy Validation In All SDKs via Proxy · Issue #695 · lightbend/kalix-jvm-sdk · GitHub

Thank you, this was very helpful. I’ve converted my config function to what is suggested for akka-grpc integration with proto-gen-validate and it works.

Glad to hear that proto-gen-validate will be supported by the Kalix plugin going forward. It took several days and this topic to get it to work properly and I hope in the future nobody has to repeat that sbt confusion :slight_smile: