• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    해결됨

create 요청시 에러

23.11.16 23:45 작성 조회수 198

0

2023/11/16 23:42:58 http: panic serving [::1]:6279: Undefined validation function 'require' on field 'Name'

goroutine 21 [running]:

net/http.(*conn).serve.func1()

C:/Program Files/Go/src/net/http/server.go:1854 +0xbf

panic({0x8d4c00, 0xc000061b50})

C:/Program Files/Go/src/runtime/panic.go:890 +0x263

github.com/go-playground/validator/v10.(*Validate).parseFieldTagsRecursive(0xc00014e0e0, {0x8bf3a5?, 0x9661a2?}, {0x8bf38b, 0x4}, {0x0, 0x0}, 0x0)

C:/go_gin/pkg/mod/github.com/go-playground/validator/v10@v10.16.0/cache.go:299 +0xa39

github.com/go-playground/validator/v10.(*Validate).extractStructCache(0xc00014e0e0, {0x909600?, 0xc000008108?, 0x1f4e0985898?}, {0x8b7042, 0xd})

C:/go_gin/pkg/mod/github.com/go-playground/validator/v10@v10.16.0/cache.go:156 +0x5ac

github.com/go-playground/validator/v10.(*validate).validateStruct(0xc00043a240, {0xa1ce68, 0xc000026158}, {0x909600?, 0xc000008108?, 0xcb2930?}, {0x909600?, 0xc000008108?, 0x0?}, {0xa220a8, ...}, ...)

C:/go_gin/pkg/mod/github.com/go-playground/validator/v10@v10.16.0/validator.go:37 +0x19d

github.com/go-playground/validator/v10.(*Validate).StructCtx(0xc00014e0e0, {0xa1ce68, 0xc000026158}, {0x909600, 0xc000008108?})

C:/go_gin/pkg/mod/github.com/go-playground/validator/v10@v10.16.0/validator_instance.go:394 +0x479

github.com/go-playground/validator/v10.(*Validate).Struct(...)

C:/go_gin/pkg/mod/github.com/go-playground/validator/v10@v10.16.0/validator_instance.go:367

github.com/gin-gonic/gin/binding.(*defaultValidator).validateStruct(0xcb2930, {0x909600, 0xc000008108})

C:/go_gin/pkg/mod/github.com/gin-gonic/gin@v1.9.1/binding/default_validator.go:80 +0x4e

github.com/gin-gonic/gin/binding.(*defaultValidator).ValidateStruct(0x909600?, {0x909600?, 0xc000008108?})

C:/go_gin/pkg/mod/github.com/gin-gonic/gin@v1.9.1/binding/default_validator.go:59 +0x189

github.com/gin-gonic/gin/binding.(*defaultValidator).ValidateStruct(0xc000436000?, {0x8e4840?, 0xc0000080c0?})

C:/go_gin/pkg/mod/github.com/gin-gonic/gin@v1.9.1/binding/default_validator.go:57 +0x105

github.com/gin-gonic/gin/binding.validate(...)

C:/go_gin/pkg/mod/github.com/gin-gonic/gin@v1.9.1/binding/binding.go:120

github.com/gin-gonic/gin/binding.decodeJSON({0x1f4e0aa8188?, 0xc000038040?}, {0x8e4840, 0xc0000080c0})

C:/go_gin/pkg/mod/github.com/gin-gonic/gin@v1.9.1/binding/json.go:55 +0xa2

github.com/gin-gonic/gin/binding.jsonBinding.Bind({}, 0xa1a460?, {0x8e4840, 0xc0000080c0})

C:/go_gin/pkg/mod/github.com/gin-gonic/gin@v1.9.1/binding/json.go:37 +0x66

github.com/gin-gonic/gin.(*Context).ShouldBindWith(...)

C:/go_gin/pkg/mod/github.com/gin-gonic/gin@v1.9.1/context.go:741

github.com/gin-gonic/gin.(*Context).ShouldBindJSON(...)

C:/go_gin/pkg/mod/github.com/gin-gonic/gin@v1.9.1/context.go:701

github.com/dev-yakuza/study-golang/gin/start/network.(*userRouter).create(0xc000098080, 0xc00051c100)

C:/go_gin/src/CURS_SERVICE/network/user.go:43 +0xbf

github.com/gin-gonic/gin.(*Context).Next(...)

C:/go_gin/pkg/mod/github.com/gin-gonic/gin@v1.9.1/context.go:174

