Validator: How can I define custom error message?

Created on 20 Dec 2019  路  2Comments  路  Source: go-playground/validator

How can I define custom error message? I don't want to make a detailed explanation. I just want to make a brief statement.

For example:

type RegisterRequest struct {
    Username    string `validate:"required"`        // UsernameIsRequired
    Email       string `validate:"required,email"`      // EmailIsRequired,EmailIsNotValid
    Password    string `validate:"required,gte=8,lte=32"`   // PasswordRequired,PasswordGreaterThan8,PasswordLessThan32
}
question

Most helpful comment

@bakbuz take a look at the example of the translation for custom error message by locale https://github.com/go-playground/validator/blob/master/_examples/translations/main.go it's geared towards more general messages for each tag.

If you need more custom messages I recommend creating your own messaging layer using all the information returned by each error, see the simple example for details https://github.com/go-playground/validator/blob/master/_examples/simple/main.go#L69

All 2 comments

@bakbuz take a look at the example of the translation for custom error message by locale https://github.com/go-playground/validator/blob/master/_examples/translations/main.go it's geared towards more general messages for each tag.

If you need more custom messages I recommend creating your own messaging layer using all the information returned by each error, see the simple example for details https://github.com/go-playground/validator/blob/master/_examples/simple/main.go#L69

Thank you very much.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

donileo picture donileo  路  3Comments

0505gonzalez picture 0505gonzalez  路  3Comments

phenrigomes picture phenrigomes  路  5Comments

AlexMain picture AlexMain  路  3Comments

muhammadkholidb picture muhammadkholidb  路  3Comments