inflearn logo
강의

Course

Instructor

Developing backend and setting up environment using Golang

Handling server memory data, binding requests

create 요청시 에러

Resolved

411

JUNI

22 asked

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


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

aws go crud postman-mocks

Answer 2

1

JUNI

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 {

 

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

0

July

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


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

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


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

Undefined validation function 'require' on field 'Name'

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

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

0

July

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

 

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

 

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

 

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

iam 권한

0

10

1

수강기간 연장 가능하실까요?

0

15

2

강의 듣는 중인데,

0

22

1

36강 오탈자가 있는 거 같습니다.

0

17

2

node.js 설치 관련 질문 드립니다

0

18

1

Cursor 설정

0

24

1

맥북으로 RDP 연결 질문

0

28

2

라우팅 테이블 설정 중 궁금한게 있습니다.

0

34

1

수강 연장 가능하면 요청 드립니다.

0

27

2

JPA Repository 질문이 있습니다!

1

33

2

페이지네이션 처리를 쿼리에서 하는 방식 질문

1

36

1

(강의 6분 50초 부분) Workspace 안에 index.html이 비어있음

0

32

2

프로젝트 구조 관련 질문이 있습니다.

0

47

1

UserResponse struct 질문

1

267

1

키보드 어떤거 사용하시는지요?

1

305

1

repository 트랜잭션 관련 질문

2

296

1

hello world 가 2번 뜨는 이유가 뭘까요?

1

421

2

UserRouter 질문

1

260

1

소스 코드가 있는 github 주소 공유 부탁드려도 될까요?

1

314

1

강의 코드 따라친거 public repository에 올려도 되나요?

0

453

1

service, repository 단계 구성하기 - 1

0

360

1

delete 에러 관련되서 질문이 있습니다.

0

358

1

서버 구조에 대한 질문

1

466

1

1분24초에 go run .를 실행할때 filepath에러

0

497

2