gilcon
(I can't hide my name.)
March 8, 2018, 2:40pm
1
I was wondering if this validation can be simplified more.
func (f *foo) Validate() (err error) {
if err = conform.Strings(f); err != nil {
return
}
_, err = govalidator.ValidateStruct(f)
return
}
I will appreciate any advice/suggestion.
Thanks
calmh
(Jakob Borg)
March 8, 2018, 2:41pm
2
I’d advice against the named return variable, otherwise it looks simple enough as is to me.
2 Likes
lutzhorn
(Lutz Horn)
March 8, 2018, 2:51pm
3
Will you ever return an error
?
gilcon
(I can't hide my name.)
March 8, 2018, 2:52pm
4
Is this you meant?
func (f *foo) Validate() error {
if err := conform.Strings(f); err != nil {
return err
}
_, err := govalidator.ValidateStruct(f)
return err
}
2 Likes
lutzhorn
(Lutz Horn)
March 8, 2018, 2:54pm
6
I’d keep the good case on the first code level:
func (f *foo) Validate() error {
if err := conform.Strings(f); err != nil {
return err
}
if _, err := govalidator.ValidateStruct(f); err != nil {
return err
}
return nil
}
1 Like
FullBoa
(Georgy)
March 13, 2018, 1:26pm
7
I’d stop on this, it seems pretty good. It’s enough short and clear.
1 Like
Ideally you also want to decorate your errors: fmt.Errorf("value does not conform: %v", err)
and fmt.Errorf("validation failed: %v", err)
. Use something like github.com/pkg/errors
to retain the original error.
1 Like