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

feat: support filters #70

Merged
merged 6 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 4 additions & 16 deletions context_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,12 +362,12 @@ func (r *ContextRequest) Url() string {
return r.instance.Request.RequestURI
}

func (r *ContextRequest) Validate(rules map[string]string, options ...contractsvalidate.Option) (contractsvalidate.Validator, error) {
func (r *ContextRequest) Validate(rules, filters map[string]string, options ...contractsvalidate.Option) (contractsvalidate.Validator, error) {
devhaozi marked this conversation as resolved.
Show resolved Hide resolved
if len(rules) == 0 {
return nil, errors.New("rules can't be empty")
}

options = append(options, validation.Rules(rules), validation.CustomRules(r.validation.Rules()))
options = append(options, validation.Rules(rules), validation.CustomRules(r.validation.Rules()), validation.CustomFilters(r.validation.Filters()))

dataFace, err := validate.FromRequest(r.ctx.Request().Origin())
if err != nil {
Expand All @@ -390,28 +390,16 @@ func (r *ContextRequest) Validate(rules map[string]string, options ...contractsv
}
}

return r.validation.Make(dataFace, rules, options...)
return r.validation.Make(dataFace, rules, filters, options...)
}

func (r *ContextRequest) ValidateRequest(request contractshttp.FormRequest) (contractsvalidate.Errors, error) {
if err := request.Authorize(r.ctx); err != nil {
return nil, err
}

filters := make(map[string]string)
val := reflect.Indirect(reflect.ValueOf(request))
for i := 0; i < val.Type().NumField(); i++ {
field := val.Type().Field(i)
form := field.Tag.Get("form")
filter := field.Tag.Get("filter")
if len(form) > 0 && len(filter) > 0 {
filters[form] = filter
}
}

validator, err := r.Validate(request.Rules(r.ctx), validation.Messages(request.Messages(r.ctx)), validation.Attributes(request.Attributes(r.ctx)), func(options map[string]any) {
validator, err := r.Validate(request.Rules(r.ctx), request.Filters(r.ctx), validation.Messages(request.Messages(r.ctx)), validation.Attributes(request.Attributes(r.ctx)), func(options map[string]any) {
options["prepareForValidation"] = request.PrepareForValidation
options["filters"] = filters
})
if err != nil {
return nil, err
Expand Down
27 changes: 19 additions & 8 deletions context_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,9 @@ func (s *ContextRequestSuite) TestValidate_GetSuccess() {
validator, err := ctx.Request().Validate(map[string]string{
"uuid": "min_len:2",
"name": "required",
}, map[string]string{
"uuid": "trim",
"name": "trim",
})
if err != nil {
return ctx.Response().String(400, "Validate error: "+err.Error())
Expand All @@ -1151,7 +1154,7 @@ func (s *ContextRequestSuite) TestValidate_GetSuccess() {
})
})

req, err := http.NewRequest("GET", "/validate/get-success/abc?name=Goravel", nil)
req, err := http.NewRequest("GET", "/validate/get-success/abc?name= Goravel ", nil)
s.Require().Nil(err)

code, body, _, _ := s.request(req)
Expand All @@ -1165,6 +1168,9 @@ func (s *ContextRequestSuite) TestValidate_GetFail() {
validator, err := ctx.Request().Validate(map[string]string{
"uuid": "min_len:4",
"name": "required",
}, map[string]string{
"uuid": "trim",
"name": "trim",
})
if err != nil {
return ctx.Response().String(400, "Validate error: "+err.Error())
Expand All @@ -1176,7 +1182,7 @@ func (s *ContextRequestSuite) TestValidate_GetFail() {
return nil
})

req, err := http.NewRequest("GET", "/validate/get-fail/abc?name=Goravel", nil)
req, err := http.NewRequest("GET", "/validate/get-fail/abc?name= Goravel ", nil)
s.Require().Nil(err)

code, body, _, _ := s.request(req)
Expand All @@ -1192,6 +1198,11 @@ func (s *ContextRequestSuite) TestValidate_PostSuccess() {
"uuid": "required",
"age": "required",
"name": "required",
}, map[string]string{
"id": "trim",
"uuid": "trim",
"age": "trim",
"name": "trim",
})
if err != nil {
return ctx.Response().String(400, "Validate error: "+err.Error())
Expand Down Expand Up @@ -1220,8 +1231,8 @@ func (s *ContextRequestSuite) TestValidate_PostSuccess() {
})

payload := strings.NewReader(`{
"name": "Goravel",
"uuid": "3"
"name": " Goravel ",
"uuid": " 3 "
}`)
req, err := http.NewRequest("POST", "/validate/post-success/1/2?age=2", payload)
s.Require().Nil(err)
Expand All @@ -1237,6 +1248,8 @@ func (s *ContextRequestSuite) TestValidate_PostFail() {
s.route.Post("/validate/post-fail", func(ctx contractshttp.Context) contractshttp.Response {
validator, err := ctx.Request().Validate(map[string]string{
"name1": "required",
}, map[string]string{
"name1": "trim",
})
if err != nil {
return ctx.Response().String(400, "Validate error: "+err.Error())
Expand Down Expand Up @@ -1514,8 +1527,7 @@ func (s *ContextRequestSuite) TestValidateRequest_GetSuccessWithFilter() {

code, body, _, _ := s.request(req)

// TODO Optimize the assertation in https://github.com/goravel/goravel/issues/416
s.Equal("{\"name\":\" Goravel 1\"}", body)
s.Equal("{\"name\":\"Goravel 1\"}", body)
s.Equal(http.StatusOK, code)
}

Expand Down Expand Up @@ -1544,8 +1556,7 @@ func (s *ContextRequestSuite) TestValidateRequest_PostSuccessWithFilter() {
req.Header.Set("Content-Type", "application/json")
code, body, _, _ := s.request(req)

// TODO Optimize the assertation in https://github.com/goravel/goravel/issues/416
s.Equal("{\"name\":\" Goravel 1\"}", body)
s.Equal("{\"name\":\"Goravel 1\"}", body)
s.Equal(http.StatusOK, code)
}

Expand Down
43 changes: 20 additions & 23 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module github.com/goravel/gin

go 1.21
go 1.22
devhaozi marked this conversation as resolved.
Show resolved Hide resolved

require (
github.com/gin-gonic/gin v1.10.0
github.com/gookit/validate v1.5.2
github.com/goravel/framework v1.14.3
github.com/goravel/framework v1.14.1-0.20240710200656-77b6e6f701ab
github.com/rs/cors v1.11.0
github.com/savioxavier/termlink v1.3.0
github.com/spf13/cast v1.6.0
Expand All @@ -18,8 +18,7 @@ require (
atomicgo.dev/keyboard v0.2.9 // indirect
atomicgo.dev/schedule v0.1.0 // indirect
cloud.google.com/go v0.112.1 // indirect
cloud.google.com/go/compute v1.25.1 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/compute/metadata v0.3.0 // indirect
cloud.google.com/go/iam v1.1.6 // indirect
cloud.google.com/go/pubsub v1.36.1 // indirect
filippo.io/edwards25519 v1.1.0 // indirect
Expand All @@ -36,15 +35,15 @@ require (
github.com/bytedance/sonic v1.11.6 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/catppuccin/go v0.2.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/charmbracelet/bubbles v0.18.0 // indirect
github.com/charmbracelet/bubbletea v0.26.3 // indirect
github.com/charmbracelet/huh v0.4.2 // indirect
github.com/charmbracelet/huh/spinner v0.0.0-20240508140610-13957916abf0 // indirect
github.com/charmbracelet/lipgloss v0.11.0 // indirect
github.com/charmbracelet/x/ansi v0.1.1 // indirect
github.com/charmbracelet/x/exp/strings v0.0.0-20240524151031-ff83003bf67a // indirect
github.com/charmbracelet/x/input v0.1.1 // indirect
github.com/charmbracelet/bubbletea v0.26.4 // indirect
github.com/charmbracelet/huh v0.5.1 // indirect
github.com/charmbracelet/huh/spinner v0.0.0-20240709222038-80fcf53648bd // indirect
github.com/charmbracelet/lipgloss v0.11.1 // indirect
github.com/charmbracelet/x/ansi v0.1.3 // indirect
github.com/charmbracelet/x/exp/strings v0.0.0-20240617190524-788ec55faed1 // indirect
github.com/charmbracelet/x/input v0.1.2 // indirect
github.com/charmbracelet/x/term v0.1.1 // indirect
github.com/charmbracelet/x/windows v0.1.2 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
Expand Down Expand Up @@ -89,7 +88,6 @@ require (
github.com/gookit/filter v1.2.1 // indirect
github.com/gookit/goutil v0.6.15 // indirect
github.com/goravel/file-rotatelogs/v2 v2.4.2 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
Expand All @@ -113,14 +111,14 @@ require (
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/microsoft/go-mssqldb v1.6.0 // indirect
github.com/mitchellh/hashstructure/v2 v2.0.2 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
Expand Down Expand Up @@ -161,21 +159,20 @@ require (
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/exp v0.0.0-20240707233637-46b078467d37 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/oauth2 v0.18.0 // indirect
golang.org/x/oauth2 v0.20.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/term v0.21.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/term v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/api v0.171.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto v0.0.0-20240227224415-6ceb2ff114de // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240318140521-94a12d6c2237 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 // indirect
google.golang.org/grpc v1.64.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240528184218-531527333157 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240528184218-531527333157 // indirect
google.golang.org/grpc v1.65.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
Loading
Loading