1 package pt_BR
2
3 import (
4 "testing"
5 "time"
6
7 . "github.com/go-playground/assert/v2"
8 brazilian_portuguese "github.com/go-playground/locales/pt_BR"
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 ptbr := brazilian_portuguese.New()
16 uni := ut.New(ptbr, ptbr)
17 trans, _ := uni.GetTranslator("pt_BR")
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 ISSN string `validate:"issn"`
104 UUID string `validate:"uuid"`
105 UUID3 string `validate:"uuid3"`
106 UUID4 string `validate:"uuid4"`
107 UUID5 string `validate:"uuid5"`
108 ULID string `validate:"ulid"`
109 ASCII string `validate:"ascii"`
110 PrintableASCII string `validate:"printascii"`
111 MultiByte string `validate:"multibyte"`
112 DataURI string `validate:"datauri"`
113 Latitude string `validate:"latitude"`
114 Longitude string `validate:"longitude"`
115 SSN string `validate:"ssn"`
116 IP string `validate:"ip"`
117 IPv4 string `validate:"ipv4"`
118 IPv6 string `validate:"ipv6"`
119 CIDR string `validate:"cidr"`
120 CIDRv4 string `validate:"cidrv4"`
121 CIDRv6 string `validate:"cidrv6"`
122 TCPAddr string `validate:"tcp_addr"`
123 TCPAddrv4 string `validate:"tcp4_addr"`
124 TCPAddrv6 string `validate:"tcp6_addr"`
125 UDPAddr string `validate:"udp_addr"`
126 UDPAddrv4 string `validate:"udp4_addr"`
127 UDPAddrv6 string `validate:"udp6_addr"`
128 IPAddr string `validate:"ip_addr"`
129 IPAddrv4 string `validate:"ip4_addr"`
130 IPAddrv6 string `validate:"ip6_addr"`
131 UinxAddr string `validate:"unix_addr"`
132 MAC string `validate:"mac"`
133 IsColor string `validate:"iscolor"`
134 StrPtrMinLen *string `validate:"min=10"`
135 StrPtrMaxLen *string `validate:"max=1"`
136 StrPtrLen *string `validate:"len=2"`
137 StrPtrLt *string `validate:"lt=1"`
138 StrPtrLte *string `validate:"lte=1"`
139 StrPtrGt *string `validate:"gt=10"`
140 StrPtrGte *string `validate:"gte=10"`
141 OneOfString string `validate:"oneof=red green"`
142 OneOfInt int `validate:"oneof=5 63"`
143 BooleanString string `validate:"boolean"`
144 Image string `validate:"image"`
145 CveString string `validate:"cve"`
146 }
147
148 var test Test
149
150 test.Inner.EqCSFieldString = "1234"
151 test.Inner.GtCSFieldString = "1234"
152 test.Inner.GteCSFieldString = "1234"
153
154 test.MaxString = "1234"
155 test.MaxNumber = 2000
156 test.MaxMultiple = make([]string, 9)
157
158 test.LtString = "1234"
159 test.LtNumber = 6
160 test.LtMultiple = make([]string, 3)
161 test.LtTime = time.Now().Add(time.Hour * 24)
162
163 test.LteString = "1234"
164 test.LteNumber = 6
165 test.LteMultiple = make([]string, 3)
166 test.LteTime = time.Now().Add(time.Hour * 24)
167
168 test.LtFieldString = "12345"
169 test.LteFieldString = "12345"
170
171 test.LtCSFieldString = "1234"
172 test.LteCSFieldString = "1234"
173
174 test.AlphaString = "abc3"
175 test.AlphanumString = "abc3!"
176 test.NumericString = "12E.00"
177 test.NumberString = "12E"
178 test.BooleanString = "A"
179 test.CveString = "A"
180
181 test.Excludes = "este é um texto de teste"
182 test.ExcludesAll = "Isso é Ótimo!"
183 test.ExcludesRune = "Amo isso ☻"
184
185 test.ASCII = "カタカナ"
186 test.PrintableASCII = "カタカナ"
187
188 test.MultiByte = "1234feerf"
189
190 s := "toolong"
191 test.StrPtrMaxLen = &s
192 test.StrPtrLen = &s
193
194 err = validate.Struct(test)
195 NotEqual(t, err, nil)
196
197 errs, ok := err.(validator.ValidationErrors)
198 Equal(t, ok, true)
199
200 tests := []struct {
201 ns string
202 expected string
203 }{
204 {
205 ns: "Test.IsColor",
206 expected: "IsColor deve ser uma cor válida",
207 },
208 {
209 ns: "Test.MAC",
210 expected: "MAC deve conter um endereço MAC válido",
211 },
212 {
213 ns: "Test.IPAddr",
214 expected: "IPAddr deve ser um endereço IP resolvível",
215 },
216 {
217 ns: "Test.IPAddrv4",
218 expected: "IPAddrv4 deve ser um endereço IPv4 resolvível",
219 },
220 {
221 ns: "Test.IPAddrv6",
222 expected: "IPAddrv6 deve ser um endereço IPv6 resolvível",
223 },
224 {
225 ns: "Test.UDPAddr",
226 expected: "UDPAddr deve ser um endereço UDP válido",
227 },
228 {
229 ns: "Test.UDPAddrv4",
230 expected: "UDPAddrv4 deve ser um endereço IPv4 UDP válido",
231 },
232 {
233 ns: "Test.UDPAddrv6",
234 expected: "UDPAddrv6 deve ser um endereço IPv6 UDP válido",
235 },
236 {
237 ns: "Test.TCPAddr",
238 expected: "TCPAddr deve ser um endereço TCP válido",
239 },
240 {
241 ns: "Test.TCPAddrv4",
242 expected: "TCPAddrv4 deve ser um endereço IPv4 TCP válido",
243 },
244 {
245 ns: "Test.TCPAddrv6",
246 expected: "TCPAddrv6 deve ser um endereço IPv6 TCP válido",
247 },
248 {
249 ns: "Test.CIDR",
250 expected: "CIDR deve conter uma notação CIDR válida",
251 },
252 {
253 ns: "Test.CIDRv4",
254 expected: "CIDRv4 deve conter uma notação CIDR válida para um endereço IPv4",
255 },
256 {
257 ns: "Test.CIDRv6",
258 expected: "CIDRv6 deve conter uma notação CIDR válida para um endereço IPv6",
259 },
260 {
261 ns: "Test.SSN",
262 expected: "SSN deve ser um número SSN válido",
263 },
264 {
265 ns: "Test.IP",
266 expected: "IP deve ser um endereço de IP válido",
267 },
268 {
269 ns: "Test.IPv4",
270 expected: "IPv4 deve ser um endereço IPv4 válido",
271 },
272 {
273 ns: "Test.IPv6",
274 expected: "IPv6 deve ser um endereço IPv6 válido",
275 },
276 {
277 ns: "Test.DataURI",
278 expected: "DataURI deve conter um URI data válido",
279 },
280 {
281 ns: "Test.Latitude",
282 expected: "Latitude deve conter uma coordenada de latitude válida",
283 },
284 {
285 ns: "Test.Longitude",
286 expected: "Longitude deve conter uma coordenada de longitude válida",
287 },
288 {
289 ns: "Test.MultiByte",
290 expected: "MultiByte deve conter caracteres multibyte",
291 },
292 {
293 ns: "Test.ASCII",
294 expected: "ASCII deve conter apenas caracteres ascii",
295 },
296 {
297 ns: "Test.PrintableASCII",
298 expected: "PrintableASCII deve conter apenas caracteres ascii imprimíveis",
299 },
300 {
301 ns: "Test.UUID",
302 expected: "UUID deve ser um UUID válido",
303 },
304 {
305 ns: "Test.UUID3",
306 expected: "UUID3 deve ser um UUID versão 3 válido",
307 },
308 {
309 ns: "Test.UUID4",
310 expected: "UUID4 deve ser um UUID versão 4 válido",
311 },
312 {
313 ns: "Test.UUID5",
314 expected: "UUID5 deve ser um UUID versão 5 válido",
315 },
316 {
317 ns: "Test.ULID",
318 expected: "ULID deve ser uma ULID válida",
319 },
320 {
321 ns: "Test.ISBN",
322 expected: "ISBN deve ser um número ISBN válido",
323 },
324 {
325 ns: "Test.ISBN10",
326 expected: "ISBN10 deve ser um número ISBN-10 válido",
327 },
328 {
329 ns: "Test.ISBN13",
330 expected: "ISBN13 deve ser um número ISBN-13 válido",
331 },
332 {
333 ns: "Test.ISSN",
334 expected: "ISSN deve ser um número ISSN válido",
335 },
336 {
337 ns: "Test.Excludes",
338 expected: "Excludes não deve conter o texto 'text'",
339 },
340 {
341 ns: "Test.ExcludesAll",
342 expected: "ExcludesAll não deve conter nenhum dos caracteres '!@#$'",
343 },
344 {
345 ns: "Test.ExcludesRune",
346 expected: "ExcludesRune não deve conter '☻'",
347 },
348 {
349 ns: "Test.ContainsAny",
350 expected: "ContainsAny deve conter pelo menos um dos caracteres '!@#$'",
351 },
352 {
353 ns: "Test.Contains",
354 expected: "Contains deve conter o texto 'purpose'",
355 },
356 {
357 ns: "Test.Base64",
358 expected: "Base64 deve ser uma string Base64 válida",
359 },
360 {
361 ns: "Test.Email",
362 expected: "Email deve ser um endereço de e-mail válido",
363 },
364 {
365 ns: "Test.URL",
366 expected: "URL deve ser uma URL válida",
367 },
368 {
369 ns: "Test.URI",
370 expected: "URI deve ser uma URI válida",
371 },
372 {
373 ns: "Test.RGBColorString",
374 expected: "RGBColorString deve ser uma cor RGB válida",
375 },
376 {
377 ns: "Test.RGBAColorString",
378 expected: "RGBAColorString deve ser uma cor RGBA válida",
379 },
380 {
381 ns: "Test.HSLColorString",
382 expected: "HSLColorString deve ser uma cor HSL válida",
383 },
384 {
385 ns: "Test.HSLAColorString",
386 expected: "HSLAColorString deve ser uma cor HSLA válida",
387 },
388 {
389 ns: "Test.HexadecimalString",
390 expected: "HexadecimalString deve ser um hexadecimal válido",
391 },
392 {
393 ns: "Test.HexColorString",
394 expected: "HexColorString deve ser uma cor HEX válida",
395 },
396 {
397 ns: "Test.NumberString",
398 expected: "NumberString deve ser um número válido",
399 },
400 {
401 ns: "Test.NumericString",
402 expected: "NumericString deve ser um valor numérico válido",
403 },
404 {
405 ns: "Test.AlphanumString",
406 expected: "AlphanumString deve conter caracteres alfanuméricos",
407 },
408 {
409 ns: "Test.AlphaString",
410 expected: "AlphaString deve conter caracteres alfabéticos",
411 },
412 {
413 ns: "Test.LtFieldString",
414 expected: "LtFieldString deve ser menor que MaxString",
415 },
416 {
417 ns: "Test.LteFieldString",
418 expected: "LteFieldString deve ser menor ou igual a MaxString",
419 },
420 {
421 ns: "Test.GtFieldString",
422 expected: "GtFieldString deve ser maior do que MaxString",
423 },
424 {
425 ns: "Test.GteFieldString",
426 expected: "GteFieldString deve ser maior ou igual a MaxString",
427 },
428 {
429 ns: "Test.NeFieldString",
430 expected: "NeFieldString não deve ser igual a EqFieldString",
431 },
432 {
433 ns: "Test.LtCSFieldString",
434 expected: "LtCSFieldString deve ser menor que Inner.LtCSFieldString",
435 },
436 {
437 ns: "Test.LteCSFieldString",
438 expected: "LteCSFieldString deve ser menor ou igual a Inner.LteCSFieldString",
439 },
440 {
441 ns: "Test.GtCSFieldString",
442 expected: "GtCSFieldString deve ser maior do que Inner.GtCSFieldString",
443 },
444 {
445 ns: "Test.GteCSFieldString",
446 expected: "GteCSFieldString deve ser maior ou igual a Inner.GteCSFieldString",
447 },
448 {
449 ns: "Test.NeCSFieldString",
450 expected: "NeCSFieldString não deve ser igual a Inner.NeCSFieldString",
451 },
452 {
453 ns: "Test.EqCSFieldString",
454 expected: "EqCSFieldString deve ser igual a Inner.EqCSFieldString",
455 },
456 {
457 ns: "Test.EqFieldString",
458 expected: "EqFieldString deve ser igual a MaxString",
459 },
460 {
461 ns: "Test.GteString",
462 expected: "GteString deve ter pelo menos 3 caracteres",
463 },
464 {
465 ns: "Test.GteNumber",
466 expected: "GteNumber deve ser 5,56 ou superior",
467 },
468 {
469 ns: "Test.GteMultiple",
470 expected: "GteMultiple deve conter pelo menos 2 itens",
471 },
472 {
473 ns: "Test.GteTime",
474 expected: "GteTime deve ser maior ou igual à Data e Hora atual",
475 },
476 {
477 ns: "Test.GtString",
478 expected: "GtString deve ter mais de 3 caracteres",
479 },
480 {
481 ns: "Test.GtNumber",
482 expected: "GtNumber deve ser maior do que 5,56",
483 },
484 {
485 ns: "Test.GtMultiple",
486 expected: "GtMultiple deve conter mais de 2 itens",
487 },
488 {
489 ns: "Test.GtTime",
490 expected: "GtTime deve ser maior que a Data e Hora atual",
491 },
492 {
493 ns: "Test.LteString",
494 expected: "LteString deve ter no máximo 3 caracteres",
495 },
496 {
497 ns: "Test.LteNumber",
498 expected: "LteNumber deve ser 5,56 ou menor",
499 },
500 {
501 ns: "Test.LteMultiple",
502 expected: "LteMultiple deve conter no máximo 2 itens",
503 },
504 {
505 ns: "Test.LteTime",
506 expected: "LteTime deve ser menor ou igual à Data e Hora atual",
507 },
508 {
509 ns: "Test.LtString",
510 expected: "LtString deve ter menos de 3 caracteres",
511 },
512 {
513 ns: "Test.LtNumber",
514 expected: "LtNumber deve ser menor que 5,56",
515 },
516 {
517 ns: "Test.LtMultiple",
518 expected: "LtMultiple deve conter menos de 2 itens",
519 },
520 {
521 ns: "Test.LtTime",
522 expected: "LtTime deve ser inferior à Data e Hora atual",
523 },
524 {
525 ns: "Test.NeString",
526 expected: "NeString não deve ser igual a ",
527 },
528 {
529 ns: "Test.NeNumber",
530 expected: "NeNumber não deve ser igual a 0.00",
531 },
532 {
533 ns: "Test.NeMultiple",
534 expected: "NeMultiple não deve ser igual a 0",
535 },
536 {
537 ns: "Test.EqString",
538 expected: "EqString não é igual a 3",
539 },
540 {
541 ns: "Test.EqNumber",
542 expected: "EqNumber não é igual a 2.33",
543 },
544 {
545 ns: "Test.EqMultiple",
546 expected: "EqMultiple não é igual a 7",
547 },
548 {
549 ns: "Test.MaxString",
550 expected: "MaxString deve ter no máximo 3 caracteres",
551 },
552 {
553 ns: "Test.MaxNumber",
554 expected: "MaxNumber deve ser 1.113,00 ou menor",
555 },
556 {
557 ns: "Test.MaxMultiple",
558 expected: "MaxMultiple deve conter no máximo 7 itens",
559 },
560 {
561 ns: "Test.MinString",
562 expected: "MinString deve ter pelo menos 1 caractere",
563 },
564 {
565 ns: "Test.MinNumber",
566 expected: "MinNumber deve ser 1.113,00 ou superior",
567 },
568 {
569 ns: "Test.MinMultiple",
570 expected: "MinMultiple deve conter pelo menos 7 itens",
571 },
572 {
573 ns: "Test.LenString",
574 expected: "LenString deve ter 1 caractere",
575 },
576 {
577 ns: "Test.LenNumber",
578 expected: "LenNumber deve ser igual a 1.113,00",
579 },
580 {
581 ns: "Test.LenMultiple",
582 expected: "LenMultiple deve conter 7 itens",
583 },
584 {
585 ns: "Test.RequiredString",
586 expected: "RequiredString é um campo obrigatório",
587 },
588 {
589 ns: "Test.RequiredNumber",
590 expected: "RequiredNumber é um campo obrigatório",
591 },
592 {
593 ns: "Test.RequiredMultiple",
594 expected: "RequiredMultiple é um campo obrigatório",
595 },
596 {
597 ns: "Test.StrPtrMinLen",
598 expected: "StrPtrMinLen deve ter pelo menos 10 caracteres",
599 },
600 {
601 ns: "Test.StrPtrMaxLen",
602 expected: "StrPtrMaxLen deve ter no máximo 1 caractere",
603 },
604 {
605 ns: "Test.StrPtrLen",
606 expected: "StrPtrLen deve ter 2 caracteres",
607 },
608 {
609 ns: "Test.StrPtrLt",
610 expected: "StrPtrLt deve ter menos de 1 caractere",
611 },
612 {
613 ns: "Test.StrPtrLte",
614 expected: "StrPtrLte deve ter no máximo 1 caractere",
615 },
616 {
617 ns: "Test.StrPtrGt",
618 expected: "StrPtrGt deve ter mais de 10 caracteres",
619 },
620 {
621 ns: "Test.StrPtrGte",
622 expected: "StrPtrGte deve ter pelo menos 10 caracteres",
623 },
624 {
625 ns: "Test.OneOfString",
626 expected: "OneOfString deve ser um de [red green]",
627 },
628 {
629 ns: "Test.OneOfInt",
630 expected: "OneOfInt deve ser um de [5 63]",
631 },
632 {
633 ns: "Test.BooleanString",
634 expected: "BooleanString deve ser um valor booleano válido",
635 },
636 {
637 ns: "Test.Image",
638 expected: "Image deve ser uma imagen válido",
639 },
640 {
641 ns: "Test.CveString",
642 expected: "CveString deve ser um identificador cve válido",
643 },
644 }
645
646 for _, tt := range tests {
647
648 var fe validator.FieldError
649
650 for _, e := range errs {
651 if tt.ns == e.Namespace() {
652 fe = e
653 break
654 }
655 }
656
657 NotEqual(t, fe, nil)
658 Equal(t, tt.expected, fe.Translate(trans))
659 }
660
661 }
662
View as plain text