go-playground / validator

:100:Go Struct and Field validation, including Cross Field, Cross Struct, Map, Slice and Array diving
MIT License
16.06k stars 1.29k forks source link

Error in getting array type parameters in get method #1255

Open soul-key opened 2 months ago

soul-key commented 2 months ago

Package version eg. v9, v10:

v10

Issue, Question or Enhancement:

The GetFormValues() method in the rest/https/util.go file is unable to obtain array parameters due to r.Form. Get(name) will force the first value of the array parameter to be returned, and can only be a string. This method should be changed to
r. Form["name"]

Code sample, to showcase or reproduce:

① error code

func GetFormValues(r *http.Request) (map[string]any, error) {
    if err := r.ParseForm(); err != nil {
        return nil, err
    }

    if err := r.ParseMultipartForm(maxMemory); err != nil {
        if err != http.ErrNotMultipart {
            return nil, err
        }
    }

    params := make(map[string]any, len(r.Form))
    for name := range r.Form {
        formValue := r.Form.Get(name)
        if len(formValue) > 0 {
            params[name] = formValue
        }
    }

    return params, nil
}

right code

func GetFormValues(r *http.Request) (map[string]any, error) {
    if err := r.ParseForm(); err != nil {
        return nil, err
    }

    if err := r.ParseMultipartForm(maxMemory); err != nil {
        if err != http.ErrNotMultipart {
            return nil, err
        }
    }

    params := make(map[string]any, len(r.Form))
    for name := range r.Form {
        if len( r.Form[name]) > 1 {
            params[name] =  r.Form[name]
        }else{
                formValue := r.Form.Get(name)
                        if len(formValue) >0 {
                              params[name] = formValue
               }
                }
    }

    return params, nil
}