|
|
|
@ -80,6 +80,8 @@ func TestTranslations(t *testing.T) { |
|
|
|
|
LteFieldString string `validate:"ltefield=MaxString"` |
|
|
|
|
AlphaString string `validate:"alpha"` |
|
|
|
|
AlphanumString string `validate:"alphanum"` |
|
|
|
|
AlphanumUnicodeString string `validate:"alphanumunicode"` |
|
|
|
|
AlphaUnicodeString string `validate:"alphaunicode"` |
|
|
|
|
NumericString string `validate:"numeric"` |
|
|
|
|
NumberString string `validate:"number"` |
|
|
|
|
HexadecimalString string `validate:"hexadecimal"` |
|
|
|
@ -88,15 +90,19 @@ func TestTranslations(t *testing.T) { |
|
|
|
|
RGBAColorString string `validate:"rgba"` |
|
|
|
|
HSLColorString string `validate:"hsl"` |
|
|
|
|
HSLAColorString string `validate:"hsla"` |
|
|
|
|
E164 string `validate:"e164"` |
|
|
|
|
Email string `validate:"email"` |
|
|
|
|
URL string `validate:"url"` |
|
|
|
|
URI string `validate:"uri"` |
|
|
|
|
Base64 string `validate:"base64"` |
|
|
|
|
Contains string `validate:"contains=purpose"` |
|
|
|
|
ContainsAny string `validate:"containsany=!@#$"` |
|
|
|
|
ContainsRune string `validate:"containsrune=☻"` |
|
|
|
|
Excludes string `validate:"excludes=text"` |
|
|
|
|
ExcludesAll string `validate:"excludesall=!@#$"` |
|
|
|
|
ExcludesRune string `validate:"excludesrune=☻"` |
|
|
|
|
EndsWith string `validate:"endswith=end"` |
|
|
|
|
StartsWith string `validate:"startswith=start"` |
|
|
|
|
ISBN string `validate:"isbn"` |
|
|
|
|
ISBN10 string `validate:"isbn10"` |
|
|
|
|
ISBN13 string `validate:"isbn13"` |
|
|
|
@ -139,7 +145,15 @@ func TestTranslations(t *testing.T) { |
|
|
|
|
StrPtrGte *string `validate:"gte=10"` |
|
|
|
|
OneOfString string `validate:"oneof=red green"` |
|
|
|
|
OneOfInt int `validate:"oneof=5 63"` |
|
|
|
|
UniqueSlice []string `validate:"unique"` |
|
|
|
|
UniqueArray [3]string `validate:"unique"` |
|
|
|
|
UniqueMap map[string]string `validate:"unique"` |
|
|
|
|
JSONString string `validate:"json"` |
|
|
|
|
JWTString string `validate:"jwt"` |
|
|
|
|
LowercaseString string `validate:"lowercase"` |
|
|
|
|
UppercaseString string `validate:"uppercase"` |
|
|
|
|
Datetime string `validate:"datetime=2006-01-02"` |
|
|
|
|
BooleanString string `validate:"boolean"` |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var test Test |
|
|
|
@ -170,6 +184,8 @@ func TestTranslations(t *testing.T) { |
|
|
|
|
|
|
|
|
|
test.AlphaString = "abc3" |
|
|
|
|
test.AlphanumString = "abc3!" |
|
|
|
|
test.AlphanumUnicodeString = "abc3啊!" |
|
|
|
|
test.AlphaUnicodeString = "abc3啊" |
|
|
|
|
test.NumericString = "12E.00" |
|
|
|
|
test.NumberString = "12E" |
|
|
|
|
|
|
|
|
@ -177,6 +193,9 @@ func TestTranslations(t *testing.T) { |
|
|
|
|
test.ExcludesAll = "This is Great!" |
|
|
|
|
test.ExcludesRune = "Love it ☻" |
|
|
|
|
|
|
|
|
|
test.EndsWith = "this is some test text" |
|
|
|
|
test.StartsWith = "this is some test text" |
|
|
|
|
|
|
|
|
|
test.ASCII = "カタカナ" |
|
|
|
|
test.PrintableASCII = "カタカナ" |
|
|
|
|
|
|
|
|
@ -186,7 +205,16 @@ func TestTranslations(t *testing.T) { |
|
|
|
|
test.StrPtrMaxLen = &s |
|
|
|
|
test.StrPtrLen = &s |
|
|
|
|
|
|
|
|
|
test.Datetime = "2008-Feb-01" |
|
|
|
|
test.JSONString = "{\"foo\":\"bar\",}" |
|
|
|
|
|
|
|
|
|
test.LowercaseString = "ABCDEFG" |
|
|
|
|
test.UppercaseString = "abcdefg" |
|
|
|
|
|
|
|
|
|
test.UniqueSlice = []string{"1234", "1234"} |
|
|
|
|
test.UniqueMap = map[string]string{"key1": "1234", "key2": "1234"} |
|
|
|
|
|
|
|
|
|
test.Datetime = "20060102" |
|
|
|
|
test.BooleanString = "A" |
|
|
|
|
|
|
|
|
|
err = validate.Struct(test) |
|
|
|
|
NotEqual(t, err, nil) |
|
|
|
@ -326,6 +354,14 @@ func TestTranslations(t *testing.T) { |
|
|
|
|
ns: "Test.ISBN13", |
|
|
|
|
expected: "ISBN13必須是一個有效的ISBN-13編號", |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
ns: "Test.EndsWith", |
|
|
|
|
expected: "EndsWith必須以文本'end'結尾", |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
ns: "Test.StartsWith", |
|
|
|
|
expected: "StartsWith必須以文本'start'開頭", |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
ns: "Test.Excludes", |
|
|
|
|
expected: "Excludes不能包含文字'text'", |
|
|
|
@ -338,6 +374,10 @@ func TestTranslations(t *testing.T) { |
|
|
|
|
ns: "Test.ExcludesRune", |
|
|
|
|
expected: "ExcludesRune不能包含'☻'", |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
ns: "Test.ContainsRune", |
|
|
|
|
expected: "ContainsRune必須包含字元'☻'", |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
ns: "Test.ContainsAny", |
|
|
|
|
expected: "ContainsAny必須包含至少一個以下字元'!@#$'", |
|
|
|
@ -350,6 +390,10 @@ func TestTranslations(t *testing.T) { |
|
|
|
|
ns: "Test.Base64", |
|
|
|
|
expected: "Base64必須是一個有效的Base64字元串", |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
ns: "Test.E164", |
|
|
|
|
expected: "E164必須是一個有效的E.164手機號", |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
ns: "Test.Email", |
|
|
|
|
expected: "Email必須是一個有效的信箱", |
|
|
|
@ -394,6 +438,14 @@ func TestTranslations(t *testing.T) { |
|
|
|
|
ns: "Test.NumericString", |
|
|
|
|
expected: "NumericString必須是一個有效的數值", |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
ns: "Test.AlphaUnicodeString", |
|
|
|
|
expected: "AlphaUnicodeString只能包含字母和Unicode字元", |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
ns: "Test.AlphanumUnicodeString", |
|
|
|
|
expected: "AlphanumUnicodeString只能包含字母數字和Unicode字元", |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
ns: "Test.AlphanumString", |
|
|
|
|
expected: "AlphanumString只能包含字母和數字", |
|
|
|
@ -622,9 +674,41 @@ func TestTranslations(t *testing.T) { |
|
|
|
|
ns: "Test.OneOfInt", |
|
|
|
|
expected: "OneOfInt必須是[5 63]中的一個", |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
ns: "Test.UniqueSlice", |
|
|
|
|
expected: "UniqueSlice字段內的值必須唯一", |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
ns: "Test.UniqueArray", |
|
|
|
|
expected: "UniqueArray字段內的值必須唯一", |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
ns: "Test.UniqueMap", |
|
|
|
|
expected: "UniqueMap字段內的值必須唯一", |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
ns: "Test.JSONString", |
|
|
|
|
expected: "JSONString必須是一個有效的JSON字符串", |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
ns: "Test.JWTString", |
|
|
|
|
expected: "JWTString必須是一個有效的JWT字符串", |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
ns: "Test.LowercaseString", |
|
|
|
|
expected: "LowercaseString必須是小寫字母", |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
ns: "Test.UppercaseString", |
|
|
|
|
expected: "UppercaseString必須是大寫字母", |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
ns: "Test.Datetime", |
|
|
|
|
expected: "Datetime與2006-01-02格式不匹配", |
|
|
|
|
expected: "Datetime的格式必須是2006-01-02", |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
ns: "Test.BooleanString", |
|
|
|
|
expected: "BooleanString必須是一個有效的布爾值", |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|