diff --git a/README.md b/README.md index 8b730b6..50605fa 100644 --- a/README.md +++ b/README.md @@ -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) | diff --git a/baked_in.go b/baked_in.go index f2f0939..606fe57 100644 --- a/baked_in.go +++ b/baked_in.go @@ -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()) diff --git a/doc.go b/doc.go index 7341c67..7d270ec 100644 --- a/doc.go +++ b/doc.go @@ -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. diff --git a/regexes.go b/regexes.go index 9c1c634..4918b50 100644 --- a/regexes.go +++ b/regexes.go @@ -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) diff --git a/translations/en/en.go b/translations/en/en.go index ee05f91..eb5c0ef 100644 --- a/translations/en/en.go +++ b/translations/en/en.go @@ -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 { diff --git a/translations/en/en_test.go b/translations/en/en_test.go index 9cb6deb..639ea81 100644 --- a/translations/en/en_test.go +++ b/translations/en/en_test.go @@ -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 { diff --git a/translations/es/es.go b/translations/es/es.go index 2635408..5979e50 100644 --- a/translations/es/es.go +++ b/translations/es/es.go @@ -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 { diff --git a/translations/es/es_test.go b/translations/es/es_test.go index 814f2ab..7bfcf65 100644 --- a/translations/es/es_test.go +++ b/translations/es/es_test.go @@ -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 { diff --git a/translations/fa/fa.go b/translations/fa/fa.go index b7c100b..b41c916 100644 --- a/translations/fa/fa.go +++ b/translations/fa/fa.go @@ -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 { diff --git a/translations/fa/fa_test.go b/translations/fa/fa_test.go index 1acb8cf..56e880f 100644 --- a/translations/fa/fa_test.go +++ b/translations/fa/fa_test.go @@ -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 { diff --git a/translations/fr/fr.go b/translations/fr/fr.go index c455195..f8df361 100644 --- a/translations/fr/fr.go +++ b/translations/fr/fr.go @@ -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 { diff --git a/translations/fr/fr_test.go b/translations/fr/fr_test.go index f1ba280..880d44a 100644 --- a/translations/fr/fr_test.go +++ b/translations/fr/fr_test.go @@ -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 { diff --git a/translations/id/id.go b/translations/id/id.go index 08b6ad5..caf7407 100644 --- a/translations/id/id.go +++ b/translations/id/id.go @@ -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 { diff --git a/translations/id/id_test.go b/translations/id/id_test.go index 5d80940..7f83553 100644 --- a/translations/id/id_test.go +++ b/translations/id/id_test.go @@ -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 { diff --git a/translations/it/it.go b/translations/it/it.go index 0b46fc4..daa2103 100644 --- a/translations/it/it.go +++ b/translations/it/it.go @@ -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 { diff --git a/translations/it/it_test.go b/translations/it/it_test.go index 98d2ab5..aaf01d7 100644 --- a/translations/it/it_test.go +++ b/translations/it/it_test.go @@ -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 { diff --git a/translations/ja/ja.go b/translations/ja/ja.go index 1cc67a4..c7cc763 100644 --- a/translations/ja/ja.go +++ b/translations/ja/ja.go @@ -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 { diff --git a/translations/ja/ja_test.go b/translations/ja/ja_test.go index 7950251..b2ad079 100644 --- a/translations/ja/ja_test.go +++ b/translations/ja/ja_test.go @@ -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 { diff --git a/translations/nl/nl.go b/translations/nl/nl.go index ca7c554..b83fb30 100644 --- a/translations/nl/nl.go +++ b/translations/nl/nl.go @@ -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 { diff --git a/translations/nl/nl_test.go b/translations/nl/nl_test.go index 137e0c4..bf41dba 100644 --- a/translations/nl/nl_test.go +++ b/translations/nl/nl_test.go @@ -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 { diff --git a/translations/pt/pt.go b/translations/pt/pt.go index 4748331..ebf1752 100644 --- a/translations/pt/pt.go +++ b/translations/pt/pt.go @@ -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 { diff --git a/translations/pt/pt_test.go b/translations/pt/pt_test.go index 0422ab0..b0434d6 100644 --- a/translations/pt/pt_test.go +++ b/translations/pt/pt_test.go @@ -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 { diff --git a/translations/pt_BR/pt_BR.go b/translations/pt_BR/pt_BR.go index d6883aa..68ff38a 100644 --- a/translations/pt_BR/pt_BR.go +++ b/translations/pt_BR/pt_BR.go @@ -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 { diff --git a/translations/pt_BR/pt_BR_test.go b/translations/pt_BR/pt_BR_test.go index 426f246..20018f3 100644 --- a/translations/pt_BR/pt_BR_test.go +++ b/translations/pt_BR/pt_BR_test.go @@ -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 { diff --git a/translations/tr/tr.go b/translations/tr/tr.go index 2e88a20..c3a4b88 100644 --- a/translations/tr/tr.go +++ b/translations/tr/tr.go @@ -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 { diff --git a/translations/tr/tr_test.go b/translations/tr/tr_test.go index b72329e..2a687cd 100644 --- a/translations/tr/tr_test.go +++ b/translations/tr/tr_test.go @@ -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 { diff --git a/translations/vi/vi.go b/translations/vi/vi.go index 009ba4c..5a3b529 100644 --- a/translations/vi/vi.go +++ b/translations/vi/vi.go @@ -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 { diff --git a/translations/vi/vi_test.go b/translations/vi/vi_test.go index 6e12866..46b7bae 100644 --- a/translations/vi/vi_test.go +++ b/translations/vi/vi_test.go @@ -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 { diff --git a/translations/zh/zh.go b/translations/zh/zh.go index 80165d0..0b4af5e 100644 --- a/translations/zh/zh.go +++ b/translations/zh/zh.go @@ -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 { diff --git a/translations/zh/zh_test.go b/translations/zh/zh_test.go index cf76590..b9bc4fb 100644 --- a/translations/zh/zh_test.go +++ b/translations/zh/zh_test.go @@ -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 { diff --git a/translations/zh_tw/zh_tw.go b/translations/zh_tw/zh_tw.go index c39a8d7..429fb63 100644 --- a/translations/zh_tw/zh_tw.go +++ b/translations/zh_tw/zh_tw.go @@ -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 { diff --git a/translations/zh_tw/zh_tw_test.go b/translations/zh_tw/zh_tw_test.go index 129c51a..b4ef721 100644 --- a/translations/zh_tw/zh_tw_test.go +++ b/translations/zh_tw/zh_tw_test.go @@ -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 { diff --git a/validator_test.go b/validator_test.go index 1ebdea5..ac773bc 100644 --- a/validator_test.go +++ b/validator_test.go @@ -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) + } + } +}