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

Error validating integer value #9

Closed
hotrush opened this issue Nov 1, 2016 · 3 comments
Closed

Error validating integer value #9

hotrush opened this issue Nov 1, 2016 · 3 comments

Comments

@hotrush
Copy link

hotrush commented Nov 1, 2016

package model

import (
    "github.com/go-ozzo/ozzo-validation"
    "github.com/go-ozzo/ozzo-validation/is"
)

type UserConfirmation struct {
	ID int64 `json:"id" meddler:"id,pk"`
	UserID int64 `json:"user_id" meddler:"user_id" validation:"user_id"`
	Type string `json:"type" meddler:"type"`
	Code string `json:"-" meddler:"code"`
	Time int32 `json:"expire" meddler:"timestamp"`
}

func (uc UserConfirmation) Validate() error {
    return validation.StructRules{}.
        Add("UserID", validation.Required, is.Int).
        Add("Type", validation.Required, validation.In("sms","email")).
        Validate(uc)
}

and some main.go code

confirmation := &model.UserConfirmation{}
_ := c.BindJSON(confirmation)
errs := confirmation.Validate()
if errs != nil {
    errs := errs.(validation.Errors)
    c.JSON(http.StatusBadRequest, gin.H{
        "errors": errs,
    })
    return
}

Sending integer user_id value

curl -XPOST localhost:8080/api/auth/confirmation/send -d '{"user_id":4,"type":"sms"}'
{"errors":{"user_id":"must be either a string or byte slice"}}

It ask me to send a string.... But i need an int value. And sending a string causes struct error:

curl -XPOST localhost:8080/api/auth/confirmation/send -d '{"user_id":"asdsa","type":"sms"}'
{"error":"json: cannot unmarshal string into Go value of type int64"}

Any ideas? I'm new in go)

@qiangxue
Copy link
Member

qiangxue commented Nov 1, 2016

Since you have already declared UserID to be an integer, you should remove the is.Int rule because it doesn't make sense to validate if an integer typed value is an int (that is always true, isn't it?)

FYI, all is.* rules only validate strings or byte arrays.

@hotrush
Copy link
Author

hotrush commented Nov 1, 2016

Understand, thank you!

@hotrush hotrush closed this as completed Nov 1, 2016
@majidalaeinia
Copy link

majidalaeinia commented Mar 9, 2022

@qiangxue
That's right that we expect an integer for an integer when the struct declares so, but when we pass a string we expect the package to inform the user that the parameter should be an integer, not pass the validation and get a JSON unmarshal error!

So, there seems to be no way validating the type of a field would be integer for example by this package. Not?

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

3 participants