feat: base58 support

pull/971/head
Wachiu Siu 2 years ago
parent 9e2ea40380
commit 30637bbbe5
  1. 1
      README.md
  2. 6
      baked_in.go
  3. 9
      doc.go
  4. 2
      regexes.go
  5. 5
      translations/en/en.go
  6. 7
      translations/en/en_test.go
  7. 5
      translations/es/es.go
  8. 7
      translations/es/es_test.go
  9. 5
      translations/fa/fa.go
  10. 7
      translations/fa/fa_test.go
  11. 5
      translations/fr/fr.go
  12. 7
      translations/fr/fr_test.go
  13. 5
      translations/id/id.go
  14. 7
      translations/id/id_test.go
  15. 9
      translations/it/it.go
  16. 7
      translations/it/it_test.go
  17. 5
      translations/ja/ja.go
  18. 7
      translations/ja/ja_test.go
  19. 5
      translations/nl/nl.go
  20. 7
      translations/nl/nl_test.go
  21. 5
      translations/pt/pt.go
  22. 7
      translations/pt/pt_test.go
  23. 5
      translations/pt_BR/pt_BR.go
  24. 7
      translations/pt_BR/pt_BR_test.go
  25. 5
      translations/tr/tr.go
  26. 7
      translations/tr/tr_test.go
  27. 5
      translations/vi/vi.go
  28. 7
      translations/vi/vi_test.go
  29. 5
      translations/zh/zh.go
  30. 7
      translations/zh/zh_test.go
  31. 5
      translations/zh_tw/zh_tw.go
  32. 7
      translations/zh_tw/zh_tw_test.go
  33. 81
      validator_test.go

@ -147,6 +147,7 @@ Baked-in Validations
### Format:
| Tag | Description |
| - | - |
| base58 | Base58 String |
| base64 | Base64 String |
| base64url | Base64URL String |
| bic | Business Identifier Code (ISO 9362) |

