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

chore: a int test fail with validator #36

Closed
wants to merge 1 commit into from
Closed
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
35 changes: 35 additions & 0 deletions context_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,41 @@ func TestRequest(t *testing.T) {
expectCode: http.StatusOK,
expectBodyJson: "{\"name\":\"Goravel1\"}",
},
{
name: "GET with int validator and validate request pass",
method: "GET",
url: "/validator/validate-request/success?id=1",
setup: func(method, url string) error {
fiber.Get("/validator/validate-request/success", func(ctx contractshttp.Context) contractshttp.Response {
mockValidation := &validationmocks.Validation{}
mockValidation.On("Rules").Return([]validation.Rule{}).Once()
ValidationFacade = mockValidation

var createUser CreateUser2
validateErrors, err := ctx.Request().ValidateRequest(&createUser)
if err != nil {
return ctx.Response().String(http.StatusBadRequest, "Validate error: "+err.Error())
}
if validateErrors != nil {
return ctx.Response().String(http.StatusBadRequest, fmt.Sprintf("Validate fail: %+v", validateErrors.All()))
}

return ctx.Response().Success().Json(contractshttp.Json{
"id": createUser.ID,
})
})

var err error
req, err = http.NewRequest(method, url, nil)
if err != nil {
return err
}

return nil
},
expectCode: http.StatusOK,
expectBodyJson: "{\"id\":1}",
},
{
name: "GET with validator but validate request fail",
method: "GET",
Expand Down
28 changes: 28 additions & 0 deletions route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,34 @@ func (r *CreateUser) PrepareForValidation(ctx contractshttp.Context, data valida
return nil
}

type CreateUser2 struct {
ID int `form:"id" json:"id"`
}

func (r *CreateUser2) Authorize(ctx contractshttp.Context) error {
return nil
}

func (r *CreateUser2) Rules(ctx contractshttp.Context) map[string]string {
return map[string]string{
"id": "required|uint",
}
}

func (r *CreateUser2) Messages(ctx contractshttp.Context) map[string]string {
return map[string]string{}
}

func (r *CreateUser2) Attributes(ctx contractshttp.Context) map[string]string {
return map[string]string{}
}

func (r *CreateUser2) PrepareForValidation(ctx contractshttp.Context, data validation.Data) error {
_ = data.Set("id", ctx.Request().QueryInt("id"))

return nil
}

type Unauthorize struct {
Name string `form:"name" json:"name"`
}
Expand Down
Loading