Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File validation not working? #24

Closed
nmsobri opened this issue Feb 19, 2018 · 6 comments
Closed

File validation not working? #24

nmsobri opened this issue Feb 19, 2018 · 6 comments

Comments

@nmsobri
Copy link

nmsobri commented Feb 19, 2018

Hey adam..

I think the form validation is not working when the rules is optional (no required)

func handler(w http.ResponseWriter, r *http.Request) {
	rules := govalidator.MapData{
                //Notice this rules is optional
		"file:photo": []string{"ext:jpg,png", "size:2000000", "mime:jpg,png"},
	}

	messages := govalidator.MapData{
		"file:photo": []string{"ext:Only jpg/png is allowed", "size:Maximux size is 2Mb"},
	}

	opts := govalidator.Options{
		Request: r,     // request object
		Rules:   rules, // rules map,
		Messages: messages,
	}

	v := govalidator.New(opts)
	e := v.Validate()
	err := map[string]interface{}{"validationError": e}
	w.Header().Set("Content-type", "applciation/json")
	json.NewEncoder(w).Encode(err)
}

Eventhough im not uploading any file, the validation still run through yielding me to Only jpg/png is allowed

Regarding this line:
https://github.com/thedevsaddam/govalidator/blob/master/validator.go#L95

Even though there is no uploaded file, file is still not nil
I think the correct way should be

if strings.HasPrefix(field, "file:") {
	fld := strings.TrimPrefix(field, "file:")
	file, fileHeader, _ := v.Opts.Request.FormFile(fld)
	if fileHeader.Filename  != "" {
		validateFiles(v.Opts.Request, fld, rule, msg, errsBag)
		validateCustomRules(fld, rule, msg, file, errsBag)
	} else {
		validateCustomRules(fld, rule, msg, nil, errsBag)
	}
}

Some additional info:
Golang v1.10
I'm entirely not sure prior to v1.10 whether its working or not

@thedevsaddam
Copy link
Owner

thedevsaddam commented Feb 19, 2018

Go version:

go version go1.10 darwin/amd64

Govalidator version:

v1.8

Code

package main

import (
	"encoding/json"
	"fmt"
	"net/http"

	"github.com/thedevsaddam/govalidator"
)

func handler(w http.ResponseWriter, r *http.Request) {
	rules := govalidator.MapData{
		"file:photo": []string{"ext:jpg,png", "size:10000", "mime:jpg,png"},
	}

	messages := govalidator.MapData{
		"file:photo": []string{"ext:Only jpg/png is allowed", "required:Photo is required"},
	}

	opts := govalidator.Options{
		Request:  r,     // request object
		Rules:    rules, // rules map,
		Messages: messages,
	}
	v := govalidator.New(opts)
	e := v.Validate()
	err := map[string]interface{}{"validationError": e}
	w.Header().Set("Content-type", "applciation/json")
	json.NewEncoder(w).Encode(err)
}

func main() {
	http.HandleFunc("/", handler)
	fmt.Println("Listening on port: 9999")
	http.ListenAndServe(":9999", nil)
}

Response:

Without providing any file
screen shot 2018-02-19 at 5 54 15 pm

Could you please provide me more details. @slier81 It seem's okay

@nmsobri
Copy link
Author

nmsobri commented Feb 21, 2018

@thedevsaddam ill double check, will inform u afterward

@nmsobri
Copy link
Author

nmsobri commented Feb 21, 2018

@thedevsaddam

I can assure you that the bug really exist.
I dont know why you didnt get the error when u run it through postman
Here is sample code you can quickly test

package main

import (
	"fmt"
	"net/http"
	"text/template"
	"github.com/thedevsaddam/govalidator"
)

func handler(w http.ResponseWriter, r *http.Request) {

	if r.Method == "GET" {
		t, _ := template.New("foo").Parse(`
			<html><form action="/" method="post" enctype="multipart/form-data">
				<input type="file" name="photo">
				<input type="submit">
    		        </form></html>
		`)

		t.Execute(w, nil)

	} else {
		rules := govalidator.MapData{
			"file:photo": []string{"ext:jpg,png", "size:10000000"},
		}

		messages := govalidator.MapData{
			"file:photo": []string{"ext:Only jpg/png is allowed", "required:Photo is required"},
		}

		opts := govalidator.Options{
			Request:  r,     // request object
			Rules:    rules, // rules map,
			Messages: messages,
		}
		v := govalidator.New(opts)
		e := v.Validate()
		err := map[string]interface{}{"validationError": e}
		fmt.Fprint(w, err)
	}
}

func main() {
	http.HandleFunc("/", handler)
	fmt.Println("Listening on port: 9999")
	http.ListenAndServe(":9999", nil)
}

When u submit the form without uploading any file, u will get :
map[validationError:map[photo:[Only jpg/png is allowed]]]

Another bug, when u explicitly put required rules and submit the form without uploading any files, the required rules seems not being apply.It will return:
map[validationError:map[photo:[Only jpg/png is allowed]]] like the one without required rules

The validation seem to be working before this..When i pull the latest code from your repos, it break everything. #18 possible cause?

thedevsaddam added a commit that referenced this issue Feb 21, 2018
thedevsaddam added a commit that referenced this issue Feb 21, 2018
@thedevsaddam
Copy link
Owner

@slier81 Hope it'll work now. Please check from master branch and if it's okay then I'll tag this commit as v1.9

@nmsobri
Copy link
Author

nmsobri commented Feb 21, 2018

@thedevsaddam eh i thought u waiting for my PR? nvm ill check it, and confirm with u afterwards

@nmsobri
Copy link
Author

nmsobri commented Feb 21, 2018

@thedevsaddam Yes everything is working as expected.Thank you for your hard work..You can tag this commit as v1.9 and close this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants