Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
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
Justin Clift
@justinclift
Ahhh, good optimisation. Will do Dean, thanks. :)
Dean Karn
@deankarn
no problem, let me know if you need anything else :smile:
Justin Clift
@justinclift
Used your name as the author for the optimisation commit: sqlitebrowser/dbhub-application@b2fe17e
Yay, now the repo has 2 contributors. :D
Dean Karn
@deankarn
Cool thanks :+1:
Rosa Imantoro
@rimantoro
hi, I’m new to golang and want to explore this package
is there any way to get invalid validation message ?
Dean Karn
@deankarn
Yes you can get the normal validation message from the error with err.Error() to get the string representation...but if you want to build your own you can get all of the error in formation like in the examples https://github.com/go-playground/validator/blob/v9/examples/simple/main.go#L56 (errors are an array of error not a map, have to update the notes in example
Rosa Imantoro
@rimantoro
thx, that actually what I want to do
grab all error, and return in single list og messages
Dean Karn
@deankarn
awesome
Nic Pottier
@nicpottier
hey guys, quick Q, what's the pattern used to validate a struct with unexported fields? ideally i'd love to just define a func Validate() on it and have it do it, but that doesn't seem to be the way validator is structured
(in my case the top level struct has exported structs I want to validate which has unexported fields)
Dean Karn
@deankarn
Registering a struct level validation should allow you to do what your looking to do
Nic Pottier
@nicpottier
oh nice, ya, that'll work
saw a reference to Validatable, is that another approach?