-
Notifications
You must be signed in to change notification settings - Fork 15
Prepare GitHub docs for shift to buf.build/docs #238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jrinehart-buf
merged 14 commits into
main
from
jrinehart/dvrl-60-update-protovalidate-java-repository
Apr 3, 2025
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
938ad92
DVRL-60 - draft of Java README
jrinehart-buf 652a08f
DVRL-60 - spelling fix
jrinehart-buf fd9e881
DVRL-60 - header fix
jrinehart-buf d6dfe71
DVRL-60 - revisions before PR
jrinehart-buf c0f0320
DVRL-60 - removing duplicative link
jrinehart-buf 24812bf
Merge branch 'main' into jrinehart/dvrl-60-update-protovalidate-java-…
jrinehart-buf f66c5f7
DVRL-60 - updating bug report
jrinehart-buf 0ccc7df
Merge remote-tracking branch 'origin/jrinehart/dvrl-60-update-protova…
jrinehart-buf f61084e
DVRL-60 - consistent logo treatment
jrinehart-buf 37d06c4
Apply suggestions from code review
jrinehart-buf 88359fb
Merge branch 'main' into jrinehart/dvrl-60-update-protovalidate-java-…
jrinehart-buf 3326d95
DVRL-60 - correcting protovalidate-es name
jrinehart-buf b596958
DVRL-60 - removing stale reference to examples
jrinehart-buf b0d93f3
DVRL-60 - updating links to docs
jrinehart-buf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,138 +1,131 @@ | ||
# [][buf] protovalidate-java | ||
[][buf] | ||
|
||
# protovalidate-java | ||
|
||
[](https://github.com/bufbuild/protovalidate-java/actions/workflows/ci.yaml) | ||
[](https://github.com/bufbuild/protovalidate-java/actions/workflows/conformance.yaml) | ||
[][buf-mod] | ||
|
||
`protovalidate-java` is the Java language implementation of [`protovalidate`](https://github.com/bufbuild/protovalidate) designed to validate Protobuf messages at runtime based on user-defined validation constraints. Powered by Google's Common Expression Language ([CEL](https://github.com/google/cel-spec)), it provides a flexible and efficient foundation for defining and evaluating custom validation rules. The primary goal of `protovalidate` is to help developers ensure data consistency and integrity across the network without requiring generated code. | ||
[Protovalidate][protovalidate] provides standard annotations to validate common constraints on messages and fields, as well as the ability to use [CEL][cel] to write custom constraints. It's the next generation of [protoc-gen-validate][protoc-gen-validate], the only widely used validation library for Protobuf. | ||
|
||
## The `protovalidate` project | ||
With Protovalidate, you can annotate your Protobuf messages with both standard and custom validation rules: | ||
|
||
Head over to the core [`protovalidate`](https://github.com/bufbuild/protovalidate/) repository for: | ||
```protobuf | ||
syntax = "proto3"; | ||
|
||
- [The API definition](https://github.com/bufbuild/protovalidate/tree/main/proto/protovalidate/buf/validate/validate.proto): used to describe validation constraints | ||
- [Documentation](https://github.com/bufbuild/protovalidate/tree/main/docs): how to apply `protovalidate` effectively | ||
- [Migration tooling](https://github.com/bufbuild/protovalidate/tree/main/docs/migrate.md): incrementally migrate from `protoc-gen-validate` | ||
- [Conformance testing utilities](https://github.com/bufbuild/protovalidate/tree/main/docs/conformance.md): for acceptance testing of `protovalidate` implementations | ||
package banking.v1; | ||
|
||
import "buf/validate/validate.proto"; | ||
|
||
Other `protovalidate` runtime implementations include: | ||
message MoneyTransfer { | ||
string to_account_id = 1 [ | ||
// Standard rule: `to_account_id` must be a UUID | ||
(buf.validate.field).string.uuid = true | ||
]; | ||
|
||
- C++: [`protovalidate-cc`](https://github.com/bufbuild/protovalidate-cc) | ||
- Go: [`protovalidate-go`](https://github.com/bufbuild/protovalidate-go) | ||
- Python: [`protovalidate-python`](https://github.com/bufbuild/protovalidate-python) | ||
string from_account_id = 2 [ | ||
// Standard rule: `from_account_id` must be a UUID | ||
(buf.validate.field).string.uuid = true | ||
]; | ||
|
||
And others coming soon: | ||
// Custom rule: `to_account_id` and `from_account_id` can't be the same. | ||
option (buf.validate.message).cel = { | ||
id: "to_account_id.not.from_account_id" | ||
message: "to_account_id and from_account_id should not be the same value" | ||
expression: "this.to_account_id != this.from_account_id" | ||
}; | ||
} | ||
``` | ||
|
||
- TypeScript: `protovalidate-ts` | ||
Once you've added `protovalidate-java` to your project, validation is idiomatic Java: | ||
|
||
```java | ||
ValidationResult result = validator.validate(message); | ||
if (!result.isSuccess()) { | ||
// Handle failure. | ||
} | ||
``` | ||
|
||
## Installation | ||
|
||
To include `protovalidate-java` in your project, add the following to your build file: | ||
> [!TIP] | ||
> The easiest way to get started with Protovalidate for RPC APIs are the quickstarts in Buf's documentation. There's one available for [Java and gRPC][grpc-java]. | ||
|
||
`protovalidate-java` is listed in [Maven Central][maven], which provides installation snippets for Gradle, Maven, and other package managers. In Gradle, it's: | ||
|
||
```gradle | ||
dependencies { | ||
implementation 'build.buf:protovalidate:<version>' | ||
} | ||
``` | ||
|
||
Remember to always check for the latest version of `protovalidate-java` on the project's [GitHub releases page](https://github.com/bufbuild/protovalidate-java/releases) to ensure you're using the most up-to-date version. | ||
## Documentation | ||
|
||
## Usage | ||
Comprehensive documentation for Protovalidate is available in [Buf's documentation library][protovalidate]. | ||
|
||
### Implementing validation constraints | ||
Highlights for Java developers include: | ||
|
||
Validation constraints are defined directly within `.proto` files. Documentation for adding constraints can be found in the `protovalidate` project [README](https://github.com/bufbuild/protovalidate) and its [comprehensive docs](https://github.com/bufbuild/protovalidate/tree/main/docs). | ||
* The [developer quickstart][quickstart] | ||
* A comprehensive RPC quickstart for [Java and gRPC][grpc-java] | ||
* A [migration guide for protoc-gen-validate][migration-guide] users | ||
|
||
```protobuf | ||
syntax = "proto3"; | ||
## Additional Languages and Repositories | ||
|
||
package my.package; | ||
Protovalidate isn't just for Java! You might be interested in sibling repositories for other languages: | ||
|
||
import "google/protobuf/timestamp.proto"; | ||
import "buf/validate/validate.proto"; | ||
- [`protovalidate-go`][pv-go] (Go) | ||
- [`protovalidate-python`][pv-python] (Python) | ||
- [`protovalidate-cc`][pv-cc] (C++) | ||
- `protovalidate-es` (TypeScript and JavaScript, coming soon!) | ||
|
||
message Transaction { | ||
uint64 id = 1 [(buf.validate.field).uint64.gt = 999]; | ||
google.protobuf.Timestamp purchase_date = 2; | ||
google.protobuf.Timestamp delivery_date = 3; | ||
|
||
string price = 4 [(buf.validate.field).cel = { | ||
id: "transaction.price", | ||
message: "price must be positive and include a valid currency symbol ($ or £)", | ||
expression: "(this.startsWith('$') || this.startsWith('£')) && double(this.substring(1)) > 0" | ||
}]; | ||
|
||
option (buf.validate.message).cel = { | ||
id: "transaction.delivery_date", | ||
message: "delivery date must be after purchase date", | ||
expression: "this.delivery_date > this.purchase_date" | ||
}; | ||
} | ||
``` | ||
Additionally, [protovalidate's core repository](https://github.com/bufbuild/protovalidate) provides: | ||
|
||
### Example | ||
- [Protovalidate's Protobuf API][validate-proto] | ||
- [Conformance testing utilities][conformance] for acceptance testing of `protovalidate` implementations | ||
|
||
In your Java code, create an instance of the `Validator` class and use the `validate` method to validate your messages. | ||
|
||
```java | ||
// Import the required packages | ||
package build.buf; | ||
|
||
import build.buf.protovalidate.results.ValidationException; | ||
import build.buf.protovalidate.results.ValidationResult; | ||
import com.my.package.Transaction; | ||
import com.google.protobuf.Timestamp; | ||
|
||
import build.buf.protovalidate.Validator; | ||
import build.buf.protovalidate.Config; | ||
|
||
public class Main { | ||
|
||
// Create timestamps for purchase and delivery date | ||
Timestamp purchaseDate = Timestamp.newBuilder().build(); | ||
Timestamp deliveryDate = Timestamp.newBuilder().build(); | ||
|
||
// Create a transaction object using the Builder pattern | ||
Transaction transaction = | ||
Transaction.newBuilder() | ||
.setId(1234) | ||
.setPrice("$5.67") | ||
.setPurchaseDate(purchaseDate) | ||
.setDeliveryDate(deliveryDate) | ||
.build(); | ||
|
||
// Create a validator object with the default Configuration | ||
Validator validator = new Validator(); | ||
// Validate the transaction object using the validator | ||
try { | ||
ValidationResult result = validator.validate(transaction); | ||
|
||
// Check if there are any validation violations | ||
if (result.getViolations().isEmpty()) { | ||
// No violations, validation successful | ||
System.out.println("Validation succeeded"); | ||
} else { | ||
// Print the violations if any found | ||
System.out.println(result.toString()); | ||
} | ||
} catch (ValidationException e) { | ||
// Catch and print any ValidationExceptions thrown during the validation process | ||
System.out.println("Validation failed: " + e.getMessage()); | ||
} | ||
} | ||
``` | ||
## Contribution | ||
|
||
We genuinely appreciate any help! If you'd like to contribute, check out these resources: | ||
|
||
- [Contributing Guidelines][contributing]: Guidelines to make your contribution process straightforward and meaningful | ||
- [Conformance testing utilities](https://github.com/bufbuild/protovalidate/tree/main/docs/conformance.md): Utilities providing acceptance testing of `protovalidate` implementations | ||
|
||
### Ecosystem | ||
## Related Sites | ||
|
||
- [`protovalidate`](https://github.com/bufbuild/protovalidate) core repository | ||
- [Buf][buf] | ||
- [CEL Spec][cel-spec] | ||
- [Buf][buf]: Enterprise-grade Kafka and gRPC for the modern age | ||
- [Common Expression Language (CEL)][cel]: The open-source technology at the core of Protovalidate | ||
|
||
## Legal | ||
|
||
Offered under the [Apache 2 license][license]. | ||
|
||
[license]: LICENSE | ||
[buf]: https://buf.build | ||
[cel]: https://cel.dev | ||
|
||
[pv-go]: https://github.com/bufbuild/protovalidate-go | ||
[pv-java]: https://github.com/bufbuild/protovalidate-java | ||
[pv-python]: https://github.com/bufbuild/protovalidate-python | ||
[pv-cc]: https://github.com/bufbuild/protovalidate-cc | ||
|
||
[license]: LICENSE | ||
[contributing]: .github/CONTRIBUTING.md | ||
[buf-mod]: https://buf.build/bufbuild/protovalidate | ||
[cel-spec]: https://github.com/google/cel-spec | ||
|
||
[protoc-gen-validate]: https://github.com/bufbuild/protoc-gen-validate | ||
|
||
[protovalidate]: https://buf.build/docs/protovalidate | ||
[quickstart]: https://buf.build/docs/protovalidate/quickstart/ | ||
[connect-go]: https://buf.build/docs/protovalidate/quickstart/connect-go/ | ||
[grpc-go]: https://buf.build/docs/protovalidate/quickstart/grpc-go/ | ||
[grpc-java]: https://buf.build/docs/protovalidate/quickstart/grpc-java/ | ||
[grpc-python]: https://buf.build/docs/protovalidate/quickstart/grpc-python/ | ||
[migration-guide]: https://buf.build/docs/migration-guides/migrate-from-protoc-gen-validate/ | ||
|
||
[maven]: https://central.sonatype.com/artifact/build.buf/protovalidate/overview | ||
[pkg-go]: https://pkg.go.dev/github.com/bufbuild/protovalidate-go | ||
|
||
[validate-proto]: https://buf.build/bufbuild/protovalidate/docs/main:buf.validate | ||
[conformance]: https://github.com/bufbuild/protovalidate/blob/main/docs/conformance.md | ||
[examples]: https://github.com/bufbuild/protovalidate/tree/main/examples | ||
[migrate]: https://buf.build/docs/migration-guides/migrate-from-protoc-gen-validate/ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At some point we should add some more badges to this README including the latest version number and potentially a link to published Javadocs.