Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
Justin Clift
@justinclift
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?
Dean Karn
@deankarn
without specific casting to the struct type, reflect can't read unexported fields.
Nic Pottier
@nicpottier
right, was just kind of expecting that maybe there was an interface I could implement, which validator would call if it existed
so i could implement Validate() error on my Struct and it could take care of figuring out if it is valid returning any error
sounds like that's a lot like the user level struct validator, just with less casting (though understand that you may not be able to implement said interface)
Dean Karn
@deankarn
That's interesting, the only reason I didn't go the interface route and stuct with struct level validation is that you can use struct level validation for struct you don;t have permissions to add validations for or and interface to.
Nic Pottier
@nicpottier
right, ya, understood.. welp, this will definitely work, but I'll give a +1 to having a Validatable interface if that ever comes up. much thanks!
Dean Karn
@deankarn
I'll definitly think about it
Nic Pottier
@nicpottier
especially since the registering is a bit obnoxious from a circular dependency point of view (i need to make my global validator know about all my types)
or expose my global validator
which maybe is the better approach there :)