Skip to content

Commit

Permalink
feat: support filters
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Jul 12, 2024
1 parent 83d732c commit ad52319
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 158 deletions.
14 changes: 1 addition & 13 deletions context_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,20 +415,8 @@ func (r *ContextRequest) ValidateRequest(request contractshttp.FormRequest) (con
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), validation.Filters(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
81 changes: 46 additions & 35 deletions context_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1128,7 +1128,10 @@ func (s *ContextRequestSuite) TestValidate_GetSuccess() {
validator, err := ctx.Request().Validate(map[string]string{
"uuid": "min_len:2",
"name": "required",
})
}, validation.Filters(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,7 +1168,10 @@ func (s *ContextRequestSuite) TestValidate_GetFail() {
validator, err := ctx.Request().Validate(map[string]string{
"uuid": "min_len:4",
"name": "required",
})
}, validation.Filters(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,7 +1198,12 @@ func (s *ContextRequestSuite) TestValidate_PostSuccess() {
"uuid": "required",
"age": "required",
"name": "required",
})
}, validation.Filters(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,7 +1248,9 @@ 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",
})
}, validation.Filters(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 Expand Up @@ -1591,70 +1602,70 @@ func (s *ContextRequestSuite) request(req *http.Request) (int, string, http.Head
func TestGetValueFromHttpBody(t *testing.T) {
tests := []struct {
name string
postData map[string]any
httpBody map[string]any
key string
expectValue any
}{
{
name: "Return nil when postData is nil",
name: "Return nil when httpBody is nil",
},
{
name: "Return string when postData is map[string]string",
postData: map[string]any{"name": "goravel"},
name: "Return string when httpBody is map[string]string",
httpBody: map[string]any{"name": "goravel"},
key: "name",
expectValue: "goravel",
},
{
name: "Return map when postData is map[string]map[string]string",
postData: map[string]any{"name": map[string]string{"sub": "goravel"}},
name: "Return map when httpBody is map[string]map[string]string",
httpBody: map[string]any{"name": map[string]string{"sub": "goravel"}},
key: "name",
expectValue: map[string]string{"sub": "goravel"},
},
{
name: "Return slice when postData is map[string][]string",
postData: map[string]any{"name[]": []string{"a", "b"}},
name: "Return slice when httpBody is map[string][]string",
httpBody: map[string]any{"name[]": []string{"a", "b"}},
key: "name[]",
expectValue: []string{"a", "b"},
},
{
name: "Return slice when postData is map[string][]string, but key doesn't contain []",
postData: map[string]any{"name": []string{"a", "b"}},
name: "Return slice when httpBody is map[string][]string, but key doesn't contain []",
httpBody: map[string]any{"name": []string{"a", "b"}},
key: "name",
expectValue: []string{"a", "b"},
},
{
name: "Return string when postData is map[string]map[string]string and key with point",
postData: map[string]any{"name": map[string]string{"sub": "goravel"}},
name: "Return string when httpBody is map[string]map[string]string and key with point",
httpBody: map[string]any{"name": map[string]string{"sub": "goravel"}},
key: "name.sub",
expectValue: "goravel",
},
{
name: "Return int when postData is map[string]map[string]int and key with point",
postData: map[string]any{"name": map[string]int{"sub": 1}},
name: "Return int when httpBody is map[string]map[string]int and key with point",
httpBody: map[string]any{"name": map[string]int{"sub": 1}},
key: "name.sub",
expectValue: 1,
},
{
name: "Return string when postData is map[string][]string and key with point",
postData: map[string]any{"name[]": []string{"a", "b"}},
name: "Return string when httpBody is map[string][]string and key with point",
httpBody: map[string]any{"name[]": []string{"a", "b"}},
key: "name[].0",
expectValue: "a",
},
{
name: "Return string when postData is map[string][]string and key with point and index is 1",
postData: map[string]any{"name[]": []string{"a", "b"}},
name: "Return string when httpBody is map[string][]string and key with point and index is 1",
httpBody: map[string]any{"name[]": []string{"a", "b"}},
key: "name[].1",
expectValue: "b",
},
{
name: "Return string when postData is map[string][]string and key with point, but key doesn't contain []",
postData: map[string]any{"name[]": []string{"a", "b"}},
name: "Return string when httpBody is map[string][]string and key with point, but key doesn't contain []",
httpBody: map[string]any{"name[]": []string{"a", "b"}},
key: "name.0",
expectValue: "a",
},
{
name: "Return string when postData is map[string][]string and key with point and index is 1, but key doesn't contain []",
postData: map[string]any{"name[]": []string{"a", "b"}},
name: "Return string when httpBody is map[string][]string and key with point and index is 1, but key doesn't contain []",
httpBody: map[string]any{"name[]": []string{"a", "b"}},
key: "name.1",
expectValue: "b",
},
Expand All @@ -1663,7 +1674,7 @@ func TestGetValueFromHttpBody(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
contextRequest := &ContextRequest{
httpBody: test.postData,
httpBody: test.httpBody,
}

value := contextRequest.getValueFromHttpBody(test.key)
Expand Down
55 changes: 26 additions & 29 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/gofiber/fiber/v2 v2.52.5
github.com/gofiber/template/html/v2 v2.1.2
github.com/gookit/validate v1.5.2
github.com/goravel/framework v1.14.3
github.com/goravel/framework v1.14.1-0.20240712021404-0ba88953a817
github.com/savioxavier/termlink v1.3.0
github.com/spf13/cast v1.6.0
github.com/stretchr/testify v1.9.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 @@ -35,15 +34,15 @@ require (
github.com/aws/aws-sdk-go v1.49.6 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.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-20240711191530-dbf5a5ac0510 // indirect
github.com/charmbracelet/lipgloss v0.12.0 // 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/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
Expand Down Expand Up @@ -83,13 +82,12 @@ 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
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.5.4 // indirect
github.com/jackc/pgx/v5 v5.5.5 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
Expand All @@ -105,18 +103,18 @@ 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/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
github.com/pterm/pterm v0.12.79 // indirect
github.com/rabbitmq/amqp091-go v1.9.0 // indirect
github.com/redis/go-redis/v9 v9.5.2 // indirect
github.com/redis/go-redis/v9 v9.5.3 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
Expand Down Expand Up @@ -150,30 +148,29 @@ require (
go.opentelemetry.io/otel/trace v1.24.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.9.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.26.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/protobuf v1.33.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
gopkg.in/yaml.v3 v3.0.1 // indirect
gorm.io/driver/mysql v1.5.6 // indirect
gorm.io/driver/postgres v1.5.7 // indirect
gorm.io/driver/mysql v1.5.7 // indirect
gorm.io/driver/postgres v1.5.9 // indirect
gorm.io/driver/sqlserver v1.5.3 // indirect
gorm.io/gorm v1.25.10 // indirect
gorm.io/plugin/dbresolver v1.5.1 // indirect
gorm.io/gorm v1.25.11 // indirect
gorm.io/plugin/dbresolver v1.5.2 // indirect
modernc.org/libc v1.37.6 // indirect
modernc.org/mathutil v1.6.0 // indirect
modernc.org/memory v1.7.2 // indirect
Expand Down
Loading

0 comments on commit ad52319

Please sign in to comment.