@ -124,6 +124,7 @@ var (
"uri": isURI,
"urn_rfc2141": isUrnRFC2141, // RFC 2141
"file": isFile,
"base58": isBase58,
"base64": isBase64,
"base64url": isBase64URL,
"contains": contains,
@ -1301,6 +1302,11 @@ func isPostcodeByIso3166Alpha2Field(fl FieldLevel) bool {
return reg.MatchString(field.String())
}
// isBase58 is the validation function for validating if the current field's value is a valid base 58.
func isBase58(fl FieldLevel) bool {
return base58Regex.MatchString(fl.Field().String())
}
// isBase64 is the validation function for validating if the current field's value is a valid base 64.
func isBase64(fl FieldLevel) bool {
return base64Regex.MatchString(fl.Field().String())

@ -895,6 +895,15 @@ according to the RFC 2141 spec.
Usage: urn_rfc2141
Base58 String
This validates that a string value contains a valid base58 value.
Although an empty string is valid base58 this will report an empty string
as an error, if you wish to accept an empty string as valid you can use
this with the omitempty tag.
Usage: base58
Base64 String
This validates that a string value contains a valid base64 value.

@ -17,6 +17,7 @@ const (
hslaRegexString = "^hsla\\(\\s*(?:0|[1-9]\\d?|[12]\\d\\d|3[0-5]\\d|360)\\s*,\\s*(?:(?:0|[1-9]\\d?|100)%)\\s*,\\s*(?:(?:0|[1-9]\\d?|100)%)\\s*,\\s*(?:(?:0.[1-9]*)|[01])\\s*\\)$"
emailRegexString = "^(?:(?:(?:(?:[a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])+(?:\\.([a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])+)*)|(?:(?:\\x22)(?:(?:(?:(?:\\x20|\\x09)*(?:\\x0d\\x0a))?(?:\\x20|\\x09)+)?(?:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f]|\\x21|[\\x23-\\x5b]|[\\x5d-\\x7e]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(?:(?:[\\x01-\\x09\\x0b\\x0c\\x0d-\\x7f]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}]))))*(?:(?:(?:\\x20|\\x09)*(?:\\x0d\\x0a))?(\\x20|\\x09)+)?(?:\\x22))))@(?:(?:(?:[a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(?:(?:[a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])(?:[a-zA-Z]|\\d|-|\\.|~|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])*(?:[a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.)+(?:(?:[a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(?:(?:[a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])(?:[a-zA-Z]|\\d|-|\\.|~|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])*(?:[a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.?$"
e164RegexString = "^\\+[1-9]?[0-9]{7,14}$"
base58RegexString = "^[A-HJ-NP-Za-km-z1-9]+$"
base64RegexString = "^(?:[A-Za-z0-9+\\/]{4})*(?:[A-Za-z0-9+\\/]{2}==|[A-Za-z0-9+\\/]{3}=|[A-Za-z0-9+\\/]{4})$"
base64URLRegexString = "^(?:[A-Za-z0-9-_]{4})*(?:[A-Za-z0-9-_]{2}==|[A-Za-z0-9-_]{3}=|[A-Za-z0-9-_]{4})$"
iSBN10RegexString = "^(?:[0-9]{9}X|[0-9]{10})$"
@ -81,6 +82,7 @@ var (
hslaRegex = regexp.MustCompile(hslaRegexString)
e164Regex = regexp.MustCompile(e164RegexString)
emailRegex = regexp.MustCompile(emailRegexString)
base58Regex = regexp.MustCompile(base58RegexString)
base64Regex = regexp.MustCompile(base64RegexString)
base64URLRegex = regexp.MustCompile(base64URLRegexString)
iSBN10Regex = regexp.MustCompile(iSBN10RegexString)

@ -1356,6 +1356,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
translation: "{0} must be a valid boolean value",
override: false,
},
{
tag: "base58",
translation: "{0} must be a valid Base58 string",
override: false,
},
}
for _, t := range translations {

@ -152,6 +152,7 @@ func TestTranslations(t *testing.T) {
PostCodeCountry string
PostCodeByField string `validate:"postcode_iso3166_alpha2_field=PostCodeCountry"`
BooleanString string `validate:"boolean"`
Base58 string `validate:"base58"`
}
var test Test
@ -208,6 +209,8 @@ func TestTranslations(t *testing.T) {
test.Inner.RequiredIf = "abcd"
test.Base58 = "2TdKEvPTKpDtJo6pwxd79atZFQNWiSUT2T47nF9j5qFI"
err = validate.Struct(test)
NotEqual(t, err, nil)
@ -690,6 +693,10 @@ func TestTranslations(t *testing.T) {
ns: "Test.BooleanString",
expected: "BooleanString must be a valid boolean value",
},
{
ns: "Test.Base58",
expected: "Base58 must be a valid Base58 string",
},
}
for _, tt := range tests {

@ -1326,6 +1326,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
return s
},
},
{
tag: "base58",
translation: "{0} debe ser una cadena de Base58 válida",
override: false,
},
}
for _, t := range translations {

@ -142,6 +142,7 @@ func TestTranslations(t *testing.T) {
UniqueSlice []string `validate:"unique"`
UniqueArray [3]string `validate:"unique"`
UniqueMap map[string]string `validate:"unique"`
Base58 string `validate:"base58"`
}
var test Test
@ -191,6 +192,8 @@ func TestTranslations(t *testing.T) {
test.UniqueSlice = []string{"1234", "1234"}
test.UniqueMap = map[string]string{"key1": "1234", "key2": "1234"}
test.Base58 = "2TdKEvPTKpDtJo6pwxd79atZFQNWiSUT2T47nF9j5qFI"
err = validate.Struct(test)
NotEqual(t, err, nil)
@ -637,6 +640,10 @@ func TestTranslations(t *testing.T) {
ns: "Test.UniqueMap",
expected: "UniqueMap debe contener valores únicos",
},
{
ns: "Test.Base58",
expected: "Base58 debe ser una cadena de Base58 válida",
},
}
for _, tt := range tests {

@ -1341,6 +1341,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
return t
},
},
{
tag: "base58",
translation: "{0} باید یک متن درمبنای58 معتبر باشد",
override: false,
},
}
for _, t := range translations {

@ -148,6 +148,7 @@ func TestTranslations(t *testing.T) {
PostCode string `validate:"postcode_iso3166_alpha2=SG"`
PostCodeCountry string
PostCodeByField string `validate:"postcode_iso3166_alpha2_field=PostCodeCountry"`
Base58 string `validate:"base58"`
}
var test Test
@ -201,6 +202,8 @@ func TestTranslations(t *testing.T) {
test.UniqueMap = map[string]string{"key1": "1234", "key2": "1234"}
test.Datetime = "2008-Feb-01"
test.Base58 = "2TdKEvPTKpDtJo6pwxd79atZFQNWiSUT2T47nF9j5qFI"
err = validate.Struct(test)
NotEqual(t, err, nil)
@ -671,6 +674,10 @@ func TestTranslations(t *testing.T) {
ns: "Test.PostCodeByField",
expected: "PostCodeByField یک کدپستی معتبر کشور فیلد PostCodeCountry نیست",
},
{
ns: "Test.Base58",
expected: "Base58 باید یک متن درمبنای58 معتبر باشد",
},
}
for _, tt := range tests {

@ -1316,6 +1316,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
return s
},
},
{
tag: "base58",
translation: "{0} doit être une chaîne de caractères au format Base58 valide",
override: false,
},
}
for _, t := range translations {

@ -139,6 +139,7 @@ func TestTranslations(t *testing.T) {
StrPtrGte *string `validate:"gte=10"`
OneOfString string `validate:"oneof=red green"`
OneOfInt int `validate:"oneof=5 63"`
Base58 string `validate:"base58"`
}
var test Test
@ -185,6 +186,8 @@ func TestTranslations(t *testing.T) {
test.StrPtrMaxLen = &s
test.StrPtrLen = &s
test.Base58 = "2TdKEvPTKpDtJo6pwxd79atZFQNWiSUT2T47nF9j5qFI"
err = validate.Struct(test)
NotEqual(t, err, nil)
@ -619,6 +622,10 @@ func TestTranslations(t *testing.T) {
ns: "Test.OneOfInt",
expected: "OneOfInt doit être l'un des choix suivants [5 63]",
},
{
ns: "Test.Base58",
expected: "Base58 doit être une chaîne de caractères au format Base58 valide",
},
}
for _, tt := range tests {

@ -1316,6 +1316,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
return s
},
},
{
tag: "base58",
translation: "{0} harus berupa string Base58 yang valid",
override: false,
},
}
for _, t := range translations {

@ -139,6 +139,7 @@ func TestTranslations(t *testing.T) {
StrPtrGte *string `validate:"gte=10"`
OneOfString string `validate:"oneof=merah hijau"`
OneOfInt int `validate:"oneof=5 63"`
Base58 string `validate:"base58"`
}
var test Test
@ -185,6 +186,8 @@ func TestTranslations(t *testing.T) {
test.StrPtrMaxLen = &s
test.StrPtrLen = &s
test.Base58 = "2TdKEvPTKpDtJo6pwxd79atZFQNWiSUT2T47nF9j5qFI"
err = validate.Struct(test)
NotEqual(t, err, nil)
@ -619,6 +622,10 @@ func TestTranslations(t *testing.T) {
ns: "Test.OneOfInt",
expected: "OneOfInt harus berupa salah satu dari [5 63]",
},
{
ns: "Test.Base58",
expected: "Base58 harus berupa string Base58 yang valid",
},
}
for _, tt := range tests {

@ -124,7 +124,7 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
{
tag: "min",
customRegisFunc: func(ut ut.Translator) (err error) {
if err = ut.Add("min-string", "{0} deve essere lungo almeno {1}", false); err != nil {
return
}
@ -432,7 +432,7 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
{
tag: "lte",
customRegisFunc: func(ut ut.Translator) (err error) {
if err = ut.Add("lte-string", "{0} deve essere lungo al massimo {1}", false); err != nil {
return
}
@ -1205,6 +1205,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
override: false,
customTransFunc: customTransFuncV1,
},
{
tag: "base58",
translation: "{0} deve essere una stringa Base58 valida",
override: false,
},
}
for _, t := range translations {

@ -154,6 +154,7 @@ func TestTranslations(t *testing.T) {
PostCode string `validate:"postcode_iso3166_alpha2=SG"`
PostCodeCountry string
PostCodeByField string `validate:"postcode_iso3166_alpha2_field=PostCodeCountry"`
Base58 string `validate:"base58"`
}
var test Test
@ -212,6 +213,8 @@ func TestTranslations(t *testing.T) {
test.UniqueMap = map[string]string{"key1": "1234", "key2": "1234"}
test.Datetime = "2008-Feb-01"
test.Base58 = "2TdKEvPTKpDtJo6pwxd79atZFQNWiSUT2T47nF9j5qFI"
err = validate.Struct(test)
NotEqual(t, err, nil)
@ -706,6 +709,10 @@ func TestTranslations(t *testing.T) {
ns: "Test.PostCodeByField",
expected: "PostCodeByField non corrisponde al formato del codice postale dello stato nel campo PostCodeCountry",
},
{
ns: "Test.Base58",
expected: "Base58 deve essere una stringa Base58 valida",
},
}
for _, tt := range tests {

@ -1372,6 +1372,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
return s
},
},
{
tag: "base58",
translation: "{0}は正しいBase58文字列でなければなりません",
override: false,
},
}
for _, t := range translations {

@ -139,6 +139,7 @@ func TestTranslations(t *testing.T) {
StrPtrGte *string `validate:"gte=10"`
OneOfString string `validate:"oneof=red green"`
OneOfInt int `validate:"oneof=5 63"`
Base58 string `validate:"base58"`
}
var test Test
@ -185,6 +186,8 @@ func TestTranslations(t *testing.T) {
test.StrPtrMaxLen = &s
test.StrPtrLen = &s
test.Base58 = "2TdKEvPTKpDtJo6pwxd79atZFQNWiSUT2T47nF9j5qFI"
err = validate.Struct(test)
NotEqual(t, err, nil)
@ -619,6 +622,10 @@ func TestTranslations(t *testing.T) {
ns: "Test.OneOfInt",
expected: "OneOfIntは[5 63]のうちのいずれかでなければなりません",
},
{
ns: "Test.Base58",
expected: "Base58は正しいBase58文字列でなければなりません",
},
}
for _, tt := range tests {

@ -1316,6 +1316,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
return s
},
},
{
tag: "base58",
translation: "{0} moet een geldige Base58 string zijn",
override: false,
},
}
for _, t := range translations {

@ -139,6 +139,7 @@ func TestTranslations(t *testing.T) {
StrPtrGte *string `validate:"gte=10"`
OneOfString string `validate:"oneof=red green"`
OneOfInt int `validate:"oneof=5 63"`
Base58 string `validate:"base58"`
}
var test Test
@ -185,6 +186,8 @@ func TestTranslations(t *testing.T) {
test.StrPtrMaxLen = &s
test.StrPtrLen = &s
test.Base58 = "2TdKEvPTKpDtJo6pwxd79atZFQNWiSUT2T47nF9j5qFI"
err = validate.Struct(test)
NotEqual(t, err, nil)
@ -619,6 +622,10 @@ func TestTranslations(t *testing.T) {
ns: "Test.OneOfInt",
expected: "OneOfInt moet een van de volgende zijn [5 63]",
},
{
ns: "Test.Base58",
expected: "Base58 moet een geldige Base58 string zijn",
},
}
for _, tt := range tests {

@ -1356,6 +1356,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
return t
},
},
{
tag: "base58",
translation: "{0} deve ser uma string Base58 válida",
override: false,
},
}
for _, t := range translations {

@ -147,6 +147,7 @@ func TestTranslations(t *testing.T) {
LowercaseString string `validate:"lowercase"`
UppercaseString string `validate:"uppercase"`
Datetime string `validate:"datetime=2006-01-02"`
Base58 string `validate:"base58"`
}
var test Test
@ -200,6 +201,8 @@ func TestTranslations(t *testing.T) {
test.UniqueMap = map[string]string{"key1": "1234", "key2": "1234"}
test.Datetime = "2008-Feb-01"
test.Base58 = "2TdKEvPTKpDtJo6pwxd79atZFQNWiSUT2T47nF9j5qFI"
err = validate.Struct(test)
NotEqual(t, err, nil)
@ -662,6 +665,10 @@ func TestTranslations(t *testing.T) {
ns: "Test.Datetime",
expected: "Datetime não está no formato 2006-01-02",
},
{
ns: "Test.Base58",
expected: "Base58 deve ser uma string Base58 válida",
},
}
for _, tt := range tests {

@ -1321,6 +1321,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
translation: "{0} deve ser um valor booleano válido",
override: false,
},
{
tag: "base58",
translation: "{0} deve ser uma string Base58 válida",
override: false,
},
}
for _, t := range translations {

@ -140,6 +140,7 @@ func TestTranslations(t *testing.T) {
OneOfString string `validate:"oneof=red green"`
OneOfInt int `validate:"oneof=5 63"`
BooleanString string `validate:"boolean"`
Base58 string `validate:"base58"`
}
var test Test
@ -187,6 +188,8 @@ func TestTranslations(t *testing.T) {
test.StrPtrMaxLen = &s
test.StrPtrLen = &s
test.Base58 = "2TdKEvPTKpDtJo6pwxd79atZFQNWiSUT2T47nF9j5qFI"
err = validate.Struct(test)
NotEqual(t, err, nil)
@ -625,6 +628,10 @@ func TestTranslations(t *testing.T) {
ns: "Test.BooleanString",
expected: "BooleanString deve ser um valor booleano válido",
},
{
ns: "Test.Base58",
expected: "Base58 deve ser uma string Base58 válida",
},
}
for _, tt := range tests {

@ -1321,6 +1321,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
return s
},
},
{
tag: "base58",
translation: "{0} geçerli bir Base58 karakter dizesi olmalıdır",
override: false,
},
}
for _, t := range translations {

@ -142,6 +142,7 @@ func TestTranslations(t *testing.T) {
UniqueSlice []string `validate:"unique"`
UniqueArray [3]string `validate:"unique"`
UniqueMap map[string]string `validate:"unique"`
Base58 string `validate:"base58"`
}
var test Test
@ -191,6 +192,8 @@ func TestTranslations(t *testing.T) {
test.UniqueSlice = []string{"1234", "1234"}
test.UniqueMap = map[string]string{"key1": "1234", "key2": "1234"}
test.Base58 = "2TdKEvPTKpDtJo6pwxd79atZFQNWiSUT2T47nF9j5qFI"
err = validate.Struct(test)
NotEqual(t, err, nil)
@ -637,6 +640,10 @@ func TestTranslations(t *testing.T) {
ns: "Test.UniqueMap",
expected: "UniqueMap benzersiz değerler içermelidir",
},
{
ns: "Test.Base58",
expected: "Base58 geçerli bir Base58 karakter dizesi olmalıdır",
},
}
for _, tt := range tests {

@ -1341,6 +1341,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
return t
},
},
{
tag: "base58",
translation: "{0} phải là giá trị chuỗi Base58",
override: false,
},
}
for _, t := range translations {

@ -148,6 +148,7 @@ func TestTranslations(t *testing.T) {
PostCode string `validate:"postcode_iso3166_alpha2=SG"`
PostCodeCountry string
PostCodeByField string `validate:"postcode_iso3166_alpha2_field=PostCodeCountry"`
Base58 string `validate:"base58"`
}
var test Test
@ -201,6 +202,8 @@ func TestTranslations(t *testing.T) {
test.UniqueMap = map[string]string{"key1": "1234", "key2": "1234"}
test.Datetime = "2008-Feb-01"
test.Base58 = "2TdKEvPTKpDtJo6pwxd79atZFQNWiSUT2T47nF9j5qFI"
err = validate.Struct(test)
NotEqual(t, err, nil)
@ -671,6 +674,10 @@ func TestTranslations(t *testing.T) {
ns: "Test.PostCodeByField",
expected: "PostCodeByField sai định dạng postcode của quốc gia tương ứng thuộc trường PostCodeCountry",
},
{
ns: "Test.Base58",
expected: "Base58 phải là giá trị chuỗi Base58",
},
}
for _, tt := range tests {

@ -1428,6 +1428,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
return t
},
},
{
tag: "base58",
translation: "{0}必须是一个有效的Base58字符串",
override: false,
},
}
for _, t := range translations {

@ -148,6 +148,7 @@ func TestTranslations(t *testing.T) {
LowercaseString string `validate:"lowercase"`
UppercaseString string `validate:"uppercase"`
Datetime string `validate:"datetime=2006-01-02"`
Base58 string `validate:"base58"`
}
var test Test
@ -206,6 +207,8 @@ func TestTranslations(t *testing.T) {
test.Datetime = "20060102"
test.Base58 = "2TdKEvPTKpDtJo6pwxd79atZFQNWiSUT2T47nF9j5qFI"
err = validate.Struct(test)
NotEqual(t, err, nil)
@ -676,6 +679,10 @@ func TestTranslations(t *testing.T) {
ns: "Test.Datetime",
expected: "Datetime的格式必须是2006-01-02",
},
{
ns: "Test.Base58",
expected: "Base58必须是一个有效的Base58字符串",
},
}
for _, tt := range tests {

@ -1324,6 +1324,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
return t
},
},
{
tag: "base58",
translation: "{0}必須是一個有效的Base58字元串",
override: false,
},
}
for _, t := range translations {

@ -140,6 +140,7 @@ func TestTranslations(t *testing.T) {
OneOfString string `validate:"oneof=red green"`
OneOfInt int `validate:"oneof=5 63"`
Datetime string `validate:"datetime=2006-01-02"`
Base58 string `validate:"base58"`
}
var test Test
@ -188,6 +189,8 @@ func TestTranslations(t *testing.T) {
test.Datetime = "2008-Feb-01"
test.Base58 = "2TdKEvPTKpDtJo6pwxd79atZFQNWiSUT2T47nF9j5qFI"
err = validate.Struct(test)
NotEqual(t, err, nil)
@ -626,6 +629,10 @@ func TestTranslations(t *testing.T) {
ns: "Test.Datetime",
expected: "Datetime與2006-01-02格式不匹配",
},
{
ns: "Test.Base58",
expected: "Base58必須是一個有效的Base58字元串",
},
}
for _, tt := range tests {

@ -12264,25 +12264,62 @@ func TestCreditCardFormatValidation(t *testing.T) {
}
func TestMultiOrOperatorGroup(t *testing.T) {
tests := []struct {
Value int `validate:"eq=1|gte=5,eq=1|lt=7"`
expected bool
}{
{1, true}, {2, false}, {5, true}, {6, true}, {8, false},
}
validate := New()
for i, test := range tests {
errs := validate.Struct(test)
if test.expected {
if !IsEqual(errs, nil) {
t.Fatalf("Index: %d multi_group_of_OR_operators failed Error: %s", i, errs)
}
} else {
if IsEqual(errs, nil) {
t.Fatalf("Index: %d multi_group_of_OR_operators should have errs", i)
}
}
}
}
tests := []struct {
Value int `validate:"eq=1|gte=5,eq=1|lt=7"`
expected bool
}{
{1, true}, {2, false}, {5, true}, {6, true}, {8, false},
}
validate := New()
for i, test := range tests {
errs := validate.Struct(test)
if test.expected {
if !IsEqual(errs, nil) {
t.Fatalf("Index: %d multi_group_of_OR_operators failed Error: %s", i, errs)
}
} else {
if IsEqual(errs, nil) {
t.Fatalf("Index: %d multi_group_of_OR_operators should have errs", i)
}
}
}
}
func TestBase58Validation(t *testing.T) {
tests := []struct {
Value string `validate:"base58"`
Expected bool
}{
{"kpDDATsPB4AR2WmSDUiNuUz8Y9VFqN3WrDqyZTeC3hL", true},
{"", false},
{Value: "2TdKEvPTKpDtJo6pwxd79atZFQNWiSUT2T47nF9j5qFI", Expected: false},
}
validate := New()
for i, test := range tests {
errs := validate.Var(test.Value, "base58")
if test.Expected {
if !IsEqual(errs, nil) {
t.Fatalf("Index: %d Var() failed Error: %s", i, errs)
}
} else {
if IsEqual(errs, nil) {
t.Fatalf("Index: %d Var() failed Error: %s", i, errs)
} else {
val := getError(errs, "", "")
if val.Tag() != "base58" {
t.Fatalf("Index: %d Var() failed Error: %s", i, errs)
}
}
}
}
for i, test := range tests {
errs := validate.Struct(test)
if (test.Expected && errs != nil) || (!test.Expected && errs == nil) {
t.Fatalf("Index: %d Struct() failed Error: %s", i, errs)
}
}
}

Loading…
Cancel
Save