This commit is contained in:
@@ -48,7 +48,7 @@ func (h *AuthController) Signin(c fiber.Ctx) error {
|
||||
if err := validator.ValidateBodyDto(c, dto); err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
|
||||
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 {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
|
||||
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 {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
|
||||
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 {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
|
||||
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 {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
|
||||
Status: false,
|
||||
Message: err.Error(),
|
||||
Errors: err,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -67,7 +67,7 @@ func (m *MediaController) SearchMedia(c fiber.Ctx) error {
|
||||
if err := validator.ValidateQueryDto(c, dto); err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
|
||||
Status: false,
|
||||
Message: err.Error(),
|
||||
Errors: err,
|
||||
})
|
||||
}
|
||||
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 {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
|
||||
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 {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
|
||||
Status: false,
|
||||
Message: err.Error(),
|
||||
Errors: err,
|
||||
})
|
||||
}
|
||||
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 {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
|
||||
Status: false,
|
||||
Message: err.Error(),
|
||||
Errors: err,
|
||||
})
|
||||
}
|
||||
res, err := m.service.PreSignedCompleted(ctx, c.Locals("uid").(string), dto)
|
||||
|
||||
@@ -188,7 +188,7 @@ func (h *UserController) UpdateProfile(c fiber.Ctx) error {
|
||||
if err := validator.ValidateBodyDto(c, dto); err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
|
||||
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 {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
|
||||
Status: false,
|
||||
Message: err.Error(),
|
||||
Errors: err,
|
||||
})
|
||||
}
|
||||
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 {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
|
||||
Status: false,
|
||||
Message: err.Error(),
|
||||
Errors: err,
|
||||
})
|
||||
}
|
||||
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 {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
|
||||
Status: false,
|
||||
Message: err.Error(),
|
||||
Errors: err,
|
||||
})
|
||||
}
|
||||
res, err := h.service.SearchUser(ctx, dto)
|
||||
|
||||
@@ -64,7 +64,7 @@ func (m *VerificationController) SearchVerification(c fiber.Ctx) error {
|
||||
if err := validator.ValidateQueryDto(c, dto); err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
|
||||
Status: false,
|
||||
Message: err.Error(),
|
||||
Errors: err,
|
||||
})
|
||||
}
|
||||
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 {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
|
||||
Status: false,
|
||||
Message: err.Error(),
|
||||
Errors: err,
|
||||
})
|
||||
}
|
||||
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 {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
|
||||
Status: false,
|
||||
Message: err.Error(),
|
||||
Errors: err,
|
||||
})
|
||||
}
|
||||
verificationId := c.Params("id")
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
type CommonResponse struct {
|
||||
Status bool `json:"status"`
|
||||
Data any `json:"data"`
|
||||
Errors any `json:"errors"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
@@ -30,6 +31,7 @@ type PaginatedResponse struct {
|
||||
Status bool `json:"status"`
|
||||
Message string `json:"message"`
|
||||
Data any `json:"data"`
|
||||
Errors any `json:"errors"`
|
||||
Pagination *PaginationMeta `json:"pagination"`
|
||||
}
|
||||
|
||||
|
||||
@@ -32,7 +32,6 @@ func init() {
|
||||
}
|
||||
return isImageURL(val)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func isImageURL(u string) bool {
|
||||
@@ -52,68 +51,69 @@ func isImageURL(u string) bool {
|
||||
}
|
||||
|
||||
type ErrorResponse struct {
|
||||
FailedField string `json:"failed_field"`
|
||||
Tag string `json:"tag"`
|
||||
Value string `json:"value"`
|
||||
FailedField string `json:"failed_field,omitempty"`
|
||||
Tag string `json:"tag,omitempty"`
|
||||
Value string `json:"value,omitempty"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
func formatValidationError(err error) []ErrorResponse {
|
||||
func formatValidationError(err error) []*ErrorResponse {
|
||||
var validationErrors validator.ValidationErrors
|
||||
var errorsList []ErrorResponse
|
||||
var errorsList []*ErrorResponse
|
||||
|
||||
if errors.As(err, &validationErrors) {
|
||||
for _, fieldError := range validationErrors {
|
||||
var element ErrorResponse
|
||||
element.FailedField = fieldError.Field()
|
||||
element.Tag = fieldError.Tag()
|
||||
element.Value = fieldError.Param()
|
||||
message := ""
|
||||
switch fieldError.Tag() {
|
||||
case "required":
|
||||
element.Message = fieldError.Field() + " is required"
|
||||
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"
|
||||
message = fieldError.Field() + " is mandatory"
|
||||
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:
|
||||
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
|
||||
}
|
||||
|
||||
func ValidateQueryDto(c fiber.Ctx, dto any) error {
|
||||
func ValidateQueryDto(c fiber.Ctx, dto any) []*ErrorResponse {
|
||||
if err := c.Bind().Query(dto); err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||
"error": "Failed to parse query parameters: " + err.Error(),
|
||||
})
|
||||
return formatValidationError(err)
|
||||
}
|
||||
|
||||
if err := validate.Struct(dto); err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||
"errors": formatValidationError(err),
|
||||
})
|
||||
return formatValidationError(err)
|
||||
}
|
||||
|
||||
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 {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||
"error": "Invalid request body: " + err.Error(),
|
||||
})
|
||||
return formatValidationError(err)
|
||||
}
|
||||
|
||||
if err := validate.Struct(dto); err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||
"errors": formatValidationError(err),
|
||||
})
|
||||
return formatValidationError(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user