File tree 5 files changed +124
-23
lines changed
5 files changed +124
-23
lines changed Original file line number Diff line number Diff line change @@ -4,13 +4,13 @@ This demo is about to demonstrate how to use protobuf and protovalidate with RES
4
4
5
5
## Requirements
6
6
7
- To generate the go files for the protobuf definitions you need ` buf ` .
7
+ To generate the go files for the protobuf definitions you need ` buf ` installed .
8
8
9
9
## Usage
10
10
11
11
### Protobuf
12
12
13
- To generate the go files for the protobuf definitions run:
13
+ To generate the go files to ` pkg/proto/demo/v1 ` for the protobuf definitions located under ` proto/demo/v1 ` run:
14
14
15
15
``` shell
16
16
buf generate
@@ -29,5 +29,17 @@ go run cmd/server/main.go
29
29
Then request the server with:
30
30
31
31
``` shell
32
- curl localhost:8080/customer
32
+ curl -XPOST --json ' {}' localhost:8080/customer
33
+ ```
34
+
35
+ ## Routes
36
+
37
+ The following CRUD routes are available:
38
+
39
+ ``` shell
40
+ GET localhost:8080/customer
41
+ POST localhost:8080/customer
42
+ GET localhost:8080/customer/{id}
43
+ PUT localhost:8080/customer/{id}
44
+ DELETE localhost:8080/customer/{id}
33
45
```
Original file line number Diff line number Diff line change 1
1
package main
2
2
3
3
import (
4
- "github.com/arkadiusjonczek/go-rest-api-protobuf-protovalidate-demo.git/internal/pkg/app"
5
- v1 "github.com/arkadiusjonczek/go-rest-api-protobuf-protovalidate-demo.git/pkg/proto/demo/v1"
6
- "github.com/bufbuild/protovalidate-go"
7
4
"log"
8
5
"net/http"
6
+
7
+ "github.com/bufbuild/protovalidate-go"
8
+
9
+ "github.com/arkadiusjonczek/go-rest-api-protobuf-protovalidate-demo.git/internal/pkg/app"
10
+ v1 "github.com/arkadiusjonczek/go-rest-api-protobuf-protovalidate-demo.git/pkg/proto/demo/v1"
9
11
)
10
12
11
13
const (
Original file line number Diff line number Diff line change @@ -13,6 +13,8 @@ type Decoder[E Entity] interface {
13
13
14
14
var _ Decoder [v1.Customer ] = (* CustomerDecoder )(nil )
15
15
16
+ // CustomerDecoder is a conrete implementation which doesn't use generics
17
+ // because protojson and protovalidate has problems with generics
16
18
type CustomerDecoder struct {
17
19
unmarshalOptions * protojson.UnmarshalOptions
18
20
protovalidate * protovalidate.Validator
Original file line number Diff line number Diff line change @@ -7,6 +7,18 @@ option go_package = "demo/v1";
7
7
import "buf/validate/validate.proto" ;
8
8
9
9
message Customer {
10
+ option (buf.validate.message ).cel = {
11
+ id : "age_required" ,
12
+ message : "age has to be set" ,
13
+ expression : "has(this.age)"
14
+ };
15
+
16
+ option (buf.validate.message ).cel = {
17
+ id : "age_max" ,
18
+ message : "age has to be gt 0 and lt 120" ,
19
+ expression : "this.age > 0 && this.age < 120"
20
+ };
21
+
10
22
string name = 1 [
11
23
(buf.validate.field ).required = true ,
12
24
(buf.validate.field ).string = {
@@ -15,5 +27,26 @@ message Customer {
15
27
}
16
28
];
17
29
18
- string test_name = 2 ;
30
+ string username = 2 [
31
+ (buf.validate.field ).cel = {
32
+ id : "username_format" ,
33
+ message : "username must be 3 - 16 characters long and only contain letters and digits" ,
34
+ // `this.matches` match the string against a regex pattern, and evaluates to a bool.
35
+ expression : "this.matches('^[A-Za-z0-9]{3,16}$')"
36
+ }
37
+ ];
38
+
39
+ string email = 3 [
40
+ (buf.validate.field ).cel = {
41
+ id : "valid_email" ,
42
+ message : "email must be a valid email" ,
43
+ expression : "this.isEmail()"
44
+ }
45
+ ];
46
+
47
+ uint32 age = 4 ;
48
+
49
+ string agree = 5 [
50
+ (buf.validate.field ) .string.const = "I agree to the terms and conditions."
51
+ ];
19
52
}
You can’t perform that action at this time.
0 commit comments