We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
v10
Question
package main import ( "fmt" "github.com/go-playground/validator/v10" ) type TI18nKey string const ( I18nKeyInternalServer TI18nKey = "InternalServer" I18nKeyBadRequest TI18nKey = "BadRequest" I18nKeyNotFound TI18nKey = "NotFound" I18nKeyUserPhoneNumberExists TI18nKey = "UserPhoneNumberExists" I18nKeyPhoneNumberOrPasswordInvalid TI18nKey = "PhoneNumberOrPasswordInvalid" ) func (r TI18nKey) String() string { return string(r) } func (r TI18nKey) IsValid() bool { switch r { case I18nKeyInternalServer, I18nKeyBadRequest, I18nKeyNotFound, I18nKeyUserPhoneNumberExists, I18nKeyPhoneNumberOrPasswordInvalid: return true default: return false } } type TLanguage string const ( LanguageEN TLanguage = "en" LanguageFA TLanguage = "fa" ) func (r TLanguage) String() string { return string(r) } func (r TLanguage) IsValid() bool { switch r { case LanguageEN, LanguageFA: return true default: return false } } type Config struct { Messages map[TLanguage]map[TI18nKey]string `validate:"required,dive,keys,customType"` } func main() { validate := validator.New() validate.RegisterValidation("customType", customTypeValidator) messages := map[TLanguage]map[TI18nKey]string{ LanguageEN: { I18nKeyInternalServer: "en internal server error", "NotRegisteredKey": "in this case expect validator return validation error", }, LanguageFA: { I18nKeyInternalServer: "fa internal server error", "NotRegisteredKey": "in this case expect validator return validation error", }, "NotRegisteredLanguage": {}, // in this case validator is return error : its true } err := validate.Struct(Config{Messages: messages}) if err != nil { fmt.Printf("messages not valid! %s\n", err) } } type iCustomTypeValidatorValidator interface { IsValid() bool } func customTypeValidator(fl validator.FieldLevel) bool { value := fl.Field().Interface().(iCustomTypeValidatorValidator) return value.IsValid() }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Package version eg. v9, v10:
v10
Issue, Question or Enhancement:
Question
Code sample, to showcase or reproduce:
The text was updated successfully, but these errors were encountered: