Kotlin code generation for protovalidate (buf.validate) and protoc-gen-validate (PGV) constraints.
Generates Kotlin validation functions from protobuf constraint annotations at compile time via a protoc plugin. Includes a lightweight runtime library for common validation logic.
| Module | Description |
|---|---|
runtime |
Validation helper functions used by generated code |
protoc-plugin-core |
Shared code generator logic (field emitters, CEL transpiler) |
protoc-plugin |
protoc plugin for PGV (validate/validate.proto) constraints |
protoc-plugin-buf |
protoc plugin for buf validate (buf/validate/validate.proto) constraints |
gradle-plugin |
Gradle plugin that wires everything together for consumers |
conformance |
Buf protovalidate conformance test executor |
pgv-conformance |
PGV conformance test executor |
The simplest way to use protovalidate-kt. Requires the protobuf-gradle-plugin.
plugins {
id("com.google.protobuf") version "0.9.6"
id("dev.bmcreations.protovalidate") version "<version>"
}This automatically:
- Registers the protoc plugin for code generation
- Adds the
runtimedependency - Configures
generateProtoTasksto invoke the plugin
By default the buf validate variant is used. To use PGV instead:
import dev.bmcreations.protovalidate.gradle.ProtoVariant
protovalidate {
variant.set(ProtoVariant.PGV)
}buf validate is the actively-maintained successor to PGV. Protos use buf.validate annotations:
import "buf/validate/validate.proto";
message User {
string email = 1 [(buf.validate.field).string.email = true];
uint32 age = 2 [(buf.validate.field).uint32 = {gte: 0, lte: 150}];
}protoc-gen-validate uses validate annotations:
import "validate/validate.proto";
message User {
string email = 1 [(validate.rules).string.email = true];
uint32 age = 2 [(validate.rules).uint32 = {gte: 0, lte: 150}];
}PGV protos import validate/validate.proto. If protoc can't find it, add the proto include path. The Gradle plugin handles this automatically when using ProtoVariant.PGV.
If you prefer to configure things yourself:
dependencies {
implementation("dev.bmcreations.protovalidate:runtime:<version>")
}
protobuf {
plugins {
// The protobuf-gradle-plugin auto-generates a wrapper script for JAR artifacts
create("validate-kt-buf") {
artifact = "dev.bmcreations.protovalidate:protoc-plugin-buf:<version>@jar"
}
}
generateProtoTasks {
all().forEach {
it.plugins { create("validate-kt-buf") }
}
}
}For PGV, replace validate-kt-buf / protoc-plugin-buf with validate-kt / protoc-plugin.
./gradlew :conformance:jar
conformance/run-conformance.sh./gradlew :pgv-conformance:jar
cd pgv-conformance && ./run-conformance.shRequires JDK 21+.
./gradlew buildMIT