Похожие чаты

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 ответов

4 просмотра

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

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

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

Anyone here suffers from unexplained aural migraines, who would be up for talking for a bit? Doesn't *have* to be aural, but I am not asking about headaches, I mean actual mi...
Martin Rys
55
We heading to 1.1$ 🤔?
CryptoBo$$
35
A couple thoughts from a random internet stranger: 1. If you want this project to gain traction you cannot keep being negative. If I just came here after finding out about Kd...
Eric Wild
45
Сonst magicTgHTML = (text, entities) => { let processedText = text; let offsetShift = 0; entities.forEach(entity => { const { offset, length, type, url, ...
Андрей
1
кто-то пользуется компонентами rx ? как их лучше ставить, через OPM? (lazarus)
Iluha Companets
15
But what if some projects may have no sense financially but important from public relations point of view and overall adoption (which leads to more investors so more income). ...
Katu
3
Поставил генту, установил кланг, когда запускаю его не давая ему файла он выводит странную ошибку, в чем может быть дело? max@gentoo ~> clang /usr/bin/x86_64-pc-linux-gnu-ld.b...
молодой базз
8
Здравствуйте товарищи! решил всё таки поставить линукс. Порядок создание разделов /boot/efi 200МиБ; /boot 1000; подсистему / 80000; swap 8Гб Будет норм?
K2
19
How Will #Monero Be Attacked? First of all I want to tell you that this is not teaching the network attackers, the central regulators. They already know how to attack the net...
Li₿ΞʁLiøη
3
подскажите пожалуйста, как мне освободить результат записанный в переменную result? в чем проблема подскажите если МОЖЕТЕ?
Михаил Helper
28
Карта сайта