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

Required on "map" are not working #62

Open
tiagoacardoso opened this issue Mar 11, 2019 · 0 comments
Open

Required on "map" are not working #62

tiagoacardoso opened this issue Mar 11, 2019 · 0 comments
Labels
enhancement Need PR Need pull request from community

Comments

@tiagoacardoso
Copy link
Contributor

tiagoacardoso commented Mar 11, 2019

Hi there!

Today I just found out that for example map[string]string
Just had a look on the source code but my knowledge is not enough to solve the problem.
Even adding a CustomRule it doesn't work, seems to be an internal problem in the source code.

Code:

func validate(c *gin.Context) {
	type Example struct {
		Foo string            `json:"foo"`
		Bar map[string]string `json:"bar"`
	}

	rules :=
		govalidator.MapData{
			"foo": []string{"required"},
			"bar": []string{"required"},
		}

	var ex Example
	opts := govalidator.Options{
		Request: c.Request,
		Rules:   rules,
		Data:    &ex,
	}
	v := govalidator.New(opts)
	e := v.ValidateJSON()
	if len(e) > 0 {
		validationErrors := map[string]interface{}{"validation_errors": e}
		c.JSON(http.StatusBadRequest, validationErrors)
		return
	}
}

Input:

{
    "foo": "asd",
    "bar": {
    	"1": "one"
    }
}

Output:

{
    "validation_errors": {
        "bar": [
            "The bar field is required"
        ]
    }
}

Best regards :)

@thedevsaddam thedevsaddam added enhancement Need PR Need pull request from community labels Mar 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Need PR Need pull request from community
Projects
None yet
Development

No branches or pull requests

2 participants