Skip to content

Commit

Permalink
validation msgs fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
kevincobain2000 committed Jan 26, 2024
1 parent 7121ddb commit 0da6734
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions app/pkg/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 0da6734

Please sign in to comment.