mirror of
https://github.com/hazemKrimi/crimson-vault.git
synced 2026-05-01 18:20:27 +00:00
chore: custom validation error messages
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"regexp"
|
||||
|
||||
@@ -12,10 +13,40 @@ type CustomValidator struct {
|
||||
validator *validator.Validate
|
||||
}
|
||||
|
||||
func (validator *CustomValidator) Validate(i any) error {
|
||||
if err := validator.validator.Struct(i); err != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
||||
func (v *CustomValidator) Validate(i any) error {
|
||||
if err := v.validator.Struct(i); err != nil {
|
||||
if validationErrors, ok := err.(validator.ValidationErrors); ok {
|
||||
errors := make(map[string]string)
|
||||
|
||||
for _, ve := range validationErrors {
|
||||
field := ve.Field()
|
||||
tag := ve.Tag()
|
||||
|
||||
var msg string
|
||||
|
||||
switch tag {
|
||||
case "required":
|
||||
msg = fmt.Sprintf("%s is required!", field)
|
||||
case "email":
|
||||
msg = fmt.Sprintf("%s must be a valid email!", field)
|
||||
case "alpha":
|
||||
msg = fmt.Sprintf("%s must only contain alphabetic characters!", field)
|
||||
case "e164":
|
||||
msg = fmt.Sprintf("%s must be a valid phone number in e164 format!", field)
|
||||
case "password":
|
||||
msg = fmt.Sprintf("%s must have at lease one uppercase, one lowercase, one number and one special character!", field)
|
||||
case "eqcsfield":
|
||||
msg = fmt.Sprintf("%s must be the same as %s!", field, ve.Param())
|
||||
default:
|
||||
msg = fmt.Sprintf("%s is not valid!", field)
|
||||
}
|
||||
|
||||
errors[field] = msg
|
||||
}
|
||||
return echo.NewHTTPError(http.StatusBadRequest, errors)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/hazemKrimi/crimson-vault/internal/types"
|
||||
)
|
||||
|
||||
@@ -77,7 +79,7 @@ func (db *DB) UpdateUserSecurityDetails(id int, body types.UpdateUserSecurityDet
|
||||
}
|
||||
|
||||
result = db.instance.Model(user).Updates(types.User{
|
||||
Username: body.Username,
|
||||
Username: strings.ToLower(body.Username),
|
||||
Password: body.Password,
|
||||
})
|
||||
|
||||
|
||||
@@ -21,21 +21,21 @@ type Client struct {
|
||||
}
|
||||
|
||||
type CreateClientRequestBody struct {
|
||||
Name string `json:"name" validate:"required"`
|
||||
Name string `json:"name" validate:"required,alpha"`
|
||||
FiscalCode string `json:"fiscalCode"`
|
||||
Address string `json:"address" validate:"required"`
|
||||
Zip string `json:"zip" validate:"required"`
|
||||
Country string `json:"country" validate:"required"`
|
||||
Country string `json:"country" validate:"required,alpha"`
|
||||
Phone string `json:"phone" validate:"required,e164"`
|
||||
Email string `json:"email" validate:"required,email"`
|
||||
}
|
||||
|
||||
type UpdateClientRequestBody struct {
|
||||
Name string `json:"name"`
|
||||
Name string `json:"name" validate:"omitempty,alpha"`
|
||||
FiscalCode string `json:"fiscalCode"`
|
||||
Address string `json:"address"`
|
||||
Zip string `json:"zip"`
|
||||
Country string `json:"country"`
|
||||
Country string `json:"country" validate:"omitempty,alpha"`
|
||||
Phone string `json:"phone" validate:"omitempty,e164"`
|
||||
Email string `json:"email" validate:"omitempty,email"`
|
||||
}
|
||||
|
||||
@@ -24,21 +24,21 @@ type User struct {
|
||||
}
|
||||
|
||||
type CreateUserRequestBody struct {
|
||||
Name string `json:"name" validate:"required"`
|
||||
Name string `json:"name" validate:"required,alpha"`
|
||||
FiscalCode string `json:"fiscalCode" validate:"required"`
|
||||
Address string `json:"address" validate:"required"`
|
||||
Zip string `json:"zip" validate:"required"`
|
||||
Country string `json:"country" validate:"required"`
|
||||
Country string `json:"country" validate:"required,alpha"`
|
||||
Phone string `json:"phone" validate:"required,e164"`
|
||||
Email string `json:"email" validate:"required,email"`
|
||||
}
|
||||
|
||||
type UpdateUserRequestBody struct {
|
||||
Name string `json:"name"`
|
||||
Name string `json:"name" validate:"omitempty,alpha"`
|
||||
FiscalCode string `json:"fiscalCode"`
|
||||
Address string `json:"address"`
|
||||
Zip string `json:"zip"`
|
||||
Country string `json:"country"`
|
||||
Country string `json:"country" validate:"omitempty,alpha"`
|
||||
Phone string `json:"phone" validate:"omitempty,e164"`
|
||||
Email string `json:"email" validate:"omitempty,email"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user