UPDATE: fix bug
All checks were successful
Build and Release / release (push) Successful in 1m8s

This commit is contained in:
2026-04-14 17:33:58 +07:00
parent 7f30dff737
commit 4e1e9892ac
6 changed files with 51 additions and 49 deletions

View File

@@ -48,7 +48,7 @@ func (h *AuthController) Signin(c fiber.Ctx) error {
if err := validator.ValidateBodyDto(c, dto); err != nil { if err := validator.ValidateBodyDto(c, dto); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{ return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
Status: false, Status: false,
Message: err.Error(), Errors: err,
}) })
} }
@@ -102,7 +102,7 @@ func (h *AuthController) Signup(c fiber.Ctx) error {
if err := validator.ValidateBodyDto(c, dto); err != nil { if err := validator.ValidateBodyDto(c, dto); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{ return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
Status: false, Status: false,
Message: err.Error(), Errors: err,
}) })
} }
@@ -221,7 +221,7 @@ func (h *AuthController) VerifyToken(c fiber.Ctx) error {
if err := validator.ValidateBodyDto(c, dto); err != nil { if err := validator.ValidateBodyDto(c, dto); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{ return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
Status: false, Status: false,
Message: err.Error(), Errors: err,
}) })
} }
@@ -258,7 +258,7 @@ func (h *AuthController) CreateToken(c fiber.Ctx) error {
if err := validator.ValidateBodyDto(c, dto); err != nil { if err := validator.ValidateBodyDto(c, dto); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{ return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
Status: false, Status: false,
Message: err.Error(), Errors: err,
}) })
} }
@@ -295,7 +295,7 @@ func (h *AuthController) ForgotPassword(c fiber.Ctx) error {
if err := validator.ValidateBodyDto(c, dto); err != nil { if err := validator.ValidateBodyDto(c, dto); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{ return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
Status: false, Status: false,
Message: err.Error(), Errors: err,
}) })
} }

View File

@@ -67,7 +67,7 @@ func (m *MediaController) SearchMedia(c fiber.Ctx) error {
if err := validator.ValidateQueryDto(c, dto); err != nil { if err := validator.ValidateQueryDto(c, dto); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{ return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
Status: false, Status: false,
Message: err.Error(), Errors: err,
}) })
} }
res, err := m.service.SearchMedia(ctx, dto) res, err := m.service.SearchMedia(ctx, dto)
@@ -158,7 +158,7 @@ func (m *MediaController) BulkDeleteMedia(c fiber.Ctx) error {
if err := validator.ValidateBodyDto(c, dto); err != nil { if err := validator.ValidateBodyDto(c, dto); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{ return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
Status: false, Status: false,
Message: err.Error(), Errors: err,
}) })
} }
@@ -233,7 +233,7 @@ func (m *MediaController) GeneratePresignedURL(c fiber.Ctx) error {
if err := validator.ValidateQueryDto(c, dto); err != nil { if err := validator.ValidateQueryDto(c, dto); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{ return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
Status: false, Status: false,
Message: err.Error(), Errors: err,
}) })
} }
res, err := m.service.GeneratePresignedURL(ctx, c.Locals("uid").(string), dto) res, err := m.service.GeneratePresignedURL(ctx, c.Locals("uid").(string), dto)
@@ -266,7 +266,7 @@ func (m *MediaController) PreSignedCompleted(c fiber.Ctx) error {
if err := validator.ValidateBodyDto(c, dto); err != nil { if err := validator.ValidateBodyDto(c, dto); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{ return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
Status: false, Status: false,
Message: err.Error(), Errors: err,
}) })
} }
res, err := m.service.PreSignedCompleted(ctx, c.Locals("uid").(string), dto) res, err := m.service.PreSignedCompleted(ctx, c.Locals("uid").(string), dto)

View File

@@ -188,7 +188,7 @@ func (h *UserController) UpdateProfile(c fiber.Ctx) error {
if err := validator.ValidateBodyDto(c, dto); err != nil { if err := validator.ValidateBodyDto(c, dto); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{ return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
Status: false, Status: false,
Message: err.Error(), Errors: err,
}) })
} }
@@ -225,7 +225,7 @@ func (h *UserController) ChangePassword(c fiber.Ctx) error {
if err := validator.ValidateBodyDto(c, dto); err != nil { if err := validator.ValidateBodyDto(c, dto); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{ return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
Status: false, Status: false,
Message: err.Error(), Errors: err,
}) })
} }
err := h.service.ChangePassword(ctx, c.Locals("uid").(string), dto) err := h.service.ChangePassword(ctx, c.Locals("uid").(string), dto)
@@ -318,7 +318,7 @@ func (h *UserController) ChangeRoleUser(c fiber.Ctx) error {
if err := validator.ValidateBodyDto(c, dto); err != nil { if err := validator.ValidateBodyDto(c, dto); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{ return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
Status: false, Status: false,
Message: err.Error(), Errors: err,
}) })
} }
claimsVal := c.Locals("user_claims") claimsVal := c.Locals("user_claims")
@@ -398,7 +398,7 @@ func (h *UserController) SearchUser(c fiber.Ctx) error {
if err := validator.ValidateQueryDto(c, dto); err != nil { if err := validator.ValidateQueryDto(c, dto); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{ return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
Status: false, Status: false,
Message: err.Error(), Errors: err,
}) })
} }
res, err := h.service.SearchUser(ctx, dto) res, err := h.service.SearchUser(ctx, dto)

View File

