48 lines
1.5 KiB
Go
48 lines
1.5 KiB
Go
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/go-playground/validator/v10"
|
|
)
|
|
|
|
var validate *validator.Validate
|
|
|
|
func init() {
|
|
validate = validator.New()
|
|
}
|
|
|
|
// ValidationErrorResponse represents the structure of validation errors returned to the client
|
|
type ValidationErrorResponse struct {
|
|
Errors map[string]string `json:"errors"`
|
|
}
|
|
|
|
// ValidateStruct validates a struct based on its tags using go-playground/validator
|
|
func ValidateStruct(s interface{}) *ValidationErrorResponse {
|
|
err := validate.Struct(s)
|
|
if err != nil {
|
|
var errorsMap = make(map[string]string)
|
|
for _, err := range err.(validator.ValidationErrors) {
|
|
// Simpler error messages for now. Can be enhanced with universal-translator.
|
|
fieldName := strings.ToLower(err.Field())
|
|
switch err.Tag() {
|
|
case "required":
|
|
errorsMap[fieldName] = fmt.Sprintf("Field '%s' is required", fieldName)
|
|
case "email":
|
|
errorsMap[fieldName] = fmt.Sprintf("Field '%s' must be a valid email", fieldName)
|
|
case "min":
|
|
errorsMap[fieldName] = fmt.Sprintf("Field '%s' must be at least %s characters long", fieldName, err.Param())
|
|
case "max":
|
|
errorsMap[fieldName] = fmt.Sprintf("Field '%s' must be at most %s characters long", fieldName, err.Param())
|
|
case "alphanum":
|
|
errorsMap[fieldName] = fmt.Sprintf("Field '%s' must contain only alphanumeric characters", fieldName)
|
|
default:
|
|
errorsMap[fieldName] = fmt.Sprintf("Field '%s' failed validation on '%s' tag", fieldName, err.Tag())
|
|
}
|
|
}
|
|
return &ValidationErrorResponse{Errors: errorsMap}
|
|
}
|
|
return nil
|
|
}
|