OSDN Git Service

new repo
[bytom/vapor.git] / vendor / gopkg.in / go-playground / validator.v9 / translations / en / en_test.go
1 package en
2
3 import (
4         "testing"
5         "time"
6
7         english "github.com/go-playground/locales/en"
8         ut "github.com/go-playground/universal-translator"
9         . "gopkg.in/go-playground/assert.v1"
10         "gopkg.in/go-playground/validator.v9"
11 )
12
13 func TestTranslations(t *testing.T) {
14
15         eng := english.New()
16         uni := ut.New(eng, eng)
17         trans, _ := uni.GetTranslator("en")
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         }
32
33         type Test struct {
34                 Inner             Inner
35                 RequiredString    string    `validate:"required"`
36                 RequiredNumber    int       `validate:"required"`
37                 RequiredMultiple  []string  `validate:"required"`
38                 LenString         string    `validate:"len=1"`
39                 LenNumber         float64   `validate:"len=1113.00"`
40                 LenMultiple       []string  `validate:"len=7"`
41                 MinString         string    `validate:"min=1"`
42                 MinNumber         float64   `validate:"min=1113.00"`
43                 MinMultiple       []string  `validate:"min=7"`
44                 MaxString         string    `validate:"max=3"`
45                 MaxNumber         float64   `validate:"max=1113.00"`
46                 MaxMultiple       []string  `validate:"max=7"`
47                 EqString          string    `validate:"eq=3"`
48                 EqNumber          float64   `validate:"eq=2.33"`
49                 EqMultiple        []string  `validate:"eq=7"`
50                 NeString          string    `validate:"ne="`
51                 NeNumber          float64   `validate:"ne=0.00"`
52                 NeMultiple        []string  `validate:"ne=0"`
53                 LtString          string    `validate:"lt=3"`
54                 LtNumber          float64   `validate:"lt=5.56"`
55                 LtMultiple        []string  `validate:"lt=2"`
56                 LtTime            time.Time `validate:"lt"`
57                 LteString         string    `validate:"lte=3"`
58                 LteNumber         float64   `validate:"lte=5.56"`
59                 LteMultiple       []string  `validate:"lte=2"`
60                 LteTime           time.Time `validate:"lte"`
61                 GtString          string    `validate:"gt=3"`
62                 GtNumber          float64   `validate:"gt=5.56"`
63                 GtMultiple        []string  `validate:"gt=2"`
64                 GtTime            time.Time `validate:"gt"`
65                 GteString         string    `validate:"gte=3"`
66                 GteNumber         float64   `validate:"gte=5.56"`
67                 GteMultiple       []string  `validate:"gte=2"`
68                 GteTime           time.Time `validate:"gte"`
69                 EqFieldString     string    `validate:"eqfield=MaxString"`
70                 EqCSFieldString   string    `validate:"eqcsfield=Inner.EqCSFieldString"`
71                 NeCSFieldString   string    `validate:"necsfield=Inner.NeCSFieldString"`
72                 GtCSFieldString   string    `validate:"gtcsfield=Inner.GtCSFieldString"`
73                 GteCSFieldString  string    `validate:"gtecsfield=Inner.GteCSFieldString"`
74                 LtCSFieldString   string    `validate:"ltcsfield=Inner.LtCSFieldString"`
75                 LteCSFieldString  string    `validate:"ltecsfield=Inner.LteCSFieldString"`
76                 NeFieldString     string    `validate:"nefield=EqFieldString"`
77                 GtFieldString     string    `validate:"gtfield=MaxString"`
78                 GteFieldString    string    `validate:"gtefield=MaxString"`
79                 LtFieldString     string    `validate:"ltfield=MaxString"`
80                 LteFieldString    string    `validate:"ltefield=MaxString"`
81                 AlphaString       string    `validate:"alpha"`
82                 AlphanumString    string    `validate:"alphanum"`
83                 NumericString     string    `validate:"numeric"`
84                 NumberString      string    `validate:"number"`
85                 HexadecimalString string    `validate:"hexadecimal"`
86                 HexColorString    string    `validate:"hexcolor"`
87                 RGBColorString    string    `validate:"rgb"`
88                 RGBAColorString   string    `validate:"rgba"`
89                 HSLColorString    string    `validate:"hsl"`
90                 HSLAColorString   string    `validate:"hsla"`
91                 Email             string    `validate:"email"`
92                 URL               string    `validate:"url"`
93                 URI               string    `validate:"uri"`
94                 Base64            string    `validate:"base64"`
95                 Contains          string    `validate:"contains=purpose"`
96                 ContainsAny       string    `validate:"containsany=!@#$"`
97                 Excludes          string    `validate:"excludes=text"`
98                 ExcludesAll       string    `validate:"excludesall=!@#$"`
99                 ExcludesRune      string    `validate:"excludesrune=☻"`
100                 ISBN              string    `validate:"isbn"`
101                 ISBN10            string    `validate:"isbn10"`
102                 ISBN13            string    `validate:"isbn13"`
103                 UUID              string    `validate:"uuid"`
104                 UUID3             string    `validate:"uuid3"`
105                 UUID4             string    `validate:"uuid4"`
106                 UUID5             string    `validate:"uuid5"`
107                 ASCII             string    `validate:"ascii"`
108                 PrintableASCII    string    `validate:"printascii"`
109                 MultiByte         string    `validate:"multibyte"`
110                 DataURI           string    `validate:"datauri"`
111                 Latitude          string    `validate:"latitude"`
112                 Longitude         string    `validate:"longitude"`
113                 SSN               string    `validate:"ssn"`
114                 IP                string    `validate:"ip"`
115                 IPv4              string    `validate:"ipv4"`
116                 IPv6              string    `validate:"ipv6"`
117                 CIDR              string    `validate:"cidr"`
118                 CIDRv4            string    `validate:"cidrv4"`
119                 CIDRv6            string    `validate:"cidrv6"`
120                 TCPAddr           string    `validate:"tcp_addr"`
121                 TCPAddrv4         string    `validate:"tcp4_addr"`
122                 TCPAddrv6         string    `validate:"tcp6_addr"`
123                 UDPAddr           string    `validate:"udp_addr"`
124                 UDPAddrv4         string    `validate:"udp4_addr"`
125                 UDPAddrv6         string    `validate:"udp6_addr"`
126                 IPAddr            string    `validate:"ip_addr"`
127                 IPAddrv4          string    `validate:"ip4_addr"`
128                 IPAddrv6          string    `validate:"ip6_addr"`
129                 UinxAddr          string    `validate:"unix_addr"` // can't fail from within Go's net package currently, but maybe in the future
130                 MAC               string    `validate:"mac"`
131                 IsColor           string    `validate:"iscolor"`
132         }
133
134         var test Test
135
136         test.Inner.EqCSFieldString = "1234"
137         test.Inner.GtCSFieldString = "1234"
138         test.Inner.GteCSFieldString = "1234"
139
140         test.MaxString = "1234"
141         test.MaxNumber = 2000
142         test.MaxMultiple = make([]string, 9)
143
144         test.LtString = "1234"
145         test.LtNumber = 6
146         test.LtMultiple = make([]string, 3)
147         test.LtTime = time.Now().Add(time.Hour * 24)
148
149         test.LteString = "1234"
150         test.LteNumber = 6
151         test.LteMultiple = make([]string, 3)
152         test.LteTime = time.Now().Add(time.Hour * 24)
153
154         test.LtFieldString = "12345"
155         test.LteFieldString = "12345"
156
157         test.LtCSFieldString = "1234"
158         test.LteCSFieldString = "1234"
159
160         test.AlphaString = "abc3"
161         test.AlphanumString = "abc3!"
162         test.NumericString = "12E.00"
163         test.NumberString = "12E"
164
165         test.Excludes = "this is some test text"
166         test.ExcludesAll = "This is Great!"
167         test.ExcludesRune = "Love it ☻"
168
169         test.ASCII = "カタカナ"
170         test.PrintableASCII = "カタカナ"
171
172         test.MultiByte = "1234feerf"
173
174         err = validate.Struct(test)
175         NotEqual(t, err, nil)
176
177         errs, ok := err.(validator.ValidationErrors)
178         Equal(t, ok, true)
179
180         tests := []struct {
181                 ns       string
182                 expected string
183         }{
184                 {
185                         ns:       "Test.IsColor",
186                         expected: "IsColor must be a valid color",
187                 },
188                 {
189                         ns:       "Test.MAC",
190                         expected: "MAC must contain a valid MAC address",
191                 },
192                 {
193                         ns:       "Test.IPAddr",
194                         expected: "IPAddr must be a resolvable IP address",
195                 },
196                 {
197                         ns:       "Test.IPAddrv4",
198                         expected: "IPAddrv4 must be a resolvable IPv4 address",
199                 },
200                 {
201                         ns:       "Test.IPAddrv6",
202                         expected: "IPAddrv6 must be a resolvable IPv6 address",
203                 },
204                 {
205                         ns:       "Test.UDPAddr",
206                         expected: "UDPAddr must be a valid UDP address",
207                 },
208                 {
209                         ns:       "Test.UDPAddrv4",
210                         expected: "UDPAddrv4 must be a valid IPv4 UDP address",
211                 },
212                 {
213                         ns:       "Test.UDPAddrv6",
214                         expected: "UDPAddrv6 must be a valid IPv6 UDP address",
215                 },
216                 {
217                         ns:       "Test.TCPAddr",
218                         expected: "TCPAddr must be a valid TCP address",
219                 },
220                 {
221                         ns:       "Test.TCPAddrv4",
222                         expected: "TCPAddrv4 must be a valid IPv4 TCP address",
223                 },
224                 {
225                         ns:       "Test.TCPAddrv6",
226                         expected: "TCPAddrv6 must be a valid IPv6 TCP address",
227                 },
228                 {
229                         ns:       "Test.CIDR",
230                         expected: "CIDR must contain a valid CIDR notation",
231                 },
232                 {
233                         ns:       "Test.CIDRv4",
234                         expected: "CIDRv4 must contain a valid CIDR notation for an IPv4 address",
235                 },
236                 {
237                         ns:       "Test.CIDRv6",
238                         expected: "CIDRv6 must contain a valid CIDR notation for an IPv6 address",
239                 },
240                 {
241                         ns:       "Test.SSN",
242                         expected: "SSN must be a valid SSN number",
243                 },
244                 {
245                         ns:       "Test.IP",
246                         expected: "IP must be a valid IP address",
247                 },
248                 {
249                         ns:       "Test.IPv4",
250                         expected: "IPv4 must be a valid IPv4 address",
251                 },
252                 {
253                         ns:       "Test.IPv6",
254                         expected: "IPv6 must be a valid IPv6 address",
255                 },
256                 {
257                         ns:       "Test.DataURI",
258                         expected: "DataURI must contain a valid Data URI",
259                 },
260                 {
261                         ns:       "Test.Latitude",
262                         expected: "Latitude must contain valid latitude coordinates",
263                 },
264                 {
265                         ns:       "Test.Longitude",
266                         expected: "Longitude must contain a valid longitude coordinates",
267                 },
268                 {
269                         ns:       "Test.MultiByte",
270                         expected: "MultiByte must contain multibyte characters",
271                 },
272                 {
273                         ns:       "Test.ASCII",
274                         expected: "ASCII must contain only ascii characters",
275                 },
276                 {
277                         ns:       "Test.PrintableASCII",
278                         expected: "PrintableASCII must contain only printable ascii characters",
279                 },
280                 {
281                         ns:       "Test.UUID",
282                         expected: "UUID must be a valid UUID",
283                 },
284                 {
285                         ns:       "Test.UUID3",
286                         expected: "UUID3 must be a valid version 3 UUID",
287                 },
288                 {
289                         ns:       "Test.UUID4",
290                         expected: "UUID4 must be a valid version 4 UUID",
291                 },
292                 {
293                         ns:       "Test.UUID5",
294                         expected: "UUID5 must be a valid version 5 UUID",
295                 },
296                 {
297                         ns:       "Test.ISBN",
298                         expected: "ISBN must be a valid ISBN number",
299                 },
300                 {
301                         ns:       "Test.ISBN10",
302                         expected: "ISBN10 must be a valid ISBN-10 number",
303                 },
304                 {
305                         ns:       "Test.ISBN13",
306                         expected: "ISBN13 must be a valid ISBN-13 number",
307                 },
308                 {
309                         ns:       "Test.Excludes",
310                         expected: "Excludes cannot contain the text 'text'",
311                 },
312                 {
313                         ns:       "Test.ExcludesAll",
314                         expected: "ExcludesAll cannot contain any of the following characters '!@#$'",
315                 },
316                 {
317                         ns:       "Test.ExcludesRune",
318                         expected: "ExcludesRune cannot contain the following '☻'",
319                 },
320                 {
321                         ns:       "Test.ContainsAny",
322                         expected: "ContainsAny must contain at least one of the following characters '!@#$'",
323                 },
324                 {
325                         ns:       "Test.Contains",
326                         expected: "Contains must contain the text 'purpose'",
327                 },
328                 {
329                         ns:       "Test.Base64",
330                         expected: "Base64 must be a valid Base64 string",
331                 },
332                 {
333                         ns:       "Test.Email",
334                         expected: "Email must be a valid email address",
335                 },
336                 {
337                         ns:       "Test.URL",
338                         expected: "URL must be a valid URL",
339                 },
340                 {
341                         ns:       "Test.URI",
342                         expected: "URI must be a valid URI",
343                 },
344                 {
345                         ns:       "Test.RGBColorString",
346                         expected: "RGBColorString must be a valid RGB color",
347                 },
348                 {
349                         ns:       "Test.RGBAColorString",
350                         expected: "RGBAColorString must be a valid RGBA color",
351                 },
352                 {
353                         ns:       "Test.HSLColorString",
354                         expected: "HSLColorString must be a valid HSL color",
355                 },
356                 {
357                         ns:       "Test.HSLAColorString",
358                         expected: "HSLAColorString must be a valid HSLA color",
359                 },
360                 {
361                         ns:       "Test.HexadecimalString",
362                         expected: "HexadecimalString must be a valid hexadecimal",
363                 },
364                 {
365                         ns:       "Test.HexColorString",
366                         expected: "HexColorString must be a valid HEX color",
367                 },
368                 {
369                         ns:       "Test.NumberString",
370                         expected: "NumberString must be a valid number",
371                 },
372                 {
373                         ns:       "Test.NumericString",
374                         expected: "NumericString must be a valid numeric value",
375                 },
376                 {
377                         ns:       "Test.AlphanumString",
378                         expected: "AlphanumString can only contain alphanumeric characters",
379                 },
380                 {
381                         ns:       "Test.AlphaString",
382                         expected: "AlphaString can only contain alphabetic characters",
383                 },
384                 {
385                         ns:       "Test.LtFieldString",
386                         expected: "LtFieldString must be less than MaxString",
387                 },
388                 {
389                         ns:       "Test.LteFieldString",
390                         expected: "LteFieldString must be less than or equal to MaxString",
391                 },
392                 {
393                         ns:       "Test.GtFieldString",
394                         expected: "GtFieldString must be greater than MaxString",
395                 },
396                 {
397                         ns:       "Test.GteFieldString",
398                         expected: "GteFieldString must be greater than or equal to MaxString",
399                 },
400                 {
401                         ns:       "Test.NeFieldString",
402                         expected: "NeFieldString cannot be equal to EqFieldString",
403                 },
404                 {
405                         ns:       "Test.LtCSFieldString",
406                         expected: "LtCSFieldString must be less than Inner.LtCSFieldString",
407                 },
408                 {
409                         ns:       "Test.LteCSFieldString",
410                         expected: "LteCSFieldString must be less than or equal to Inner.LteCSFieldString",
411                 },
412                 {
413                         ns:       "Test.GtCSFieldString",
414                         expected: "GtCSFieldString must be greater than Inner.GtCSFieldString",
415                 },
416                 {
417                         ns:       "Test.GteCSFieldString",
418                         expected: "GteCSFieldString must be greater than or equal to Inner.GteCSFieldString",
419                 },
420                 {
421                         ns:       "Test.NeCSFieldString",
422                         expected: "NeCSFieldString cannot be equal to Inner.NeCSFieldString",
423                 },
424                 {
425                         ns:       "Test.EqCSFieldString",
426                         expected: "EqCSFieldString must be equal to Inner.EqCSFieldString",
427                 },
428                 {
429                         ns:       "Test.EqFieldString",
430                         expected: "EqFieldString must be equal to MaxString",
431                 },
432                 {
433                         ns:       "Test.GteString",
434                         expected: "GteString must be at least 3 characters in length",
435                 },
436                 {
437                         ns:       "Test.GteNumber",
438                         expected: "GteNumber must be 5.56 or greater",
439                 },
440                 {
441                         ns:       "Test.GteMultiple",
442                         expected: "GteMultiple must contain at least 2 items",
443                 },
444                 {
445                         ns:       "Test.GteTime",
446                         expected: "GteTime must be greater than or equal to the current Date & Time",
447                 },
448                 {
449                         ns:       "Test.GtString",
450                         expected: "GtString must be greater than 3 characters in length",
451                 },
452                 {
453                         ns:       "Test.GtNumber",
454                         expected: "GtNumber must be greater than 5.56",
455                 },
456                 {
457                         ns:       "Test.GtMultiple",
458                         expected: "GtMultiple must contain more than 2 items",
459                 },
460                 {
461                         ns:       "Test.GtTime",
462                         expected: "GtTime must be greater than the current Date & Time",
463                 },
464                 {
465                         ns:       "Test.LteString",
466                         expected: "LteString must be at maximum 3 characters in length",
467                 },
468                 {
469                         ns:       "Test.LteNumber",
470                         expected: "LteNumber must be 5.56 or less",
471                 },
472                 {
473                         ns:       "Test.LteMultiple",
474                         expected: "LteMultiple must contain at maximum 2 items",
475                 },
476                 {
477                         ns:       "Test.LteTime",
478                         expected: "LteTime must be less than or equal to the current Date & Time",
479                 },
480                 {
481                         ns:       "Test.LtString",
482                         expected: "LtString must be less than 3 characters in length",
483                 },
484                 {
485                         ns:       "Test.LtNumber",
486                         expected: "LtNumber must be less than 5.56",
487                 },
488                 {
489                         ns:       "Test.LtMultiple",
490                         expected: "LtMultiple must contain less than 2 items",
491                 },
492                 {
493                         ns:       "Test.LtTime",
494                         expected: "LtTime must be less than the current Date & Time",
495                 },
496                 {
497                         ns:       "Test.NeString",
498                         expected: "NeString should not be equal to ",
499                 },
500                 {
501                         ns:       "Test.NeNumber",
502                         expected: "NeNumber should not be equal to 0.00",
503                 },
504                 {
505                         ns:       "Test.NeMultiple",
506                         expected: "NeMultiple should not be equal to 0",
507                 },
508                 {
509                         ns:       "Test.EqString",
510                         expected: "EqString is not equal to 3",
511                 },
512                 {
513                         ns:       "Test.EqNumber",
514                         expected: "EqNumber is not equal to 2.33",
515                 },
516                 {
517                         ns:       "Test.EqMultiple",
518                         expected: "EqMultiple is not equal to 7",
519                 },
520                 {
521                         ns:       "Test.MaxString",
522                         expected: "MaxString must be a maximum of 3 characters in length",
523                 },
524                 {
525                         ns:       "Test.MaxNumber",
526                         expected: "MaxNumber must be 1,113.00 or less",
527                 },
528                 {
529                         ns:       "Test.MaxMultiple",
530                         expected: "MaxMultiple must contain at maximum 7 items",
531                 },
532                 {
533                         ns:       "Test.MinString",
534                         expected: "MinString must be at least 1 character in length",
535                 },
536                 {
537                         ns:       "Test.MinNumber",
538                         expected: "MinNumber must be 1,113.00 or greater",
539                 },
540                 {
541                         ns:       "Test.MinMultiple",
542                         expected: "MinMultiple must contain at least 7 items",
543                 },
544                 {
545                         ns:       "Test.LenString",
546                         expected: "LenString must be 1 character in length",
547                 },
548                 {
549                         ns:       "Test.LenNumber",
550                         expected: "LenNumber must be equal to 1,113.00",
551                 },
552                 {
553                         ns:       "Test.LenMultiple",
554                         expected: "LenMultiple must contain 7 items",
555                 },
556                 {
557                         ns:       "Test.RequiredString",
558                         expected: "RequiredString is a required field",
559                 },
560                 {
561                         ns:       "Test.RequiredNumber",
562                         expected: "RequiredNumber is a required field",
563                 },
564                 {
565                         ns:       "Test.RequiredMultiple",
566                         expected: "RequiredMultiple is a required field",
567                 },
568         }
569
570         for _, tt := range tests {
571
572                 var fe validator.FieldError
573
574                 for _, e := range errs {
575                         if tt.ns == e.Namespace() {
576                                 fe = e
577                                 break
578                         }
579                 }
580
581                 NotEqual(t, fe, nil)
582                 Equal(t, tt.expected, fe.Translate(trans))
583         }
584
585 }