@@ -175,21 +175,25 @@ func FormValues(r *http.Request, params interface{}, validationRules []*v.FieldR
175175 structValue := ref .Elem ()
176176 fields := map [string ]bool {}
177177 for i := 0 ; i < structType .NumField (); i ++ {
178- name := structType .Field (i ).Name
179- underscoredName := util .Underscore (name )
180- value := strings .TrimSpace (r .FormValue (underscoredName ))
178+ fieldName := structType .Field (i ).Name
179+ formattedName := util .Underscore (fieldName )
180+ jsonName , ok := structType .Field (i ).Tag .Lookup ("json" )
181+ if ok {
182+ formattedName = jsonName
183+ }
184+ value := strings .TrimSpace (r .FormValue (formattedName ))
181185
182186 // if param is not set at all, continue
183187 // comes after call to r.FormValue so form values get parsed internally (if they arent already)
184- if len (r .Form [underscoredName ]) == 0 {
188+ if len (r .Form [formattedName ]) == 0 {
185189 continue
186190 }
187191
188- fields [underscoredName ] = true
192+ fields [formattedName ] = true
189193 isPtr := false
190194 var finalValue reflect.Value
191195
192- structField := structValue .FieldByName (name )
196+ structField := structValue .FieldByName (fieldName )
193197 structFieldKind := structField .Kind ()
194198 if structFieldKind == reflect .Ptr {
195199 isPtr = true
@@ -205,7 +209,7 @@ func FormValues(r *http.Request, params interface{}, validationRules []*v.FieldR
205209 }
206210 castVal , err := cast .ToInt64E (value )
207211 if err != nil {
208- return errors .Err ("%s: must be an integer" , underscoredName )
212+ return errors .Err ("%s: must be an integer" , formattedName )
209213 }
210214 switch structFieldKind {
211215 case reflect .Int :
@@ -225,7 +229,7 @@ func FormValues(r *http.Request, params interface{}, validationRules []*v.FieldR
225229 }
226230 castVal , err := cast .ToUint64E (value )
227231 if err != nil {
228- return errors .Err ("%s: must be an unsigned integer" , underscoredName )
232+ return errors .Err ("%s: must be an unsigned integer" , formattedName )
229233 }
230234 switch structFieldKind {
231235 case reflect .Uint :
@@ -245,7 +249,7 @@ func FormValues(r *http.Request, params interface{}, validationRules []*v.FieldR
245249 }
246250 if ! validator .IsBoolString (value ) {
247251 return errors .Err ("%s: must be one of the following values: %s" ,
248- underscoredName , strings .Join (validator .GetBoolStringValues (), ", " ))
252+ formattedName , strings .Join (validator .GetBoolStringValues (), ", " ))
249253 }
250254 finalValue = reflect .ValueOf (validator .IsTruthy (value ))
251255
@@ -255,7 +259,7 @@ func FormValues(r *http.Request, params interface{}, validationRules []*v.FieldR
255259 }
256260 castVal , err := cast .ToFloat64E (value )
257261 if err != nil {
258- return errors .Err ("%s: must be a floating point number" , underscoredName )
262+ return errors .Err ("%s: must be a floating point number" , formattedName )
259263 }
260264 switch structFieldKind {
261265 case reflect .Float32 :
@@ -264,7 +268,7 @@ func FormValues(r *http.Request, params interface{}, validationRules []*v.FieldR
264268 finalValue = reflect .ValueOf (float64 (castVal ))
265269 }
266270 default :
267- return errors .Err ("field %s is an unsupported type" , name )
271+ return errors .Err ("field %s is an unsupported type" , fieldName )
268272 }
269273
270274 if isPtr {
0 commit comments