@@ -64,7 +64,7 @@ func (m *VerificationController) SearchVerification(c fiber.Ctx) error {
if err := validator.ValidateQueryDto(c, dto); err != nil { if err := validator.ValidateQueryDto(c, dto); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{ return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
Status: false, Status: false,
Message: err.Error(), Errors: err,
}) })
} }
res, err := m.service.SearchVerification(ctx, dto) res, err := m.service.SearchVerification(ctx, dto)
@@ -138,7 +138,7 @@ func (m *VerificationController) CreateVerification(c fiber.Ctx) error {
if err := validator.ValidateBodyDto(c, dto); err != nil { if err := validator.ValidateBodyDto(c, dto); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{ return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
Status: false, Status: false,
Message: err.Error(), Errors: err,
}) })
} }
res, err := m.service.CreateVerification(ctx, c.Locals("uid").(string), dto) res, err := m.service.CreateVerification(ctx, c.Locals("uid").(string), dto)
@@ -171,7 +171,7 @@ func (m *VerificationController) UpdateVerificationStatus(c fiber.Ctx) error {
if err := validator.ValidateBodyDto(c, dto); err != nil { if err := validator.ValidateBodyDto(c, dto); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{ return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
Status: false, Status: false,
Message: err.Error(), Errors: err,
}) })
} }
verificationId := c.Params("id") verificationId := c.Params("id")

View File

@@ -9,6 +9,7 @@ import (
type CommonResponse struct { type CommonResponse struct {
Status bool `json:"status"` Status bool `json:"status"`
Data any `json:"data"` Data any `json:"data"`
Errors any `json:"errors"`
Message string `json:"message"` Message string `json:"message"`
} }
@@ -30,6 +31,7 @@ type PaginatedResponse struct {
Status bool `json:"status"` Status bool `json:"status"`
Message string `json:"message"` Message string `json:"message"`
Data any `json:"data"` Data any `json:"data"`
Errors any `json:"errors"`
Pagination *PaginationMeta `json:"pagination"` Pagination *PaginationMeta `json:"pagination"`
} }

View File

@@ -32,7 +32,6 @@ func init() {
} }
return isImageURL(val) return isImageURL(val)
}) })
} }
func isImageURL(u string) bool { func isImageURL(u string) bool {
@@ -52,67 +51,68 @@ func isImageURL(u string) bool {
} }
type ErrorResponse struct { type ErrorResponse struct {
FailedField string `json:"failed_field"` FailedField string `json:"failed_field,omitempty"`
Tag string `json:"tag"` Tag string `json:"tag,omitempty"`
Value string `json:"value"` Value string `json:"value,omitempty"`
Message string `json:"message"` Message string `json:"message"`
} }
func formatValidationError(err error) []ErrorResponse { func formatValidationError(err error) []*ErrorResponse {
var validationErrors validator.ValidationErrors var validationErrors validator.ValidationErrors
var errorsList []ErrorResponse var errorsList []*ErrorResponse
if errors.As(err, &validationErrors) { if errors.As(err, &validationErrors) {
for _, fieldError := range validationErrors { for _, fieldError := range validationErrors {
var element ErrorResponse message := ""
element.FailedField = fieldError.Field()
element.Tag = fieldError.Tag()
element.Value = fieldError.Param()
switch fieldError.Tag() { switch fieldError.Tag() {
case "required": case "required":
element.Message = fieldError.Field() + " is required" message = fieldError.Field() + " is mandatory"
case "min":
element.Message = fieldError.Field() + " must be at least " + fieldError.Param() + " characters"
case "max":
element.Message = fieldError.Field() + " must be at most " + fieldError.Param() + " characters"
case "email": case "email":
element.Message = "Invalid email format" message = "The email address is invalid"
case "min":
message = fieldError.Field() + " is too short (min " + fieldError.Param() + ")"
case "max":
message = fieldError.Field() + " is too long (max " + fieldError.Param() + ")"
case "image_url":
message = fieldError.Field() + " must be a link to an image (jpg, png, etc.)"
default: default:
element.Message = fieldError.Error() message = "Field " + fieldError.Field() + " failed on validation: " + fieldError.Tag()
} }
errorsList = append(errorsList, element)
errorsList = append(errorsList, &ErrorResponse{
FailedField: fieldError.Field(),
Tag: fieldError.Tag(),
Value: fieldError.Param(),
Message: message,
})
} }
} else {
errorsList = append(errorsList, &ErrorResponse{
Message: "Invalid request payload: " + err.Error(),
})
} }
return errorsList return errorsList
} }
func ValidateQueryDto(c fiber.Ctx, dto any) error { func ValidateQueryDto(c fiber.Ctx, dto any) []*ErrorResponse {
if err := c.Bind().Query(dto); err != nil { if err := c.Bind().Query(dto); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ return formatValidationError(err)
"error": "Failed to parse query parameters: " + err.Error(),
})
} }
if err := validate.Struct(dto); err != nil { if err := validate.Struct(dto); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ return formatValidationError(err)
"errors": formatValidationError(err),
})
} }
return nil return nil
} }
func ValidateBodyDto(c fiber.Ctx, dto any) error { func ValidateBodyDto(c fiber.Ctx, dto any) []*ErrorResponse {
if err := c.Bind().Body(dto); err != nil { if err := c.Bind().Body(dto); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ return formatValidationError(err)
"error": "Invalid request body: " + err.Error(),
})
} }
if err := validate.Struct(dto); err != nil { if err := validate.Struct(dto); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ return formatValidationError(err)
"errors": formatValidationError(err),
})
} }
return nil return nil