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
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?
Dean Karn
@deankarn

Hey @awalterschulze thanks, I'll take a more in depth look, but at first glance I agree that the speed comparison is questionable
validator does a lot more than just validate, including returning detailed information on what, if anything, failed https://github.com/go-playground/validator/blob/v9/errors.go#L83 for translation, cross field validation etc...

and when you mention Disclaimer: I did take some shortcuts on BenchmarkStructComplexSuccess and BenchmarkStructComplexFailure by not including the last two fields. the last two are the ones that require runtime reflection.

but I am intrigued and am always looking to improve :)

Walter Schulze
@awalterschulze
Returning the FieldError is a great feature, which relapse does not have. It is especially hard in relapse since it supports Not of sub expressions, where expressions are necessarily just fields. Given the limitations of simply validating fields, we could have custom leave functions, which relapse supports, like myfield->isValidWithError("my error message with fieldname", eq($string, "bla")), which then adds these errors into a list that was injected at relapse compile time.
I think translations will also follow from this.
Relapse does not support cross field validation though, since this will require a memory allocation and then the expression looses speed. There are some hacks to implement cross field validation uses custom functions on the leafs.
Walter Schulze
@awalterschulze
Regarding the runtime reflection. Katydid also does runtime reflect in its reflect parser https://github.com/katydid/katydid/blob/master/parser/reflect/reflect.go . The parser is just very naive at this point, but could easily be improved. I was just too lazy to do it for this test, before more buy in.
So yes this should all be possible, but given the hacks required for error returns and cross field validation, it is less simple that I initially thought.
Dean Karn
@deankarn
I didn't even mention the alias, or validations and dive logic I suspect it would be a lot of work and in the end not convinced it would be that much faster after it's all added in.
but still interested in taking a deeper look
Oh and I assume this would work for individual variables as well and not just structs?
Justin Clift
@justinclift
As a mention for the individual variables front... the current error output from validator is not fantastic. It just returns '' as the value which error-ed.
justinclift @justinclift has been meaning to look into that more, but well... it hasn't happened yet
Dean Karn
@deankarn
@justinclift ya looked into it when creating and there's just no way, that I have found, to get the variables name using reflection...only other thing that could be done is add another parameter for the name of the field, but that's not ideal either and doesn't work with dynamic variables. If you come up with anything though please let me know, definitely love to get better output :)
Walter Schulze
@awalterschulze
alias is already supported in relapse. I don't understand what dive is doing, but relapse supports recursion very well. The reflect parser would need some minor work for individual values, but the relapse logic will be fine with it.
Justin Clift
@justinclift
@joeybloggs No worries. :)
Alkesh Ghorpade
@_alkesh26_twitter

@all Guys u have this below struct

type PropertyMessage struct {
    id            string                   `form:"id" json:"id" binding:"required"`
    data         []map[string]interface{} `form:"data" json:"data" binding:"required" validate:"gt=0"`
}

Seems like gt is not working when my POST request payload is

{
     id: "abc",
     data: []
}
i am expecting the request to fail throwing error data has 0 length, but my request is a success.
Forgot to mention i am using the gin library