Skip to content

Commit a4b6483

Browse files
committed
DVRL-71 - initial draft of Python README
1 parent 508b366 commit a4b6483

File tree

1 file changed

+76
-126
lines changed

1 file changed

+76
-126
lines changed

README.md

Lines changed: 76 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1,165 +1,115 @@
1-
# protovalidate-python
1+
# [![The Buf logo](.github/buf-logo.svg)][buf] protovalidate-python
22

33
[![CI](https://github.com/bufbuild/protovalidate-python/actions/workflows/ci.yaml/badge.svg)](https://github.com/bufbuild/protovalidate-python/actions/workflows/ci.yaml)
44
[![Conformance](https://github.com/bufbuild/protovalidate-python/actions/workflows/conformance.yaml/badge.svg)](https://github.com/bufbuild/protovalidate-python/actions/workflows/conformance.yaml)
55
[![PyPI version](https://badge.fury.io/py/protovalidate.svg)](https://badge.fury.io/py/protovalidate)
66

7-
`protovalidate-python` is the Python implementation of [`protovalidate`](https://github.com/bufbuild/protovalidate),
8-
designed to validate Protobuf messages at runtime based on user-defined validation constraints. Powered by Google's
9-
Common Expression Language ([CEL](https://github.com/google/cel-spec)), it provides a flexible and efficient foundation
10-
for defining and evaluating custom validation rules. The primary goal of `protovalidate` is to help developers ensure
11-
data consistency and integrity across the network without requiring generated code.
7+
[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.
128

13-
## The `protovalidate` project
9+
With Protovalidate, you can annotate your Protobuf messages with both standard and custom validation rules:
1410

15-
Head over to the core [`protovalidate`](https://github.com/bufbuild/protovalidate/) repository for:
11+
```protobuf
12+
syntax = "proto3";
13+
14+
package banking.v1;
15+
16+
import "buf/validate/validate.proto";
1617
17-
- [The API definition](https://github.com/bufbuild/protovalidate/tree/main/proto/protovalidate/buf/validate/validate.proto):
18-
used to describe validation constraints
19-
- [Documentation](https://github.com/bufbuild/protovalidate/tree/main/docs): how to apply `protovalidate` effectively
20-
- [Migration tooling](https://github.com/bufbuild/protovalidate/tree/main/docs/migrate.md): incrementally migrate
21-
from `protoc-gen-validate`
22-
- [Conformance testing utilities](https://github.com/bufbuild/protovalidate/tree/main/docs/conformance.md): for
23-
acceptance testing of `protovalidate` implementations
18+
message MoneyTransfer {
19+
string to_account_id = 1 [
20+
// Standard rule: `to_account_id` must be a UUID
21+
(buf.validate.field).string.uuid = true
22+
];
2423
25-
Other `protovalidate` runtime implementations include:
24+
string from_account_id = 2 [
25+
// Standard rule: `from_account_id` must be a UUID
26+
(buf.validate.field).string.uuid = true
27+
];
2628
27-
- Go: [`protovalidate-go`](https://github.com/bufbuild/protovalidate-go)
28-
- C++: [`protovalidate-cc`](https://github.com/bufbuild/protovalidate-cc)
29-
- Java: [`protovalidate-java`](https://github.com/bufbuild/protovalidate-java)
29+
// Custom rule: `to_account_id` and `from_account_id` can't be the same.
30+
option (buf.validate.message).cel = {
31+
id: "to_account_id.not.from_account_id"
32+
message: "to_account_id and from_account_id should not be the same value"
33+
expression: "this.to_account_id != this.from_account_id"
34+
};
35+
}
36+
```
3037

31-
And others coming soon:
38+
Once you've added `protovalidate-python` to your project, validation is idiomatic Python:
3239

33-
- TypeScript: `protovalidate-ts`
40+
```python
41+
try:
42+
protovalidate.validate(message)
43+
except protovalidate.ValidationError as e:
44+
# Handle failure.
45+
```
3446

3547
## Installation
3648

37-
To install the package, use pip:
49+
> [!TIP]
50+
> The easiest way to get started with Protovalidate for RPC APIs are the how-to's in Buf's documentation. There's one available for [Python and gRPC][grpc-python].
51+
52+
To install the package, use `pip`:
3853

3954
```shell
4055
pip install protovalidate
4156
```
4257

43-
Make sure you have the latest version of `protovalidate-python` by checking the
44-
project's [PyPI page](https://pypi.org/project/protovalidate/).
58+
# Documentation
4559

46-
## Usage
60+
Comprehensive documentation for Protovalidate is available in [Buf's documentation library][protovalidate].
4761

48-
### Implementing validation constraints
62+
Highlights for Python developers include:
4963

50-
Validation constraints are defined directly within `.proto` files. Documentation for adding constraints can be found in
51-
the `protovalidate` project [README](https://github.com/bufbuild/protovalidate) and
52-
its [comprehensive docs](https://github.com/bufbuild/protovalidate/tree/main/docs).
64+
* The [developer quickstart][quickstart]
65+
* A comprehensive RPC how-to for [Python and gRPC][grpc-python]
66+
* A [migration guide for protoc-gen-validate][migration-guide] users
5367

54-
```protobuf
55-
syntax = "proto3";
68+
# Additional Languages and Repositories
5669

57-
package my.package;
58-
59-
import "google/protobuf/timestamp.proto";
60-
import "buf/validate/validate.proto";
70+
Protovalidate isn't just for Python! You might be interested in sibling repositories for other languages:
6171

62-
message Transaction {
63-
uint64 id = 1 [(buf.validate.field).uint64.gt = 999];
64-
google.protobuf.Timestamp purchase_date = 2;
65-
google.protobuf.Timestamp delivery_date = 3;
72+
- [`protovalidate-go`][pv-go] (Go)
73+
- [`protovalidate-java`][pv-java] (Java)
74+
- [`protovalidate-cc`][pv-cc] (C++)
75+
- `protovalidate-ts` (TypeScript, coming soon!)
6676

67-
string price = 4 [(buf.validate.field).cel = {
68-
id: "transaction.price",
69-
message: "price must be positive and include a valid currency symbol ($ or £)",
70-
expression: "(this.startsWith('$') || this.startsWith('£')) && double(this.substring(1)) > 0"
71-
}];
77+
For a peek into how Protovalidate works, you might also want to check out [`protovalidate's core repository`](https://github.com/bufbuild/protovalidate), where `validate.proto` defines the entire cross-language API.
7278

73-
option (buf.validate.message).cel = {
74-
id: "transaction.delivery_date",
75-
message: "delivery date must be after purchase date",
76-
expression: "this.delivery_date > this.purchase_date"
77-
};
78-
}
79-
```
79+
## Related Sites
8080

81-
### Generating Code with `buf`
82-
83-
When using the runtime library after installing it with `pip`, it's necessary to generate the Python code for the core `buf.protovalidate` Protobuf package. `buf` provides an efficient method for this:
84-
85-
1. **Initialize a New Configuration File**:
86-
```shell
87-
buf config init
88-
```
89-
This initializes the `buf.yaml` configuration file at the root of the Protobuf source files.
90-
91-
2. **Module Configuration and Dependencies**:
92-
```yaml
93-
# buf.yaml
94-
version: v2
95-
deps:
96-
- buf.build/bufbuild/protovalidate
97-
```
98-
99-
Ensure your dependencies are up-to-date with:
100-
```shell
101-
buf dep update
102-
```
103-
104-
3. **Setup Code Generation**:
105-
```yaml
106-
# buf.gen.yaml
107-
version: v2
108-
plugins:
109-
- remote: buf.build/protocolbuffers/python
110-
out: gen
111-
```
112-
113-
4. **Generate Code**:
114-
To generate the required Python code:
115-
```shell
116-
buf generate --include-imports
117-
```
118-
119-
5. **Specify import paths**:
120-
Ensure that the generated code is importable by setting the `PYTHONPATH` environment variable:
121-
```shell
122-
export PYTHONPATH=$PYTHONPATH:gen
123-
```
124-
125-
If your goal is to generate code specifically for the `buf.protovalidate` Protobuf package, run:
126-
```shell
127-
buf generate buf.build/bufbuild/protovalidate
128-
```
81+
- [Buf][buf] - Enterprise-grade Kafka and gRPC for the modern age
82+
- [Common Expression Language (CEL)][cel] - The open-source technology at the core of Protovalidate
12983

130-
> **Note:** For users familiar with `protoc`, while it's an alternative to `buf`, it is recommended to use tooling or frameworks like Bazel for direct code generation, as it provides an encapsulated environment for such tasks.
84+
# Contribution
13185

132-
### Example
86+
We genuinely appreciate any help! If you'd like to contribute, the following will be of interest:
13387

134-
```python
135-
import protovalidate
136-
from google.protobuf.timestamp_pb2 import Timestamp
137-
from my.package import Transaction
88+
- [Contributing Guidelines][contributing] - Guidelines to make your contribution process straightforward and meaningful
89+
- [Conformance testing utilities](https://github.com/bufbuild/protovalidate/tree/main/docs/conformance.md) - Utilities providing acceptance testing of `protovalidate` implementations
13890

139-
transaction = Transaction()
140-
transaction.id = 1234
141-
transaction.price = "$5.67"
142-
transaction.purchase_date.CopyFrom(Timestamp())
143-
transaction.delivery_date.CopyFrom(Timestamp())
91+
# Legal
14492

145-
try:
146-
protovalidate.validate(transaction)
147-
except protovalidate.ValidationError as e:
148-
# Report the violations
149-
```
93+
Offered under the [Apache 2 license][license].
15094

151-
### Ecosystem
95+
[buf]: https://buf.build
96+
[cel]: https://cel.dev
15297

153-
- [`protovalidate`](https://github.com/bufbuild/protovalidate) core repository
154-
- [Buf](https://buf.build)
155-
- [CEL Spec](https://github.com/google/cel-spec)
98+
[pv-go]: https://github.com/bufbuild/protovalidate-go
99+
[pv-java]: https://github.com/bufbuild/protovalidate-java
100+
[pv-python]: https://github.com/bufbuild/protovalidate-python
101+
[pv-cc]: https://github.com/bufbuild/protovalidate-cc
156102

157-
## Legal
103+
[buf-mod]: https://buf.build/bufbuild/protovalidate
104+
[license]: LICENSE
105+
[contributing]: .github/CONTRIBUTING.md
158106

159-
Offered under the [Apache 2 license][license].
107+
[protoc-gen-validate]: https://github.com/bufbuild/protoc-gen-validate
160108

161-
[license]: LICENSE
162-
[buf]: https://buf.build
163-
[buf-mod]: https://buf.build/bufbuild/protovalidate
164-
[cel-go]: https://github.com/google/cel-go
165-
[cel-spec]: https://github.com/google/cel-spec
109+
[protovalidate]: https://buf.build/docs/protovalidate/overview/
110+
[quickstart]: https://buf.build/docs/protovalidate/quickstart/
111+
[connect-go]: https://buf.build/docs/protovalidate/how-to/connect-go/
112+
[grpc-go]: https://buf.build/docs/protovalidate/how-to/grpc-go/
113+
[grpc-java]: https://buf.build/docs/protovalidate/how-to/grpc-java/
114+
[grpc-python]: https://buf.build/docs/protovalidate/how-to/grpc-python/
115+
[migration-guide]: https://buf.build/docs/migration-guides/migrate-from-protoc-gen-validate/

0 commit comments

Comments
 (0)