govalidator icon indicating copy to clipboard operation
govalidator copied to clipboard

How is `"length(min|max)": ByteLength,` supposed to work?

Open mattes opened this issue 8 years ago • 8 comments

I tried several options:

valid:"length(min)=1"
valid:"length(min):1"
valid:"length(1|255)"
valid:"length(1,255)"

This never returns any error nor does it catch an empty input. Can someone help me out?

mattes avatar Sep 24 '15 21:09 mattes

As was described in struct here: https://github.com/asaskevich/govalidator/blob/master/validator_test.go#L1668 And in test: https://github.com/asaskevich/govalidator/blob/master/validator_test.go#L1829

Can you give a short snippet with sample?

asaskevich avatar Oct 06 '15 19:10 asaskevich

Been working on this with a colleague and having:

type UserDataPatch struct {
    GivenName     string `json:"givenName,omitempty"`
    FullName      string `json:"fullName,omitempty" valid:"length(1|2)"`
    Password      string `json:"-" gorm:"-"`
    ... etc …
}

results in the FullName being considered valid.

We then looked at the tests and added in the following: To TestLengthStruct:

{LengthStruct{""}, false},

and to to TestStringLengthStruct we added:

{StringLengthStruct{""}, false},

Both tests fail. However, adding {"", "1", "2", false}, to TestByteLength passes.

If need be I can make a PR with the updated tests. Not sure how you want to tackle this. I'm happy to fix it up if you have any particular approach in mind.

nathj07 avatar May 05 '16 09:05 nathj07

what is the status of this issue?

raitucarp avatar May 30 '16 09:05 raitucarp

Still open and needs resolution. I haven't had time to look into it yet.

nathj07 avatar May 30 '16 20:05 nathj07

Could you provide a full working example of this issue please?

asaskevich avatar Jul 15 '16 17:07 asaskevich

The length validator is broken.

Use case: change zero to, say, 10 here: https://github.com/asaskevich/govalidator/blob/master/validator_test.go#L2342

Then make food an empty string here: https://github.com/asaskevich/govalidator/blob/master/validator_test.go#L2342

This test will incorrectly pass.

carlisia avatar Sep 15 '16 21:09 carlisia

maybe this will help you

#162

aldidana avatar Feb 04 '17 17:02 aldidana

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