custom struct field label in error message
for example :
type User struct {
Id int64
Username string `validate:"required,max=30"`
Password string `validate:"required,max=30"`
}
and validation error is like this :
{
"User.Password": "Password field is required"
}
I want to use custom label instead of "Password" in above message
like this
"mypass field is required"
any idea??
what exactly are you trying to achieve?
simply instead of using "<struct field> is required " I want to show my custom error I mean now I can use custom error but just can't change that <struct field> to a custom label or string
if u.Password == "" { return errors.New("mypass is required") } why do you need the lib?
😁😁 oh come on you always do codes too simple
https://github.com/go-playground/validator/raw/master/_examples/translations/main.go
It only says how to show custom error messages not custom field label to show in error message
well.. do you really need it?
emm... I wanted to write clean code and not to check every field is nil or for example is int or length is lower than x and .... I mean instead of these checking only giving the fields to that package would be enough I think
what's not clean about this code? func Validate(f Foo) error { if l := len(f.Bar); l < 5 || l > 5 { return errors.New("invalid bar") } switch f.Baz { case "orange", "apple", "banana": // Okay default: return errors.New("invalid baz") } return nil }
So instead of using that package its better to write a few global functions
I feel like the lib is for very large scales when writing everything by hand is tedious
ok thanks as always Mr.Sharkov🙏🏻
u r welcome
Обсуждают сегодня