github.com/gin-gonic/gin.(*Engine).handleHTTPRequest(0xc0000844e0, 0xc00051c100)

C:/go_gin/pkg/mod/github.com/gin-gonic/gin@v1.9.1/gin.go:620 +0x66b

github.com/gin-gonic/gin.(*Engine).ServeHTTP(0xc0000844e0, {0xa1c940?, 0xc0001920e0}, 0xc00051c000)

C:/go_gin/pkg/mod/github.com/gin-gonic/gin@v1.9.1/gin.go:576 +0x1dd

net/http.serverHandler.ServeHTTP({0xa1b9d0?}, {0xa1c940, 0xc0001920e0}, 0xc00051c000)

C:/Program Files/Go/src/net/http/server.go:2936 +0x316

net/http.(*conn).serve(0xc000190240, {0xa1ced8, 0xc0000907e0})

C:/Program Files/Go/src/net/http/server.go:1995 +0x612

created by net/http.(*Server).Serve

C:/Program Files/Go/src/net/http/server.go:3089 +0x5ed


무슨에러인지 알 수 있을까요?

답변 2

·

답변을 작성해보세요.

1

JUNI님의 프로필

JUNI

질문자

2023.11.18

func (u *userRouter) create(c *gin.Context) {
	fmt.Println("create 입니다")
	var req types.CreateRequest

	if err := c.ShouldBindJSON(&req); err != nil {
		u.router.failedResponse(c, &types.CreateUserResponse{
			ApiResponse: types.NewApiResponse(-1, "바인딩 오류", err),
		})
	} else if err := u.userService.Create(req.ToUser()); err != nil {
		u.router.failedResponse(c, &types.CreateUserResponse{
			ApiResponse: types.NewApiResponse(-1, "바인딩 오류", err),
		})
	} else {
		u.router.okResponse(c, &types.CreateUserResponse{
			ApiResponse: types.NewApiResponse(1, "성공 입니다.", err),
		})
	}

 

package types

type ApiResponse struct {

Result int64 json:"result"

Description string json:"description"

ErrCode interface{} json:"errCode"

}

func NewApiResponse(Result int64, Description string, errCode interface{}) *ApiResponse {

return &ApiResponse{

Result: Result,

Description: Description,

ErrCode: errCode,

}

}

 

package types

type User struct {

Name string json:"name"

Age int64 json:"age"

}

type CreateUserResponse struct {

*ApiResponse

}

type CreateRequest struct {

Name string json:"name" binding:"require"

Age int64 json:"age" binding:"require"

}

func (c CreateRequest) ToUser() User {

return &User{

Name: c.Name,

Age: c.Age,

}

}

type GetUserResponse struct {

*ApiResponse

Users []*User json:result

}

type UpdateUserResponse struct {

*ApiResponse

}

type UpdateRequest struct {

Name string json:"name" binding:"require"

UpdateAge int64 json:"updateAge" binding:"require"

}

type DeleteUserResponse struct {

*ApiResponse

}

type DeleteRequest struct {

Name string json:"name" binding:"require"

}

func (c DeleteRequest) ToUser() User {

return &User{

Name: c.Name,

}

}

 

이렇게 되는데
if err := c.ShouldBindJSON(&req); err != nil {

 

여기서 문제가 발생했다고 합니다. 이유를 잘 모르겠습니다.

네 코드 올려주셔서 감사합니다.
보시면 오타가 있는것으로 보여요.


CreateRequest 선언하신 타입에 대해서 request를 필수로 받아야 하는 필드를 binding하는 주석을 잘 적어주셨습니다.

하지만 아쉬운것은 require 라는 문구가 조금 아쉬워요
require보다는 required라고 적어주셔야 문제없이 gin프레임워크에서 조건을 검증을 할 수 있습니다.


에러 문구에서도 확인 가능하듯이

Undefined validation function 'require' on field 'Name'

라는 문구는 require라는 벨리데이션 함수가 없다는 의미이기 떄문에 required로 수정을 하셔서 전송을 해보시면 동작할겁니다.

한번 해보시고 추가적인 문제가 있다면 말씀해 주시면 감사하겠습니다. 🙂 좋은하루 되세요!

0

어떤 코드인지 샘플 올려주실 수 있을까요?

 

보시면 require문에서 발생을 하였고 벨리데이트할 때 발생을 한걸로 보이는데

 

어딘가에 오타나 타입선언하실 때 먼가 문제가 있어보입니다.

 

간단한 코드 보내주시면 감사하겠습니다.