Skip to content
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

feature(middleware/validate): migrate from PGV to protovalidate, since PGV has entered in maintenance #3498

Merged
merged 2 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
build:
strategy:
matrix:
go: [ 1.20.x, 1.21.x, 1.22.x ]
go: [ 1.21.x, 1.22.x, 1.23.x ]
name: build & test
runs-on: ubuntu-latest
services:
Expand Down
2 changes: 1 addition & 1 deletion cmd/kratos/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/go-kratos/kratos/cmd/kratos/v2

go 1.19
go 1.21

require (
github.com/AlecAivazis/survey/v2 v2.3.7
Expand Down
2 changes: 1 addition & 1 deletion cmd/kratos/internal/base/mod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestModulePath(t *testing.T) {

mod := `module github.com/go-kratos/kratos/v2
go 1.19`
go 1.21`
_, err = f.WriteString(mod)
if err != nil {
t.Fatal(err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/protoc-gen-go-errors/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/go-kratos/kratos/cmd/protoc-gen-go-errors/v2

go 1.19
go 1.21

require (
golang.org/x/text v0.3.8
Expand Down
2 changes: 1 addition & 1 deletion cmd/protoc-gen-go-http/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/go-kratos/kratos/cmd/protoc-gen-go-http/v2

go 1.19
go 1.21

require (
google.golang.org/genproto v0.0.0-20220519153652-3a47de7e79bd
Expand Down
2 changes: 1 addition & 1 deletion contrib/config/apollo/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/go-kratos/kratos/contrib/config/apollo/v2

go 1.19
go 1.21

require (
github.com/apolloconfig/agollo/v4 v4.3.1
Expand Down
2 changes: 1 addition & 1 deletion contrib/config/consul/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/go-kratos/kratos/contrib/config/consul/v2

go 1.19
go 1.21

require (
github.com/go-kratos/kratos/v2 v2.8.3
Expand Down
2 changes: 1 addition & 1 deletion contrib/config/etcd/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/go-kratos/kratos/contrib/config/etcd/v2

go 1.19
go 1.21

require (
github.com/go-kratos/kratos/v2 v2.8.3
Expand Down
2 changes: 1 addition & 1 deletion contrib/config/kubernetes/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/go-kratos/kratos/contrib/config/kubernetes/v2

go 1.19
go 1.21

require (
github.com/go-kratos/kratos/v2 v2.8.3
Expand Down
2 changes: 1 addition & 1 deletion contrib/config/nacos/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/go-kratos/kratos/contrib/config/nacos/v2

go 1.19
go 1.21

require (
github.com/go-kratos/kratos/v2 v2.8.3
Expand Down
2 changes: 1 addition & 1 deletion contrib/config/polaris/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/go-kratos/kratos/contrib/config/polaris/v2

go 1.19
go 1.21

require (
github.com/go-kratos/kratos/v2 v2.8.3
Expand Down
2 changes: 1 addition & 1 deletion contrib/encoding/msgpack/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/go-kratos/kratos/contrib/encoding/msgpack/v2

go 1.19
go 1.21

require (
github.com/go-kratos/kratos/v2 v2.8.3
Expand Down
2 changes: 1 addition & 1 deletion contrib/errortracker/sentry/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/go-kratos/kratos/contrib/errortracker/sentry/v2

go 1.19
go 1.21

require (
github.com/getsentry/sentry-go v0.25.0
Expand Down
2 changes: 1 addition & 1 deletion contrib/log/aliyun/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/go-kratos/kratos/contrib/log/aliyun/v2

go 1.19
go 1.21

require (
github.com/aliyun/aliyun-log-go-sdk v0.1.75
Expand Down
2 changes: 1 addition & 1 deletion contrib/log/fluent/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/go-kratos/kratos/contrib/log/fluent/v2

go 1.19
go 1.21

require (
github.com/fluent/fluent-logger-golang v1.9.0
Expand Down
2 changes: 1 addition & 1 deletion contrib/log/logrus/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/go-kratos/kratos/contrib/log/logrus/v2

go 1.19
go 1.21

require (
github.com/go-kratos/kratos/v2 v2.8.3
Expand Down
2 changes: 1 addition & 1 deletion contrib/log/tencent/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/go-kratos/kratos/contrib/log/tencent/v2

go 1.19
go 1.21

require (
github.com/go-kratos/kratos/v2 v2.8.3
Expand Down
2 changes: 1 addition & 1 deletion contrib/log/zap/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/go-kratos/kratos/contrib/log/zap/v2

go 1.19
go 1.21

require (
github.com/go-kratos/kratos/v2 v2.8.3
Expand Down
2 changes: 1 addition & 1 deletion contrib/log/zerolog/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/go-kratos/kratos/contrib/log/zerolog/v2

go 1.19
go 1.21

require (
github.com/go-kratos/kratos/v2 v2.8.3
Expand Down
15 changes: 15 additions & 0 deletions contrib/middleware/validate/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Validator Middleware for Kratos Project

This module provides a middleware for Kratos to validate request parameters, using schema defined in `.proto` files.

There used to be a middleware named `Validator` in Kratos, which calls the generated validation functions
from [PGV](https://github.com/bufbuild/protoc-gen-validate) at runtime. Since PGV has been
in [maintenance](https://github.com/bufbuild/protoc-gen-validate/commit/4a8ffc4942463929c4289407cd4b8c8328ff5422), and
recommend using [protovalidate](https://github.com/bufbuild/protovalidate) as an alternative.

That's why we provide a new middleware that uses the schema definitions and validation functions provided by
protovalidate.

protovalidate no longer requires code generation at build time, but for compatibility with existing Kratos
projects, we enable the legacy mode of protovalidate. For most users, no changes are needed to existing code. **But for
users who have manually implemented the Validator interface, you need to migrate the relevant implementation yourself**.
24 changes: 24 additions & 0 deletions contrib/middleware/validate/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module github.com/go-kratos/kratos/contrib/middleware/validate/v2

go 1.21.1

require (
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.2-20241127180247-a33202765966.1
github.com/bufbuild/protovalidate-go v0.8.2
github.com/envoyproxy/protoc-gen-validate v1.1.0
github.com/go-kratos/kratos/v2 v2.8.3
google.golang.org/protobuf v1.36.2
)

require (
cel.dev/expr v0.18.0 // indirect
github.com/antlr4-go/antlr/v4 v4.13.0 // indirect
github.com/google/cel-go v0.22.1 // indirect
github.com/stoewer/go-strcase v1.3.0 // indirect
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240826202546-f6391c0de4c7 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240826202546-f6391c0de4c7 // indirect
google.golang.org/grpc v1.65.0 // indirect
)
51 changes: 51 additions & 0 deletions contrib/middleware/validate/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.2-20241127180247-a33202765966.1 h1:BICM6du/XzvEgeorNo4xgohK3nMTmEPViGyd5t7xVqk=
buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go v1.36.2-20241127180247-a33202765966.1/go.mod h1:JnMVLi3qrNYPODVpEKG7UjHLl/d2zR221e66YCSmP2Q=
cel.dev/expr v0.18.0 h1:CJ6drgk+Hf96lkLikr4rFf19WrU0BOWEihyZnI2TAzo=
cel.dev/expr v0.18.0/go.mod h1:MrpN08Q+lEBs+bGYdLxxHkZoUSsCp0nSKTs0nTymJgw=
github.com/antlr4-go/antlr/v4 v4.13.0 h1:lxCg3LAv+EUK6t1i0y1V6/SLeUi0eKEKdhQAlS8TVTI=
github.com/antlr4-go/antlr/v4 v4.13.0/go.mod h1:pfChB/xh/Unjila75QW7+VU4TSnWnnk9UTnmpPaOR2g=
github.com/bufbuild/protovalidate-go v0.8.2 h1:sgzXHkHYP6HnAsL2Rd3I1JxkYUyEQUv9awU1PduMxbM=
github.com/bufbuild/protovalidate-go v0.8.2/go.mod h1:K6w8iPNAXBoIivVueSELbUeUl+MmeTQfCDSug85pn3M=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/envoyproxy/protoc-gen-validate v1.1.0 h1:tntQDh69XqOCOZsDz0lVJQez/2L6Uu2PdjCQwWCJ3bM=
github.com/envoyproxy/protoc-gen-validate v1.1.0/go.mod h1:sXRDRVmzEbkM7CVcM06s9shE/m23dg3wzjl0UWqJ2q4=
github.com/go-kratos/kratos/v2 v2.8.3 h1:kkNBq0gvdX+b8cbaN+p6Sdh95DgMhx7GimefXb4o7Ss=
github.com/go-kratos/kratos/v2 v2.8.3/go.mod h1:+Vfe3FzF0d+BfMdajA11jT0rAyJWublRE/seZQNZVxE=
github.com/google/cel-go v0.22.1 h1:AfVXx3chM2qwoSbM7Da8g8hX8OVSkBFwX+rz2+PcK40=
github.com/google/cel-go v0.22.1/go.mod h1:BuznPXXfQDpXKWQ9sPW3TzlAJN5zzFe+i9tIs0yC4s8=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stoewer/go-strcase v1.3.0 h1:g0eASXYtp+yvN9fK8sH94oCIk0fau9uV1/ZdJ0AVEzs=
github.com/stoewer/go-strcase v1.3.0/go.mod h1:fAH5hQ5pehh+j3nZfvwdk2RgEgQjAoM8wodgtPmh1xo=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 h1:aAcj0Da7eBAtrTp03QXWvm88pSyOt+UgdZw2BFZ+lEw=
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8/go.mod h1:CQ1k9gNrJ50XIzaKCRR2hssIjF07kZFEiieALBM/ARQ=
golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
google.golang.org/genproto/googleapis/api v0.0.0-20240826202546-f6391c0de4c7 h1:YcyjlL1PRr2Q17/I0dPk2JmYS5CDXfcdb2Z3YRioEbw=
google.golang.org/genproto/googleapis/api v0.0.0-20240826202546-f6391c0de4c7/go.mod h1:OCdP9MfskevB/rbYvHTsXTtKC+3bHWajPdoKgjcYkfo=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240826202546-f6391c0de4c7 h1:2035KHhUv+EpyB+hWgJnaWKJOdX1E95w2S8Rr4uWKTs=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240826202546-f6391c0de4c7/go.mod h1:UqMtugtsSgubUsoxbuAoiCXvqvErP7Gf0so0mK9tHxU=
google.golang.org/grpc v1.65.0 h1:bs/cUb4lp1G5iImFFd3u5ixQzweKizoZJAwBNLR42lc=
google.golang.org/grpc v1.65.0/go.mod h1:WgYC2ypjlB0EiQi6wdKixMqukr6lBc0Vo+oOgjrM5ZQ=
google.golang.org/protobuf v1.36.2 h1:R8FeyR1/eLmkutZOM5CWghmo5itiG9z0ktFlTVLuTmU=
google.golang.org/protobuf v1.36.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
3 changes: 3 additions & 0 deletions contrib/middleware/validate/internal/testdata/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package testdata

//go:generate protoc -I . -I ../../../../../third_party --go_out=paths=source_relative:. ./test.proto
Loading
Loading