diff --git a/app/pkg/validator.go b/app/pkg/validator.go index 663f064..71b240e 100644 --- a/app/pkg/validator.go +++ b/app/pkg/validator.go @@ -8,17 +8,21 @@ import ( "github.com/labstack/echo/v4" ) -func ValidateRequest[T any](request T) (map[string]string, error) { - errs := validator.New().Struct(request) - msgs := make(map[string]string) +var validate = validator.New() + +type ValidationErrs map[string][]string + +func ValidateRequest[T any](request T) (ValidationErrs, error) { + errs := validate.Struct(request) + validationErrs := ValidationErrs{} if errs != nil { for _, err := range errs.(validator.ValidationErrors) { field, _ := reflect.TypeOf(request).Elem().FieldByName(err.Field()) queryTag := getStructTag(field, "query") - message := getStructTag(field, "message") - msgs[queryTag] = message + message := err.Tag() + ":" + getStructTag(field, "message") + validationErrs[queryTag] = append(validationErrs[queryTag], message) } - return msgs, errs + return validationErrs, errs } return nil, nil }