Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
muei
@muei
3ks!
Dean Karn
@deankarn
No problem :)
Julien LEICHER
@YuukanOO
Hi there! I may have missed something but when trying to validate a simple struct, it always returns nil.
type MyStruct struct {
    Email string `validate:"required"`
}

// Something like this
validate := validator.New(&validator.Config{
  TagName: "validator",
})

validate.Struct(&MyStruct{}) // returns nil
Dean Karn
@deankarn
It's because you changed the tag name to validator but your struct tag is validate
and so it sees no tags for validation as they aren't matching up
Julien LEICHER
@YuukanOO
omg, I feel stupid...
YuukanOO @YuukanOO out
Julien LEICHER
@YuukanOO
BTW, thanks for this library @joeybloggs =)
Dean Karn
@deankarn
no problems, be sure to check out v9 :smile: it’s even better
Aleksandar Palic
@codepushr
@joeybloggs hey dean :) i have a question and didn't want to open an issue on github... i wonder if it's possible to only have some fields validated if the struct itself is validated directly? i want to have some constraints ignored if the struct is a relation of another struct
example: POST on /devices should trigger the required tag of the field user_id (as it's the foreign key) but on the other hand when POSTING on /users and sending an array of devices i want the user_id constraint to be ignored as i have no user id at that point -> it has to be persisted first
Dean Karn
@deankarn
Yes there are several options for you, here is a quick list(sorry for the formatting answering from my phone) 1. There is a StructExcept(...) function which a field or fields can be ignored. 2. There is a new StructFiltered(...) Function in which you may specify your own function to filter out fields you do or do not want included/validated (I think this may be your best option) 3. you could make 2 validator instances, with different tag names and have different validations for each tag name.
I recommend number 2 if you need an example or help let me know
Dean Karn
@deankarn
I hope I understood your scenario correctly
Aleksandar Palic
@codepushr
Makes absolute sense! Thanks a bunch! I went with option 2 so far and used strings.Index(string(ns), "UserID") to look out for the attribute :)
option 1 does not really work because the collection can be of any size
Dean Karn
@deankarn
Sounds great @codepushr just one suggestion, you may want to look at using bytes.Index(ns, uid) where uid is a var already set to []byte("UserID") that way can avoid the allocation from a byte array to string for ns
Aleksandar Palic
@codepushr
true (Y)
sumeshpoduval
@sumeshpoduval
What will be an ideal tag to validate a struct field contains ONLY values specified like
type Person struct {
Country string validate:"required,alpha,eq=USA|CANADA|SPAIN"
}
Is eq=val1|val2|val2 the correct way or is there a better way to check it contains only one of the values from val1,val2,val3?
Dean Karn
@deankarn
Hey @sumeshpoduval it all depends on your situation with your current example I would say validate:"required,eq=USA|eq=CANADA|eq=SPAIN" would work
alpha is kind of pointless as if it's not alpha it won't match the countries.
Dean Karn
@deankarn
Having said all that for lists like in your example that check equality I recommend creating a custom validation and storing the values in a map or array that can be checked within...that way if your requirements ever change all you have to do is add or remove items from your list, the validation tags don't have to change nor any error messages derived from the your custom validation tag...
sumeshpoduval
@sumeshpoduval
Thanks @joeybloggs .
Dean Karn
@deankarn
creating a custom validation is pretty easy, but if you need a hand just let me know
Justin Clift
@justinclift
Anyone around?
I'm trying to figure out the best way to validate some simple-ish strings.
Effectively, they can be alphanumeric (the easy bit), or also contain a few other characters: . - _
At first thought, contains=[char] seemed to work. But, in further testing that's not what contains seems to be for (eg a bad idea).
The only other approach which seems to allow this is by creating custom validators.
Which seems like massive overkill just for adding a few extra chars to the accept list.
Is there something obvious I'm not seeing?
Dean Karn
@deankarn
@justinclift the best way to do what you want is a custom validation. It's not hard and will take less time to write than reading this whole reply. In this case you'd want to create a regexes, base it off of existing alphanumericunicode one https://github.com/go-playground/validator/blob/v9/regexes.go#L9 and add your few more accepted characters
Then create your validation function

func isName(fl FieldLevel) bool {
return newRegex.MatchString(fl.Field().String())
}
And register the new function on startup with whatever tag you wish like "name"
And then use it on your structs.
Hope that helps :smile:
Justin Clift
@justinclift
Seems like massive overkill just to add "." to the accept list. But yeah, I can't see any other way of doing it with this validator library either.
I'll give it a shot later today and see how it goes. Thanks. :)
Dean Karn
@deankarn
If you need any help just let me know
Justin Clift
@justinclift
Will do. My head is buried in learning groupcache vs memcached stuff atm though. So, later today.
Hoping so. Turned out you're right, that wasn't hard at all. :D
Dean Karn
@deankarn
didn;t you want it to accept unicode characters
?
and the only other this is you can just return match and don’t have to check the return value e.g.
func checkPGTableName(fl validator.FieldLevel) bool {
    return regexPGTable.MatchString(fl.Field().String())
}
Justin Clift
@justinclift
These two string sets don't need unicode. The unicode ones are for later.
Dean Karn
@deankarn
kk