by

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
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 :)
Dean Karn
@deankarn
alway interested in improving it :)
if you have any ideas
Nic Pottier
@nicpottier
would you accept a Validator interface PR?
Dean Karn
@deankarn
probably as long as it doesn't slow it down too much
Nic Pottier
@nicpottier
k, will add it to my todo, can't imagine a type cast check would add any overhead
Walter Schulze
@awalterschulze
Looks like you have done a great job here at creating an easy to use validator that is quite fast, for using reflect.
I quickly threw together a questionable speed comparison with my validation language katydid relapse here:
katydid/katydid#6
You will see that I have spent way too much time optimizing katydid relapse.
It shows a 58x speed up in the worst case and a 1.5x speed up in the best case.
I think you could easily use relapse in the background without having your users notice.
Simply translate the reflected struct tags into a relapse expression and use that in the background.
That is if you are interested in some collaboration?