...

Source file src/github.com/go-playground/validator/v10/translations/zh/zh_test.go

Documentation: github.com/go-playground/validator/v10/translations/zh

     1  package zh
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	. "github.com/go-playground/assert/v2"
     8  	zhongwen "github.com/go-playground/locales/zh"
     9  	ut "github.com/go-playground/universal-translator"
    10  	"github.com/go-playground/validator/v10"
    11  )
    12  
    13  func TestTranslations(t *testing.T) {
    14  
    15  	zh := zhongwen.New()
    16  	uni := ut.New(zh, zh)
    17  	trans, _ := uni.GetTranslator("zh")
    18  
    19  	validate := validator.New()
    20  
    21  	err := RegisterDefaultTranslations(validate, trans)
    22  	Equal(t, err, nil)
    23  
    24  	type Inner struct {
    25  		EqCSFieldString    string
    26  		NeCSFieldString    string
    27  		GtCSFieldString    string
    28  		GteCSFieldString   string
    29  		LtCSFieldString    string
    30  		LteCSFieldString   string
    31  		RequiredIf         string
    32  		RequiredUnless     string
    33  		RequiredWith       string
    34  		RequiredWithAll    string
    35  		RequiredWithout    string
    36  		RequiredWithoutAll string
    37  		ExcludedIf         string
    38  		ExcludedUnless     string
    39  		ExcludedWith       string
    40  		ExcludedWithAll    string
    41  		ExcludedWithout    string
    42  		ExcludedWithoutAll string
    43  	}
    44  
    45  	type Test struct {
    46  		Inner                 Inner
    47  		RequiredString        string    `validate:"required"`
    48  		RequiredNumber        int       `validate:"required"`
    49  		RequiredMultiple      []string  `validate:"required"`
    50  		RequiredIf            string    `validate:"required_if=Inner.RequiredIf abcd"`
    51  		RequiredUnless        string    `validate:"required_unless=Inner.RequiredUnless abcd"`
    52  		RequiredWith          string    `validate:"required_with=Inner.RequiredWith"`
    53  		RequiredWithAll       string    `validate:"required_with_all=Inner.RequiredWith Inner.RequiredWithAll"`
    54  		RequiredWithout       string    `validate:"required_without=Inner.RequiredWithout"`
    55  		RequiredWithoutAll    string    `validate:"required_without_all=Inner.RequiredWithout Inner.RequiredWithoutAll"`
    56  		ExcludedIf            string    `validate:"excluded_if=Inner.ExcludedIf abcd"`
    57  		ExcludedUnless        string    `validate:"excluded_unless=Inner.ExcludedUnless abcd"`
    58  		ExcludedWith          string    `validate:"excluded_with=Inner.ExcludedWith"`
    59  		ExcludedWithout       string    `validate:"excluded_with_all=Inner.ExcludedWithAll"`
    60  		ExcludedWithAll       string    `validate:"excluded_without=Inner.ExcludedWithout"`
    61  		ExcludedWithoutAll    string    `validate:"excluded_without_all=Inner.ExcludedWithoutAll"`
    62  		IsDefault             string    `validate:"isdefault"`
    63  		LenString             string    `validate:"len=1"`
    64  		LenNumber             float64   `validate:"len=1113.00"`
    65  		LenMultiple           []string  `validate:"len=7"`
    66  		MinString             string    `validate:"min=1"`
    67  		MinNumber             float64   `validate:"min=1113.00"`
    68  		MinMultiple           []string  `validate:"min=7"`
    69  		MaxString             string    `validate:"max=3"`
    70  		MaxNumber             float64   `validate:"max=1113.00"`
    71  		MaxMultiple           []string  `validate:"max=7"`
    72  		EqString              string    `validate:"eq=3"`
    73  		EqNumber              float64   `validate:"eq=2.33"`
    74  		EqMultiple            []string  `validate:"eq=7"`
    75  		NeString              string    `validate:"ne="`
    76  		NeNumber              float64   `validate:"ne=0.00"`
    77  		NeMultiple            []string  `validate:"ne=0"`
    78  		LtString              string    `validate:"lt=3"`
    79  		LtNumber              float64   `validate:"lt=5.56"`
    80  		LtMultiple            []string  `validate:"lt=2"`
    81  		LtTime                time.Time `validate:"lt"`
    82  		LteString             string    `validate:"lte=3"`
    83  		LteNumber             float64   `validate:"lte=5.56"`
    84  		LteMultiple           []string  `validate:"lte=2"`
    85  		LteTime               time.Time `validate:"lte"`
    86  		GtString              string    `validate:"gt=3"`
    87  		GtNumber              float64   `validate:"gt=5.56"`
    88  		GtMultiple            []string  `validate:"gt=2"`
    89  		GtTime                time.Time `validate:"gt"`
    90  		GteString             string    `validate:"gte=3"`
    91  		GteNumber             float64   `validate:"gte=5.56"`
    92  		GteMultiple           []string  `validate:"gte=2"`
    93  		GteTime               time.Time `validate:"gte"`
    94  		EqFieldString         string    `validate:"eqfield=MaxString"`
    95  		EqCSFieldString       string    `validate:"eqcsfield=Inner.EqCSFieldString"`
    96  		NeCSFieldString       string    `validate:"necsfield=Inner.NeCSFieldString"`
    97  		GtCSFieldString       string    `validate:"gtcsfield=Inner.GtCSFieldString"`
    98  		GteCSFieldString      string    `validate:"gtecsfield=Inner.GteCSFieldString"`
    99  		LtCSFieldString       string    `validate:"ltcsfield=Inner.LtCSFieldString"`
   100  		LteCSFieldString      string    `validate:"ltecsfield=Inner.LteCSFieldString"`
   101  		NeFieldString         string    `validate:"nefield=EqFieldString"`
   102  		GtFieldString         string    `validate:"gtfield=MaxString"`
   103  		GteFieldString        string    `validate:"gtefield=MaxString"`
   104  		LtFieldString         string    `validate:"ltfield=MaxString"`
   105  		LteFieldString        string    `validate:"ltefield=MaxString"`
   106  		AlphaString           string    `validate:"alpha"`
   107  		AlphanumString        string    `validate:"alphanum"`
   108  		AlphanumUnicodeString string    `validate:"alphanumunicode"`
   109  		AlphaUnicodeString    string    `validate:"alphaunicode"`
   110  		NumericString         string    `validate:"numeric"`
   111  		NumberString          string    `validate:"number"`
   112  		HexadecimalString     string    `validate:"hexadecimal"`
   113  		HexColorString        string    `validate:"hexcolor"`
   114  		RGBColorString        string    `validate:"rgb"`
   115  		RGBAColorString       string    `validate:"rgba"`
   116  		HSLColorString        string    `validate:"hsl"`
   117  		HSLAColorString       string    `validate:"hsla"`
   118  		Email                 string    `validate:"email"`
   119  		URL                   string    `validate:"url"`
   120  		URI                   string    `validate:"uri"`
   121  		Base64                string    `validate:"base64"`
   122  		Contains              string    `validate:"contains=purpose"`
   123  		ContainsAny           string    `validate:"containsany=!@#$"`
   124  		ContainsRune          string    `validate:"containsrune=☻"`
   125  		Excludes              string    `validate:"excludes=text"`
   126  		ExcludesAll           string    `validate:"excludesall=!@#$"`
   127  		ExcludesRune          string    `validate:"excludesrune=☻"`
   128  		EndsWith              string    `validate:"endswith=end"`
   129  		StartsWith            string    `validate:"startswith=start"`
   130  		ISBN                  string    `validate:"isbn"`
   131  		ISBN10                string    `validate:"isbn10"`
   132  		ISBN13                string    `validate:"isbn13"`
   133  		ISSN                  string    `validate:"issn"`
   134  		UUID                  string    `validate:"uuid"`
   135  		UUID3                 string    `validate:"uuid3"`
   136  		UUID4                 string    `validate:"uuid4"`
   137  		UUID5                 string    `validate:"uuid5"`
   138  		ULID                  string    `validate:"ulid"`
   139  		ASCII                 string    `validate:"ascii"`
   140  		PrintableASCII        string    `validate:"printascii"`
   141  		MultiByte             string    `validate:"multibyte"`
   142  		DataURI               string    `validate:"datauri"`
   143  		Latitude              string    `validate:"latitude"`
   144  		Longitude             string    `validate:"longitude"`
   145  		SSN                   string    `validate:"ssn"`
   146  		IP                    string    `validate:"ip"`
   147  		IPv4                  string    `validate:"ipv4"`
   148  		IPv6                  string    `validate:"ipv6"`
   149  		CIDR                  string    `validate:"cidr"`
   150  		CIDRv4                string    `validate:"cidrv4"`
   151  		CIDRv6                string    `validate:"cidrv6"`
   152  		TCPAddr               string    `validate:"tcp_addr"`
   153  		TCPAddrv4             string    `validate:"tcp4_addr"`
   154  		TCPAddrv6             string    `validate:"tcp6_addr"`
   155  		UDPAddr               string    `validate:"udp_addr"`
   156  		UDPAddrv4             string    `validate:"udp4_addr"`
   157  		UDPAddrv6             string    `validate:"udp6_addr"`
   158  		IPAddr                string    `validate:"ip_addr"`
   159  		IPAddrv4              string    `validate:"ip4_addr"`
   160  		IPAddrv6              string    `validate:"ip6_addr"`
   161  		UinxAddr              string    `validate:"unix_addr"` // can't fail from within Go's net package currently, but maybe in the future
   162  		MAC                   string    `validate:"mac"`
   163  		IsColor               string    `validate:"iscolor"`
   164  		StrPtrMinLen          *string   `validate:"min=10"`
   165  		StrPtrMaxLen          *string   `validate:"max=1"`
   166  		StrPtrLen             *string   `validate:"len=2"`
   167  		StrPtrLt              *string   `validate:"lt=1"`
   168  		StrPtrLte             *string   `validate:"lte=1"`
   169  		StrPtrGt              *string   `validate:"gt=10"`
   170  		StrPtrGte             *string   `validate:"gte=10"`
   171  		OneOfString           string    `validate:"oneof=red green"`
   172  		OneOfInt              int       `validate:"oneof=5 63"`
   173  		JsonString            string    `validate:"json"`
   174  		LowercaseString       string    `validate:"lowercase"`
   175  		UppercaseString       string    `validate:"uppercase"`
   176  		Datetime              string    `validate:"datetime=2006-01-02"`
   177  		Image                 string    `validate:"image"`
   178  	}
   179  
   180  	var test Test
   181  
   182  	test.Inner.EqCSFieldString = "1234"
   183  	test.Inner.GtCSFieldString = "1234"
   184  	test.Inner.GteCSFieldString = "1234"
   185  	test.Inner.RequiredIf = "abcd"
   186  	test.Inner.RequiredUnless = "1234"
   187  	test.Inner.RequiredWith = "1234"
   188  	test.Inner.RequiredWithAll = "1234"
   189  	test.Inner.ExcludedIf = "abcd"
   190  	test.Inner.ExcludedUnless = "1234"
   191  	test.Inner.ExcludedWith = "1234"
   192  	test.Inner.ExcludedWithAll = "1234"
   193  
   194  	test.ExcludedIf = "1234"
   195  	test.ExcludedUnless = "1234"
   196  	test.ExcludedWith = "1234"
   197  	test.ExcludedWithAll = "1234"
   198  	test.ExcludedWithout = "1234"
   199  	test.ExcludedWithoutAll = "1234"
   200  
   201  	test.MaxString = "1234"
   202  	test.MaxNumber = 2000
   203  	test.MaxMultiple = make([]string, 9)
   204  
   205  	test.LtString = "1234"
   206  	test.LtNumber = 6
   207  	test.LtMultiple = make([]string, 3)
   208  	test.LtTime = time.Now().Add(time.Hour * 24)
   209  
   210  	test.LteString = "1234"
   211  	test.LteNumber = 6
   212  	test.LteMultiple = make([]string, 3)
   213  	test.LteTime = time.Now().Add(time.Hour * 24)
   214  
   215  	test.LtFieldString = "12345"
   216  	test.LteFieldString = "12345"
   217  
   218  	test.LtCSFieldString = "1234"
   219  	test.LteCSFieldString = "1234"
   220  
   221  	test.AlphaString = "abc3"
   222  	test.AlphanumString = "abc3!"
   223  	test.AlphanumUnicodeString = "abc3啊!"
   224  	test.AlphaUnicodeString = "abc3啊"
   225  	test.NumericString = "12E.00"
   226  	test.NumberString = "12E"
   227  
   228  	test.Excludes = "this is some test text"
   229  	test.ExcludesAll = "This is Great!"
   230  	test.ExcludesRune = "Love it ☻"
   231  
   232  	test.EndsWith = "this is some test text"
   233  	test.StartsWith = "this is some test text"
   234  
   235  	test.ASCII = "カタカナ"
   236  	test.PrintableASCII = "カタカナ"
   237  
   238  	test.MultiByte = "1234feerf"
   239  
   240  	s := "toolong"
   241  	test.StrPtrMaxLen = &s
   242  	test.StrPtrLen = &s
   243  
   244  	test.JsonString = "{\"foo\":\"bar\",}"
   245  
   246  	test.LowercaseString = "ABCDEFG"
   247  	test.UppercaseString = "abcdefg"
   248  
   249  	test.Datetime = "20060102"
   250  
   251  	err = validate.Struct(test)
   252  	NotEqual(t, err, nil)
   253  
   254  	errs, ok := err.(validator.ValidationErrors)
   255  	Equal(t, ok, true)
   256  
   257  	tests := []struct {
   258  		ns       string
   259  		expected string
   260  	}{
   261  		{
   262  			ns:       "Test.IsColor",
   263  			expected: "IsColor必须是一个有效的颜色",
   264  		},
   265  		{
   266  			ns:       "Test.MAC",
   267  			expected: "MAC必须是一个有效的MAC地址",
   268  		},
   269  		{
   270  			ns:       "Test.IPAddr",
   271  			expected: "IPAddr必须是一个有效的IP地址",
   272  		},
   273  		{
   274  			ns:       "Test.IPAddrv4",
   275  			expected: "IPAddrv4必须是一个有效的IPv4地址",
   276  		},
   277  		{
   278  			ns:       "Test.IPAddrv6",
   279  			expected: "IPAddrv6必须是一个有效的IPv6地址",
   280  		},
   281  		{
   282  			ns:       "Test.UDPAddr",
   283  			expected: "UDPAddr必须是一个有效的UDP地址",
   284  		},
   285  		{
   286  			ns:       "Test.UDPAddrv4",
   287  			expected: "UDPAddrv4必须是一个有效的IPv4 UDP地址",
   288  		},
   289  		{
   290  			ns:       "Test.UDPAddrv6",
   291  			expected: "UDPAddrv6必须是一个有效的IPv6 UDP地址",
   292  		},
   293  		{
   294  			ns:       "Test.TCPAddr",
   295  			expected: "TCPAddr必须是一个有效的TCP地址",
   296  		},
   297  		{
   298  			ns:       "Test.TCPAddrv4",
   299  			expected: "TCPAddrv4必须是一个有效的IPv4 TCP地址",
   300  		},
   301  		{
   302  			ns:       "Test.TCPAddrv6",
   303  			expected: "TCPAddrv6必须是一个有效的IPv6 TCP地址",
   304  		},
   305  		{
   306  			ns:       "Test.CIDR",
   307  			expected: "CIDR必须是一个有效的无类别域间路由(CIDR)",
   308  		},
   309  		{
   310  			ns:       "Test.CIDRv4",
   311  			expected: "CIDRv4必须是一个包含IPv4地址的有效无类别域间路由(CIDR)",
   312  		},
   313  		{
   314  			ns:       "Test.CIDRv6",
   315  			expected: "CIDRv6必须是一个包含IPv6地址的有效无类别域间路由(CIDR)",
   316  		},
   317  		{
   318  			ns:       "Test.SSN",
   319  			expected: "SSN必须是一个有效的社会安全号码(SSN)",
   320  		},
   321  		{
   322  			ns:       "Test.IP",
   323  			expected: "IP必须是一个有效的IP地址",
   324  		},
   325  		{
   326  			ns:       "Test.IPv4",
   327  			expected: "IPv4必须是一个有效的IPv4地址",
   328  		},
   329  		{
   330  			ns:       "Test.IPv6",
   331  			expected: "IPv6必须是一个有效的IPv6地址",
   332  		},
   333  		{
   334  			ns:       "Test.DataURI",
   335  			expected: "DataURI必须包含有效的数据URI",
   336  		},
   337  		{
   338  			ns:       "Test.Latitude",
   339  			expected: "Latitude必须包含有效的纬度坐标",
   340  		},
   341  		{
   342  			ns:       "Test.Longitude",
   343  			expected: "Longitude必须包含有效的经度坐标",
   344  		},
   345  		{
   346  			ns:       "Test.MultiByte",
   347  			expected: "MultiByte必须包含多字节字符",
   348  		},
   349  		{
   350  			ns:       "Test.ASCII",
   351  			expected: "ASCII必须只包含ascii字符",
   352  		},
   353  		{
   354  			ns:       "Test.PrintableASCII",
   355  			expected: "PrintableASCII必须只包含可打印的ascii字符",
   356  		},
   357  		{
   358  			ns:       "Test.UUID",
   359  			expected: "UUID必须是一个有效的UUID",
   360  		},
   361  		{
   362  			ns:       "Test.UUID3",
   363  			expected: "UUID3必须是一个有效的V3 UUID",
   364  		},
   365  		{
   366  			ns:       "Test.UUID4",
   367  			expected: "UUID4必须是一个有效的V4 UUID",
   368  		},
   369  		{
   370  			ns:       "Test.UUID5",
   371  			expected: "UUID5必须是一个有效的V5 UUID",
   372  		},
   373  		{
   374  			ns:       "Test.ULID",
   375  			expected: "ULID必须是一个有效的ULID",
   376  		},
   377  		{
   378  			ns:       "Test.ISBN",
   379  			expected: "ISBN必须是一个有效的ISBN编号",
   380  		},
   381  		{
   382  			ns:       "Test.ISBN10",
   383  			expected: "ISBN10必须是一个有效的ISBN-10编号",
   384  		},
   385  		{
   386  			ns:       "Test.ISBN13",
   387  			expected: "ISBN13必须是一个有效的ISBN-13编号",
   388  		},
   389  		{
   390  			ns:       "Test.ISSN",
   391  			expected: "ISSN必须是一个有效的ISSN编号",
   392  		},
   393  		{
   394  			ns:       "Test.EndsWith",
   395  			expected: "EndsWith必须以文本'end'结尾",
   396  		},
   397  		{
   398  			ns:       "Test.StartsWith",
   399  			expected: "StartsWith必须以文本'start'开头",
   400  		},
   401  		{
   402  			ns:       "Test.Excludes",
   403  			expected: "Excludes不能包含文本'text'",
   404  		},
   405  		{
   406  			ns:       "Test.ExcludesAll",
   407  			expected: "ExcludesAll不能包含以下任何字符'!@#$'",
   408  		},
   409  		{
   410  			ns:       "Test.ExcludesRune",
   411  			expected: "ExcludesRune不能包含'☻'",
   412  		},
   413  		{
   414  			ns:       "Test.ContainsRune",
   415  			expected: "ContainsRune必须包含字符'☻'",
   416  		},
   417  		{
   418  			ns:       "Test.ContainsAny",
   419  			expected: "ContainsAny必须包含至少一个以下字符'!@#$'",
   420  		},
   421  		{
   422  			ns:       "Test.Contains",
   423  			expected: "Contains必须包含文本'purpose'",
   424  		},
   425  		{
   426  			ns:       "Test.Base64",
   427  			expected: "Base64必须是一个有效的Base64字符串",
   428  		},
   429  		{
   430  			ns:       "Test.Email",
   431  			expected: "Email必须是一个有效的邮箱",
   432  		},
   433  		{
   434  			ns:       "Test.URL",
   435  			expected: "URL必须是一个有效的URL",
   436  		},
   437  		{
   438  			ns:       "Test.URI",
   439  			expected: "URI必须是一个有效的URI",
   440  		},
   441  		{
   442  			ns:       "Test.RGBColorString",
   443  			expected: "RGBColorString必须是一个有效的RGB颜色",
   444  		},
   445  		{
   446  			ns:       "Test.RGBAColorString",
   447  			expected: "RGBAColorString必须是一个有效的RGBA颜色",
   448  		},
   449  		{
   450  			ns:       "Test.HSLColorString",
   451  			expected: "HSLColorString必须是一个有效的HSL颜色",
   452  		},
   453  		{
   454  			ns:       "Test.HSLAColorString",
   455  			expected: "HSLAColorString必须是一个有效的HSLA颜色",
   456  		},
   457  		{
   458  			ns:       "Test.HexadecimalString",
   459  			expected: "HexadecimalString必须是一个有效的十六进制",
   460  		},
   461  		{
   462  			ns:       "Test.HexColorString",
   463  			expected: "HexColorString必须是一个有效的十六进制颜色",
   464  		},
   465  		{
   466  			ns:       "Test.NumberString",
   467  			expected: "NumberString必须是一个有效的数字",
   468  		},
   469  		{
   470  			ns:       "Test.NumericString",
   471  			expected: "NumericString必须是一个有效的数值",
   472  		},
   473  		{
   474  			ns:       "Test.AlphaUnicodeString",
   475  			expected: "AlphaUnicodeString只能包含字母和Unicode字符",
   476  		},
   477  		{
   478  			ns:       "Test.AlphanumUnicodeString",
   479  			expected: "AlphanumUnicodeString只能包含字母数字和Unicode字符",
   480  		},
   481  		{
   482  			ns:       "Test.AlphanumString",
   483  			expected: "AlphanumString只能包含字母和数字",
   484  		},
   485  		{
   486  			ns:       "Test.AlphaString",
   487  			expected: "AlphaString只能包含字母",
   488  		},
   489  		{
   490  			ns:       "Test.LtFieldString",
   491  			expected: "LtFieldString必须小于MaxString",
   492  		},
   493  		{
   494  			ns:       "Test.LteFieldString",
   495  			expected: "LteFieldString必须小于或等于MaxString",
   496  		},
   497  		{
   498  			ns:       "Test.GtFieldString",
   499  			expected: "GtFieldString必须大于MaxString",
   500  		},
   501  		{
   502  			ns:       "Test.GteFieldString",
   503  			expected: "GteFieldString必须大于或等于MaxString",
   504  		},
   505  		{
   506  			ns:       "Test.NeFieldString",
   507  			expected: "NeFieldString不能等于EqFieldString",
   508  		},
   509  		{
   510  			ns:       "Test.LtCSFieldString",
   511  			expected: "LtCSFieldString必须小于Inner.LtCSFieldString",
   512  		},
   513  		{
   514  			ns:       "Test.LteCSFieldString",
   515  			expected: "LteCSFieldString必须小于或等于Inner.LteCSFieldString",
   516  		},
   517  		{
   518  			ns:       "Test.GtCSFieldString",
   519  			expected: "GtCSFieldString必须大于Inner.GtCSFieldString",
   520  		},
   521  		{
   522  			ns:       "Test.GteCSFieldString",
   523  			expected: "GteCSFieldString必须大于或等于Inner.GteCSFieldString",
   524  		},
   525  		{
   526  			ns:       "Test.NeCSFieldString",
   527  			expected: "NeCSFieldString不能等于Inner.NeCSFieldString",
   528  		},
   529  		{
   530  			ns:       "Test.EqCSFieldString",
   531  			expected: "EqCSFieldString必须等于Inner.EqCSFieldString",
   532  		},
   533  		{
   534  			ns:       "Test.EqFieldString",
   535  			expected: "EqFieldString必须等于MaxString",
   536  		},
   537  		{
   538  			ns:       "Test.GteString",
   539  			expected: "GteString长度必须至少为3个字符",
   540  		},
   541  		{
   542  			ns:       "Test.GteNumber",
   543  			expected: "GteNumber必须大于或等于5.56",
   544  		},
   545  		{
   546  			ns:       "Test.GteMultiple",
   547  			expected: "GteMultiple必须至少包含2项",
   548  		},
   549  		{
   550  			ns:       "Test.GteTime",
   551  			expected: "GteTime必须大于或等于当前日期和时间",
   552  		},
   553  		{
   554  			ns:       "Test.GtString",
   555  			expected: "GtString长度必须大于3个字符",
   556  		},
   557  		{
   558  			ns:       "Test.GtNumber",
   559  			expected: "GtNumber必须大于5.56",
   560  		},
   561  		{
   562  			ns:       "Test.GtMultiple",
   563  			expected: "GtMultiple必须大于2项",
   564  		},
   565  		{
   566  			ns:       "Test.GtTime",
   567  			expected: "GtTime必须大于当前日期和时间",
   568  		},
   569  		{
   570  			ns:       "Test.LteString",
   571  			expected: "LteString长度不能超过3个字符",
   572  		},
   573  		{
   574  			ns:       "Test.LteNumber",
   575  			expected: "LteNumber必须小于或等于5.56",
   576  		},
   577  		{
   578  			ns:       "Test.LteMultiple",
   579  			expected: "LteMultiple最多只能包含2项",
   580  		},
   581  		{
   582  			ns:       "Test.LteTime",
   583  			expected: "LteTime必须小于或等于当前日期和时间",
   584  		},
   585  		{
   586  			ns:       "Test.LtString",
   587  			expected: "LtString长度必须小于3个字符",
   588  		},
   589  		{
   590  			ns:       "Test.LtNumber",
   591  			expected: "LtNumber必须小于5.56",
   592  		},
   593  		{
   594  			ns:       "Test.LtMultiple",
   595  			expected: "LtMultiple必须包含少于2项",
   596  		},
   597  		{
   598  			ns:       "Test.LtTime",
   599  			expected: "LtTime必须小于当前日期和时间",
   600  		},
   601  		{
   602  			ns:       "Test.NeString",
   603  			expected: "NeString不能等于",
   604  		},
   605  		{
   606  			ns:       "Test.NeNumber",
   607  			expected: "NeNumber不能等于0.00",
   608  		},
   609  		{
   610  			ns:       "Test.NeMultiple",
   611  			expected: "NeMultiple不能等于0",
   612  		},
   613  		{
   614  			ns:       "Test.EqString",
   615  			expected: "EqString不等于3",
   616  		},
   617  		{
   618  			ns:       "Test.EqNumber",
   619  			expected: "EqNumber不等于2.33",
   620  		},
   621  		{
   622  			ns:       "Test.EqMultiple",
   623  			expected: "EqMultiple不等于7",
   624  		},
   625  		{
   626  			ns:       "Test.MaxString",
   627  			expected: "MaxString长度不能超过3个字符",
   628  		},
   629  		{
   630  			ns:       "Test.MaxNumber",
   631  			expected: "MaxNumber必须小于或等于1,113.00",
   632  		},
   633  		{
   634  			ns:       "Test.MaxMultiple",
   635  			expected: "MaxMultiple最多只能包含7项",
   636  		},
   637  		{
   638  			ns:       "Test.MinString",
   639  			expected: "MinString长度必须至少为1个字符",
   640  		},
   641  		{
   642  			ns:       "Test.MinNumber",
   643  			expected: "MinNumber最小只能为1,113.00",
   644  		},
   645  		{
   646  			ns:       "Test.MinMultiple",
   647  			expected: "MinMultiple必须至少包含7项",
   648  		},
   649  		{
   650  			ns:       "Test.LenString",
   651  			expected: "LenString长度必须是1个字符",
   652  		},
   653  		{
   654  			ns:       "Test.LenNumber",
   655  			expected: "LenNumber必须等于1,113.00",
   656  		},
   657  		{
   658  			ns:       "Test.LenMultiple",
   659  			expected: "LenMultiple必须包含7项",
   660  		},
   661  		{
   662  			ns:       "Test.RequiredString",
   663  			expected: "RequiredString为必填字段",
   664  		},
   665  		{
   666  			ns:       "Test.RequiredNumber",
   667  			expected: "RequiredNumber为必填字段",
   668  		},
   669  		{
   670  			ns:       "Test.RequiredMultiple",
   671  			expected: "RequiredMultiple为必填字段",
   672  		},
   673  		{
   674  			ns:       "Test.RequiredUnless",
   675  			expected: "RequiredUnless为必填字段",
   676  		},
   677  		{
   678  			ns:       "Test.RequiredWith",
   679  			expected: "RequiredWith为必填字段",
   680  		},
   681  		{
   682  			ns:       "Test.RequiredWithAll",
   683  			expected: "RequiredWithAll为必填字段",
   684  		},
   685  		{
   686  			ns:       "Test.RequiredWithout",
   687  			expected: "RequiredWithout为必填字段",
   688  		},
   689  		{
   690  			ns:       "Test.RequiredWithoutAll",
   691  			expected: "RequiredWithoutAll为必填字段",
   692  		},
   693  		{
   694  			ns:       "Test.ExcludedIf",
   695  			expected: "ExcludedIf为禁填字段",
   696  		},
   697  		{
   698  			ns:       "Test.ExcludedUnless",
   699  			expected: "ExcludedUnless为禁填字段",
   700  		},
   701  		{
   702  			ns:       "Test.ExcludedWith",
   703  			expected: "ExcludedWith为禁填字段",
   704  		},
   705  		{
   706  			ns:       "Test.ExcludedWithAll",
   707  			expected: "ExcludedWithAll为禁填字段",
   708  		},
   709  		{
   710  			ns:       "Test.ExcludedWithout",
   711  			expected: "ExcludedWithout为禁填字段",
   712  		},
   713  		{
   714  			ns:       "Test.ExcludedWithoutAll",
   715  			expected: "ExcludedWithoutAll为禁填字段",
   716  		},
   717  		{
   718  			ns:       "Test.StrPtrMinLen",
   719  			expected: "StrPtrMinLen长度必须至少为10个字符",
   720  		},
   721  		{
   722  			ns:       "Test.StrPtrMaxLen",
   723  			expected: "StrPtrMaxLen长度不能超过1个字符",
   724  		},
   725  		{
   726  			ns:       "Test.StrPtrLen",
   727  			expected: "StrPtrLen长度必须是2个字符",
   728  		},
   729  		{
   730  			ns:       "Test.StrPtrLt",
   731  			expected: "StrPtrLt长度必须小于1个字符",
   732  		},
   733  		{
   734  			ns:       "Test.StrPtrLte",
   735  			expected: "StrPtrLte长度不能超过1个字符",
   736  		},
   737  		{
   738  			ns:       "Test.StrPtrGt",
   739  			expected: "StrPtrGt长度必须大于10个字符",
   740  		},
   741  		{
   742  			ns:       "Test.StrPtrGte",
   743  			expected: "StrPtrGte长度必须至少为10个字符",
   744  		},
   745  		{
   746  			ns:       "Test.OneOfString",
   747  			expected: "OneOfString必须是[red green]中的一个",
   748  		},
   749  		{
   750  			ns:       "Test.OneOfInt",
   751  			expected: "OneOfInt必须是[5 63]中的一个",
   752  		},
   753  		{
   754  			ns:       "Test.JsonString",
   755  			expected: "JsonString必须是一个JSON字符串",
   756  		},
   757  		{
   758  			ns:       "Test.LowercaseString",
   759  			expected: "LowercaseString必须是小写字母",
   760  		},
   761  		{
   762  			ns:       "Test.UppercaseString",
   763  			expected: "UppercaseString必须是大写字母",
   764  		},
   765  		{
   766  			ns:       "Test.Datetime",
   767  			expected: "Datetime的格式必须是2006-01-02",
   768  		},
   769  		{
   770  			ns:       "Test.Image",
   771  			expected: "Image 必须是有效图像",
   772  		},
   773  	}
   774  
   775  	for _, tt := range tests {
   776  
   777  		var fe validator.FieldError
   778  
   779  		for _, e := range errs {
   780  			if tt.ns == e.Namespace() {
   781  				fe = e
   782  				break
   783  			}
   784  		}
   785  
   786  		NotEqual(t, fe, nil)
   787  		Equal(t, tt.expected, fe.Translate(trans))
   788  	}
   789  
   790  }
   791  

View as plain text