Похожие чаты

Anyone have ever use this package ?? github.com/go-playground/validator I want to use

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??

13 ответов

19 просмотров

what exactly are you trying to achieve?

Masoud- Автор вопроса
Roman Sharkov
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

Masoud
simply instead of using "<struct field> is require...

if u.Password == "" { return errors.New("mypass is required") } why do you need the lib?

Masoud- Автор вопроса
Roman Sharkov
if u.Password == "" { return errors.New("mypass ...

😁😁 oh come on you always do codes too simple

Masoud
😁😁 oh come on you always do codes too simple

https://github.com/go-playground/validator/raw/master/_examples/translations/main.go

Masoud- Автор вопроса
Roman Sharkov
https://github.com/go-playground/validator/raw/mas...

It only says how to show custom error messages not custom field label to show in error message

Masoud- Автор вопроса
Roman Sharkov
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

Masoud
emm... I wanted to write clean code and not to che...

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 }

Masoud- Автор вопроса
Roman Sharkov
what's not clean about this code? func Validate(f...

So instead of using that package its better to write a few global functions

Masoud
So instead of using that package its better to wr...

I feel like the lib is for very large scales when writing everything by hand is tedious

Masoud- Автор вопроса

Похожие вопросы

Обсуждают сегодня

Господа, а что сейчас вообще с рынком труда на делфи происходит? Какова ситуация?
Rꙮman Yankꙮvsky
29
А вообще, что может смущать в самой Julia - бы сказал, что нет единого стандартного подхода по многим моментам, поэтому многое выглядит как "хаки" и произвол. Короче говоря, с...
Viktor G.
2
@Benzenoid can you tell me the easiest, and safest way to bu.y HEX now?
Živa Žena
20
This is a question from my wife who make a fortune with memes 😂😂 About the Migration and Tokens: 1. How will the old tokens be migrated to the new $LGCYX network? What is th...
🍿 °anton°
2
30500 за редактор? )
Владимир
47
а через ESC-код ?
Alexey Kulakov
29
What is the Dex situation? Agora team started with the Pnetwork for their dex which helped them both with integration. It’s completed but as you can see from the Pnetwork ann...
Ben
1
Гайс, вопрос для разносторонее развитых: читаю стрим с юарта, нада выделять с него фреймы с определенной структурой, если ли чо готовое, или долбаться с ринг буффером? нада у...
Vitaly
9
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
@lozuk how do I get my phex copies of my ehex from a atomic wallet, to move to my rabby?
Justfrontin 👀
11
Карта сайта