govalidator icon indicating copy to clipboard operation
govalidator copied to clipboard

Validator int doesn't support kind int for value 3;

Open Narven opened this issue 8 years ago • 11 comments

Having this struct

type BasicInfo struct {
        FirstName string `json:"first_name,omitempty" db:"first_name" valid:"alpha,required"`
        LastName  string `json:"last_name,omitempty" db:"last_name" valid:"alpha,required"`
        Gender    int    `json:"gender" db:"gender" valid:"int,required"`
    }

When i pass an int value to Gender i get

Gender: Validator int doesn't support kind int for value 3;"

Is 3 a kind of int made by aliens? Also 2,3,4,5,6,7,8,9,... does not work

How are we supose to validate anything if the Validator does not support anything? int does not work, numeric does not work...

Thanks

Narven avatar Jun 28 '16 23:06 Narven

Go is a strongly typed language, there's no need to verify if the value inside a variable of type int is a int. If you try to validate a variable of type string the verification would make more sense.

waltton avatar Jun 30 '16 23:06 waltton

So whats the point of having func IsInt(str string) bool in the validator? Increase the code size? Go is a strongly typed language when ur validating ur code... To my knowledge govalidator is not to validate ur code, is to validate input data. So if im going to validate that everything is a string... theres no point of having a validator. If i need to validate that I'm inserting an INT into a database INT field... i need to make sure that it is an INT, not a string or an email.

Narven avatar Jul 02 '16 22:07 Narven

Sometimes you need to validate data that recieved from user, e.g. forms from webpages or input in various text fields in your desktop/mobile/web UI. So, this is the main reason to have such validators. Although according to the code, ValidateStruct should accept any fields that can be casted to string, but seems that it doesnt work.

asaskevich avatar Jul 15 '16 17:07 asaskevich

@asaskevich first of all, thank you for this nice library.

Just a short notice: the use of int validation on type int64 did work in the past. I discovered the issue when a test failed just after updating govalidator today. However I'm not sure why the int64 was validated as int :-)

When I scanned my code for other occurrences of int validation, I found one use case where I validate a string field as int. I could work around this and change the type to int instead of string, but having the int validation rule is handier as the the value comes in as string, is just validated and not further processed by the system and leaves the system as string.

marcobeierer avatar Nov 11 '16 22:11 marcobeierer

@marcobeierer I have tried to understand the issue you are describing but was unable to understand it well. So to make it clear to me: is the issue about the validator ie

valid:"int,required

As of time ago some code was changed and I am not sure about code example that will demonstrate this issue. (it would be a bit easier for me to see a full piece of code that I can run to see this issue)

elico avatar Jan 24 '18 11:01 elico

I created a PR with a small solution :')

jsuarezik avatar Jul 02 '18 16:07 jsuarezik

Hi @elico

I'm sorry, I missed your comment in January.

It's hard to remember what exactly the problem was at the time, but I can confirm that it has been fixed in the meantime.

I made a local fork back then and added case reflect.Int: in the typeCheck function to fix the problem. The same change has been done in the master branch of the repo.

https://github.com/asaskevich/govalidator/blob/master/validator.go#L1047

marcobeierer avatar Jul 02 '18 21:07 marcobeierer

@marcobeierer would you check my PR ? I was having the same bug and to the date it is still not fixed.

jsuarezik avatar Jul 02 '18 21:07 jsuarezik

@marcobeierer OK I hope it works for @jsuarezik

elico avatar Jul 03 '18 05:07 elico

I got the same confusion!When I atmept to validate a param which is a int type,I got this xxx: 10 does not validate as type(int).

dartagnanli avatar Dec 08 '20 10:12 dartagnanli

Hello guys! I forked this package cause owner disappeared. Hope, he will be back, but it would be easier to merge these changes back if he is back Link to my repo: create issue there and we'll discuss it.

sergeyglazyrindev avatar Oct 17 '21 21:10 sergeyglazyrindev