fix: fixing response body validation
This commit is contained in:
parent
8644e0f510
commit
96f85fce0b
|
@ -1,6 +1,11 @@
|
|||
package dto
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
)
|
||||
|
||||
type ArticleRequest struct {
|
||||
Title string `json:"title" validate:"required"`
|
||||
|
@ -41,6 +46,52 @@ type ArticleUpdateRequest struct {
|
|||
Content string `json:"content" validate:"required"`
|
||||
}
|
||||
|
||||
func (ar *ArticleRequest) Validate() error {
|
||||
return validate.Struct(ar)
|
||||
func (c *ArticleRequest) ValidatePostArticle() error {
|
||||
err := validate.Struct(c)
|
||||
if err != nil {
|
||||
|
||||
for _, e := range err.(validator.ValidationErrors) {
|
||||
|
||||
switch e.Field() {
|
||||
case "Title":
|
||||
return fmt.Errorf("judul harus diisi")
|
||||
case "CoverImage":
|
||||
return fmt.Errorf("gambar cover harus diisi")
|
||||
case "Author":
|
||||
return fmt.Errorf("penulis harus diisi")
|
||||
case "Heading":
|
||||
return fmt.Errorf("heading harus diisi")
|
||||
case "Content":
|
||||
return fmt.Errorf("konten artikel harus diisi")
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *ArticleUpdateRequest) ValidateUpdateArticle() error {
|
||||
err := validate.Struct(c)
|
||||
if err != nil {
|
||||
|
||||
for _, e := range err.(validator.ValidationErrors) {
|
||||
|
||||
switch e.Field() {
|
||||
case "Title":
|
||||
return fmt.Errorf("judul harus diisi")
|
||||
case "CoverImage":
|
||||
return fmt.Errorf("gambar cover harus diisi")
|
||||
case "Author":
|
||||
return fmt.Errorf("penulis harus diisi")
|
||||
case "Heading":
|
||||
return fmt.Errorf("heading harus diisi")
|
||||
case "Content":
|
||||
return fmt.Errorf("konten artikel harus diisi")
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// func (ar *ArticleRequest) Validate() error {
|
||||
// return validate.Struct(ar)
|
||||
// }
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package controllers
|
||||
|
||||
import (
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/pahmiudahgede/senggoldong/dto"
|
||||
"github.com/pahmiudahgede/senggoldong/internal/services"
|
||||
|
@ -19,6 +18,15 @@ func CreateArticle(c *fiber.Ctx) error {
|
|||
))
|
||||
}
|
||||
|
||||
if err := articleRequest.ValidatePostArticle(); err != nil {
|
||||
|
||||
return c.Status(fiber.StatusBadRequest).JSON(utils.FormatResponse(
|
||||
fiber.StatusBadRequest,
|
||||
err.Error(),
|
||||
nil,
|
||||
))
|
||||
}
|
||||
|
||||
articleResponse, err := services.CreateArticle(&articleRequest)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(utils.FormatResponse(
|
||||
|
@ -139,13 +147,12 @@ func UpdateArticle(c *fiber.Ctx) error {
|
|||
))
|
||||
}
|
||||
|
||||
validate := validator.New()
|
||||
err := validate.Struct(articleUpdateRequest)
|
||||
if err != nil {
|
||||
if err := articleUpdateRequest.ValidateUpdateArticle(); err != nil {
|
||||
|
||||
return c.Status(fiber.StatusBadRequest).JSON(utils.FormatResponse(
|
||||
fiber.StatusBadRequest,
|
||||
"Validation error",
|
||||
err.Error(),
|
||||
nil,
|
||||
))
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue