gin: Unable to use Number validator

Hi,

It’s possible I just don’t understand how to use the validations correctly, but I am trying to bind this form object:

type Form struct {
   Age int `binding:"required,number"`
}

but when testing supplying a string value in the form submission, it errors with an strconv.Atoi error instead of providing a validation error. It looks like it’s failing when converting the data into my Form type, instead of first validating it.

Is there a recommended pattern for handling this? Using a interface{} or string for what should be an int field doesn’t seem ideal.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 22 (2 by maintainers)

Most upvoted comments

Sorry for bumping this old and closed issue, but I’m running into the same problem here. I’m getting a strconv.ParseInt error, which doesn’t tell me anything about which field failed validation (which is bad, because I want to show the users of my API which field was invalid).

From @benmoss:

The only options I see here would be to use an alternate parser like jsoniter or to wrap JSON errors in validation errors, trying to present a more uniform API of error types for the developer to handle.

I think exactly that is a very good idea, and would go a long way towards solving this problem .

@appcypher you can change the type of int field to string then number binding will work as expected. You can convert the string to int afterwards. Here’s an example

type RegisterDTO struct {
	Age  string `json:"limit" form:"limit" binding:"number"`
}

Also running into this issue. This renders using the validator’s translator almost pointless because there is still a need to dissect what was invalid with the request when it was malformed in this specific way. We want to use translations to provide user-friendly messages for all input errors.

I too like the idea of standardising all of the errors coming out of the Bind functions as much as possible to ValidationErrors.

@benmoss Could you please provide the code you’re trying to run? Probably the best rule for Age is min=10,max=0 or you can simply use numeric which means you can use it like this:

type Form struct {
   Age int `binding:"required,numeric,min=18,max=40"`
}

Gin uses http://godoc.org/gopkg.in/go-playground/validator.v8 as its validator. You can find the complete rules and documentation here: https://godoc.org/gopkg.in/go-playground/validator.v8

Please reopen this issue!

@benmoss Can this issue be reopened since this problem hasn’t been resolved? I will have to create a new one otherwise.

BTW, was anyone able to find a workaround for this? I would really hate to send cryptic error messages to my users.

@appleboy benmoss is right.

no, i will try to send a PR for this latter

please reopen, if a field defined as int, and client send a string like “abc”, it will comeout strconv.ParseInt: parsing "abc": invalid syntax, not validation error

@appleboy this solution still gives you a json.UnmarshalTypeError rather than a validation error. What’s failing is json.Unmarshal, and the numeric validation has no effect.

@benmoss Have you tried what I told? numeric is the correct tag but you are using number.