Function to validate struct and return errors in a map format how to use: err := ValidateStruct(&App{}) if err != nil { fmt.Println(err) }
(s interface{})
| 17 | // fmt.Println(err) |
| 18 | // } |
| 19 | func ValidateStruct(s interface{}) map[string]string { |
| 20 | err := Validate.Struct(s) |
| 21 | if err != nil { |
| 22 | errors := make(map[string]string) |
| 23 | for _, e := range err.(validator.ValidationErrors) { |
| 24 | errors[e.Field()] = fmt.Sprintf("Invalid value for %s", e.Field()) |
| 25 | } |
| 26 | return errors |
| 27 | } |
| 28 | return nil |
| 29 | } |
| 30 | |
| 31 | // BindAndValidate binds the request body to the struct and validates it |
| 32 | // how to use: |
nothing calls this directly
no outgoing calls
no test coverage detected