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 15cb1a3 commit b895450
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions pkg/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@ import (

var validate = validator.New()

func ValidateRequest[T any](request T) (map[string]string, error) {
type ValidationErrs map[string][]string

func ValidateRequest[T any](request T) (ValidationErrs, error) {
errs := validate.Struct(request)
msgs := make(map[string]string)
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 b895450

Please sign in to comment.