From cc7b5dd9e935717ae0d49f9c26d1fd7fefd0831c Mon Sep 17 00:00:00 2001 From: kaitoyama Date: Tue, 24 Oct 2023 14:58:20 +0900 Subject: [PATCH] add validator to PATCH --- router/questions.go | 12 ++++++++++++ router/responses.go | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/router/questions.go b/router/questions.go index fa6211e8..1b446686 100644 --- a/router/questions.go +++ b/router/questions.go @@ -61,6 +61,18 @@ func (q *Question) EditQuestion(c echo.Context) error { return echo.NewHTTPError(http.StatusBadRequest) } + validate, err := getValidator(c) + if err != nil { + c.Logger().Errorf("failed to get validator: %+v", err) + return echo.NewHTTPError(http.StatusInternalServerError, err) + } + + err = validate.Struct(req) + if err != nil { + c.Logger().Infof("validation failed: %+v", err) + return echo.NewHTTPError(http.StatusBadRequest, err) + } + switch req.QuestionType { case "Text": //正規表現のチェック diff --git a/router/responses.go b/router/responses.go index 56d9bb10..81d11d64 100644 --- a/router/responses.go +++ b/router/responses.go @@ -242,6 +242,18 @@ func (r *Response) EditResponse(c echo.Context) error { return echo.NewHTTPError(http.StatusBadRequest) } + validate, err := getValidator(c) + if err != nil { + c.Logger().Errorf("failed to get validator: %+v", err) + return echo.NewHTTPError(http.StatusInternalServerError, err) + } + + err = validate.Struct(req) + if err != nil { + c.Logger().Infof("validation failed: %+v", err) + return echo.NewHTTPError(http.StatusBadRequest, err) + } + limit, err := r.GetQuestionnaireLimit(c.Request().Context(), req.ID) if err != nil { if errors.Is(err, model.ErrRecordNotFound) {