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

Examples should include idiomatic error handling #29

Closed
peteyycz opened this issue Apr 10, 2018 · 2 comments
Closed

Examples should include idiomatic error handling #29

peteyycz opened this issue Apr 10, 2018 · 2 comments

Comments

@peteyycz
Copy link

peteyycz commented Apr 10, 2018

In your examples, the whole point was to show error validation errors, but in real-world applications, it's rarely the use case. As a GO API developer, I'm not sure how to handle empty validation error maps.

Please let me know if you think it's a good example and I could add them to a PR. 😄

	err := v.ValidateJSON()
	if err.Get("_error") != "" {
		log.Print(err)
		http.Error(w, "Error happened processing the request", http.StatusInternalServerError)
		return

	}
	if len(err) != 0 {
		validationErr := map[string]interface{}{"validationError": err}
		json.NewEncoder(w).Encode(validationErr)
		return
	}

@gataky
Copy link

gataky commented Dec 27, 2018

I think it would be nice if when calling Validate*() that it returns an actual error. This would be a little more idiomatic I feel.

If I really want to get the actual url.Values then I can cast to an appropriate custom error type and get them.

Anyway, I want to say I really appreciate this library so much more than other validation libraries out there. So may thanks for this.

@RobvH
Copy link

RobvH commented Jul 11, 2019

There's a bit of a difference between the conventional way go unmarshalls JSON into a struct and PHP json_decodes. I was noticing a lot of my validation rules really don't matter (numeric, for example) b/c if passed a string, Go won't be able to unmarshall type string into an int32 so I always get that error back instead of the validation error saying it needs to be an integer (er, numeric).

You "can" dynamically parse JSON in Go. I'd like to test if that is a viable solution.

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

No branches or pull requests

4 participants