1 package es
2
3 import (
4 "testing"
5 "time"
6
7 . "github.com/go-playground/assert/v2"
8 spanish "github.com/go-playground/locales/es"
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 spa := spanish.New()
16 uni := ut.New(spa, spa)
17 trans, _ := uni.GetTranslator("es")
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 RequiredIf string `validate:"required_if=Inner.RequiredIf abcd"`
39 LenString string `validate:"len=1"`
40 LenNumber float64 `validate:"len=1113.00"`
41 LenMultiple []string `validate:"len=7"`
42 MinString string `validate:"min=1"`
43 MinNumber float64 `validate:"min=1113.00"`
44 MinMultiple []string `validate:"min=7"`
45 MaxString string `validate:"max=3"`
46 MaxNumber float64 `validate:"max=1113.00"`
47 MaxMultiple []string `validate:"max=7"`
48 EqString string `validate:"eq=3"`
49 EqNumber float64 `validate:"eq=2.33"`
50 EqMultiple []string `validate:"eq=7"`
51 NeString string `validate:"ne="`
52 NeNumber float64 `validate:"ne=0.00"`
53 NeMultiple []string `validate:"ne=0"`
54 LtString string `validate:"lt=3"`
55 LtNumber float64 `validate:"lt=5.56"`
56 LtMultiple []string `validate:"lt=2"`
57 LtTime time.Time `validate:"lt"`
58 LteString string `validate:"lte=3"`
59 LteNumber float64 `validate:"lte=5.56"`
60 LteMultiple []string `validate:"lte=2"`
61 LteTime time.Time `validate:"lte"`
62 GtString string `validate:"gt=3"`
63 GtNumber float64 `validate:"gt=5.56"`
64 GtMultiple []string `validate:"gt=2"`
65 GtTime time.Time `validate:"gt"`
66 GteString string `validate:"gte=3"`
67 GteNumber float64 `validate:"gte=5.56"`
68 GteMultiple []string `validate:"gte=2"`
69 GteTime time.Time `validate:"gte"`
70 EqFieldString string `validate:"eqfield=MaxString"`
71 EqCSFieldString string `validate:"eqcsfield=Inner.EqCSFieldString"`
72 NeCSFieldString string `validate:"necsfield=Inner.NeCSFieldString"`
73 GtCSFieldString string `validate:"gtcsfield=Inner.GtCSFieldString"`
74 GteCSFieldString string `validate:"gtecsfield=Inner.GteCSFieldString"`
75 LtCSFieldString string `validate:"ltcsfield=Inner.LtCSFieldString"`
76 LteCSFieldString string `validate:"ltecsfield=Inner.LteCSFieldString"`
77 NeFieldString string `validate:"nefield=EqFieldString"`
78 GtFieldString string `validate:"gtfield=MaxString"`
79 GteFieldString string `validate:"gtefield=MaxString"`
80 LtFieldString string `validate:"ltfield=MaxString"`
81 LteFieldString string `validate:"ltefield=MaxString"`
82 AlphaString string `validate:"alpha"`
83 AlphanumString string `validate:"alphanum"`
84 NumericString string `validate:"numeric"`
85 NumberString string `validate:"number"`
86 HexadecimalString string `validate:"hexadecimal"`
87 HexColorString string `validate:"hexcolor"`
88 RGBColorString string `validate:"rgb"`
89 RGBAColorString string `validate:"rgba"`
90 HSLColorString string `validate:"hsl"`
91 HSLAColorString string `validate:"hsla"`
92 Email string `validate:"email"`
93 URL string `validate:"url"`
94 URI string `validate:"uri"`
95 Base64 string `validate:"base64"`
96 Contains string `validate:"contains=purpose"`
97 ContainsAny string `validate:"containsany=!@#$"`
98 Excludes string `validate:"excludes=text"`
99 ExcludesAll string `validate:"excludesall=!@#$"`
100 ExcludesRune string `validate:"excludesrune=☻"`
101 ISBN string `validate:"isbn"`
102 ISBN10 string `validate:"isbn10"`
103 ISBN13 string `validate:"isbn13"`
104 ISSN string `validate:"issn"`
105 UUID string `validate:"uuid"`
106 UUID3 string `validate:"uuid3"`
107 UUID4 string `validate:"uuid4"`
108 UUID5 string `validate:"uuid5"`
109 ULID string `validate:"ulid"`
110 ASCII string `validate:"ascii"`
111 PrintableASCII string `validate:"printascii"`
112 MultiByte string `validate:"multibyte"`
113 DataURI string `validate:"datauri"`
114 Latitude string `validate:"latitude"`
115 Longitude string `validate:"longitude"`
116 SSN string `validate:"ssn"`
117 IP string `validate:"ip"`
118 IPv4 string `validate:"ipv4"`
119 IPv6 string `validate:"ipv6"`
120 CIDR string `validate:"cidr"`
121 CIDRv4 string `validate:"cidrv4"`
122 CIDRv6 string `validate:"cidrv6"`
123 TCPAddr string `validate:"tcp_addr"`
124 TCPAddrv4 string `validate:"tcp4_addr"`
125 TCPAddrv6 string `validate:"tcp6_addr"`
126 UDPAddr string `validate:"udp_addr"`
127 UDPAddrv4 string `validate:"udp4_addr"`
128 UDPAddrv6 string `validate:"udp6_addr"`
129 IPAddr string `validate:"ip_addr"`
130 IPAddrv4 string `validate:"ip4_addr"`
131 IPAddrv6 string `validate:"ip6_addr"`
132 UinxAddr string `validate:"unix_addr"`
133 MAC string `validate:"mac"`
134 IsColor string `validate:"iscolor"`
135 StrPtrMinLen *string `validate:"min=10"`
136 StrPtrMaxLen *string `validate:"max=1"`
137 StrPtrLen *string `validate:"len=2"`
138 StrPtrLt *string `validate:"lt=1"`
139 StrPtrLte *string `validate:"lte=1"`
140 StrPtrGt *string `validate:"gt=10"`
141 StrPtrGte *string `validate:"gte=10"`
142 OneOfString string `validate:"oneof=red green"`
143 OneOfInt int `validate:"oneof=5 63"`
144 UniqueSlice []string `validate:"unique"`
145 UniqueArray [3]string `validate:"unique"`
146 UniqueMap map[string]string `validate:"unique"`
147 Image string `validate:"image"`
148 }
149
150 var test Test
151
152 test.Inner.EqCSFieldString = "1234"
153 test.Inner.GtCSFieldString = "1234"
154 test.Inner.GteCSFieldString = "1234"
155
156 test.MaxString = "1234"
157 test.MaxNumber = 2000
158 test.MaxMultiple = make([]string, 9)
159
160 test.LtString = "1234"
161 test.LtNumber = 6
162 test.LtMultiple = make([]string, 3)
163 test.LtTime = time.Now().Add(time.Hour * 24)
164
165 test.LteString = "1234"
166 test.LteNumber = 6
167 test.LteMultiple = make([]string, 3)
168 test.LteTime = time.Now().Add(time.Hour * 24)
169
170 test.LtFieldString = "12345"
171 test.LteFieldString = "12345"
172
173 test.LtCSFieldString = "1234"
174 test.LteCSFieldString = "1234"
175
176 test.AlphaString = "abc3"
177 test.AlphanumString = "abc3!"
178 test.NumericString = "12E.00"
179 test.NumberString = "12E"
180
181 test.Excludes = "this is some test text"
182 test.ExcludesAll = "This is Great!"
183 test.ExcludesRune = "Love it ☻"
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 test.UniqueSlice = []string{"1234", "1234"}
195 test.UniqueMap = map[string]string{"key1": "1234", "key2": "1234"}
196
197 err = validate.Struct(test)
198 NotEqual(t, err, nil)
199
200 errs, ok := err.(validator.ValidationErrors)
201 Equal(t, ok, true)
202
203 tests := []struct {
204 ns string
205 expected string
206 }{
207 {
208 ns: "Test.IsColor",
209 expected: "IsColor debe ser un color válido",
210 },
211 {
212 ns: "Test.MAC",
213 expected: "MAC debe contener una dirección MAC válida",
214 },
215 {
216 ns: "Test.IPAddr",
217 expected: "IPAddr debe ser una dirección IP resoluble",
218 },
219 {
220 ns: "Test.IPAddrv4",
221 expected: "IPAddrv4 debe ser una dirección IPv4 resoluble",
222 },
223 {
224 ns: "Test.IPAddrv6",
225 expected: "IPAddrv6 debe ser una dirección IPv6 resoluble",
226 },
227 {
228 ns: "Test.UDPAddr",
229 expected: "UDPAddr debe ser una dirección UDP válida",
230 },
231 {
232 ns: "Test.UDPAddrv4",
233 expected: "UDPAddrv4 debe ser una dirección IPv4 UDP válida",
234 },
235 {
236 ns: "Test.UDPAddrv6",
237 expected: "UDPAddrv6 debe ser una dirección IPv6 UDP válida",
238 },
239 {
240 ns: "Test.TCPAddr",
241 expected: "TCPAddr debe ser una dirección TCP válida",
242 },
243 {
244 ns: "Test.TCPAddrv4",
245 expected: "TCPAddrv4 debe ser una dirección IPv4 TCP válida",
246 },
247 {
248 ns: "Test.TCPAddrv6",
249 expected: "TCPAddrv6 debe ser una dirección IPv6 TCP válida",
250 },
251 {
252 ns: "Test.CIDR",
253 expected: "CIDR debe contener una anotación válida del CIDR",
254 },
255 {
256 ns: "Test.CIDRv4",
257 expected: "CIDRv4 debe contener una notación CIDR válida para una dirección IPv4",
258 },
259 {
260 ns: "Test.CIDRv6",
261 expected: "CIDRv6 debe contener una notación CIDR válida para una dirección IPv6",
262 },
263 {
264 ns: "Test.SSN",
265 expected: "SSN debe ser un número válido de SSN",
266 },
267 {
268 ns: "Test.IP",
269 expected: "IP debe ser una dirección IP válida",
270 },
271 {
272 ns: "Test.IPv4",
273 expected: "IPv4 debe ser una dirección IPv4 válida",
274 },
275 {
276 ns: "Test.IPv6",
277 expected: "IPv6 debe ser una dirección IPv6 válida",
278 },
279 {
280 ns: "Test.DataURI",
281 expected: "DataURI debe contener un URI de datos válido",
282 },
283 {
284 ns: "Test.Latitude",
285 expected: "Latitude debe contener coordenadas de latitud válidas",
286 },
287 {
288 ns: "Test.Longitude",
289 expected: "Longitude debe contener unas coordenadas de longitud válidas",
290 },
291 {
292 ns: "Test.MultiByte",
293 expected: "MultiByte debe contener caracteres multibyte",
294 },
295 {
296 ns: "Test.ASCII",
297 expected: "ASCII debe contener sólo caracteres ascii",
298 },
299 {
300 ns: "Test.PrintableASCII",
301 expected: "PrintableASCII debe contener sólo caracteres ASCII imprimibles",
302 },
303 {
304 ns: "Test.UUID",
305 expected: "UUID debe ser un UUID válido",
306 },
307 {
308 ns: "Test.UUID3",
309 expected: "UUID3 debe ser una versión válida 3 UUID",
310 },
311 {
312 ns: "Test.UUID4",
313 expected: "UUID4 debe ser una versión válida 4 UUID",
314 },
315 {
316 ns: "Test.UUID5",
317 expected: "UUID5 debe ser una versión válida 5 UUID",
318 },
319 {
320 ns: "Test.ULID",
321 expected: "ULID debe ser un ULID válido",
322 },
323 {
324 ns: "Test.ISBN",
325 expected: "ISBN debe ser un número ISBN válido",
326 },
327 {
328 ns: "Test.ISBN10",
329 expected: "ISBN10 debe ser un número ISBN-10 válido",
330 },
331 {
332 ns: "Test.ISBN13",
333 expected: "ISBN13 debe ser un número ISBN-13 válido",
334 },
335 {
336 ns: "Test.ISSN",
337 expected: "ISSN debe ser un número ISSN válido",
338 },
339 {
340 ns: "Test.Excludes",
341 expected: "Excludes no puede contener el texto 'text'",
342 },
343 {
344 ns: "Test.ExcludesAll",
345 expected: "ExcludesAll no puede contener ninguno de los siguientes caracteres '!@#$'",
346 },
347 {
348 ns: "Test.ExcludesRune",
349 expected: "ExcludesRune no puede contener lo siguiente '☻'",
350 },
351 {
352 ns: "Test.ContainsAny",
353 expected: "ContainsAny debe contener al menos uno de los siguientes caracteres '!@#$'",
354 },
355 {
356 ns: "Test.Contains",
357 expected: "Contains debe contener el texto 'purpose'",
358 },
359 {
360 ns: "Test.Base64",
361 expected: "Base64 debe ser una cadena de Base64 válida",
362 },
363 {
364 ns: "Test.Email",
365 expected: "Email debe ser una dirección de correo electrónico válida",
366 },
367 {
368 ns: "Test.URL",
369 expected: "URL debe ser un URL válido",
370 },
371 {
372 ns: "Test.URI",
373 expected: "URI debe ser una URI válida",
374 },
375 {
376 ns: "Test.RGBColorString",
377 expected: "RGBColorString debe ser un color RGB válido",
378 },
379 {
380 ns: "Test.RGBAColorString",
381 expected: "RGBAColorString debe ser un color RGBA válido",
382 },
383 {
384 ns: "Test.HSLColorString",
385 expected: "HSLColorString debe ser un color HSL válido",
386 },
387 {
388 ns: "Test.HSLAColorString",
389 expected: "HSLAColorString debe ser un color HSL válido",
390 },
391 {
392 ns: "Test.HexadecimalString",
393 expected: "HexadecimalString debe ser un hexadecimal válido",
394 },
395 {
396 ns: "Test.HexColorString",
397 expected: "HexColorString debe ser un color HEX válido",
398 },
399 {
400 ns: "Test.NumberString",
401 expected: "NumberString debe ser un número válido",
402 },
403 {
404 ns: "Test.NumericString",
405 expected: "NumericString debe ser un valor numérico válido",
406 },
407 {
408 ns: "Test.AlphanumString",
409 expected: "AlphanumString sólo puede contener caracteres alfanuméricos",
410 },
411 {
412 ns: "Test.AlphaString",
413 expected: "AlphaString sólo puede contener caracteres alfabéticos",
414 },
415 {
416 ns: "Test.LtFieldString",
417 expected: "LtFieldString debe ser menor que MaxString",
418 },
419 {
420 ns: "Test.LteFieldString",
421 expected: "LteFieldString debe ser menor o igual a MaxString",
422 },
423 {
424 ns: "Test.GtFieldString",
425 expected: "GtFieldString debe ser mayor que MaxString",
426 },
427 {
428 ns: "Test.GteFieldString",
429 expected: "GteFieldString debe ser mayor o igual a MaxString",
430 },
431 {
432 ns: "Test.NeFieldString",
433 expected: "NeFieldString no puede ser igual a EqFieldString",
434 },
435 {
436 ns: "Test.LtCSFieldString",
437 expected: "LtCSFieldString debe ser menor que Inner.LtCSFieldString",
438 },
439 {
440 ns: "Test.LteCSFieldString",
441 expected: "LteCSFieldString debe ser menor o igual a Inner.LteCSFieldString",
442 },
443 {
444 ns: "Test.GtCSFieldString",
445 expected: "GtCSFieldString debe ser mayor que Inner.GtCSFieldString",
446 },
447 {
448 ns: "Test.GteCSFieldString",
449 expected: "GteCSFieldString debe ser mayor o igual a Inner.GteCSFieldString",
450 },
451 {
452 ns: "Test.NeCSFieldString",
453 expected: "NeCSFieldString no puede ser igual a Inner.NeCSFieldString",
454 },
455 {
456 ns: "Test.EqCSFieldString",
457 expected: "EqCSFieldString debe ser igual a Inner.EqCSFieldString",
458 },
459 {
460 ns: "Test.EqFieldString",
461 expected: "EqFieldString debe ser igual a MaxString",
462 },
463 {
464 ns: "Test.GteString",
465 expected: "GteString debe tener al menos 3 caracteres de longitud",
466 },
467 {
468 ns: "Test.GteNumber",
469 expected: "GteNumber debe ser 5,56 o mayor",
470 },
471 {
472 ns: "Test.GteMultiple",
473 expected: "GteMultiple debe contener al menos 2 elementos",
474 },
475 {
476 ns: "Test.GteTime",
477 expected: "GteTime debe ser después o durante la fecha y hora actuales",
478 },
479 {
480 ns: "Test.GtString",
481 expected: "GtString debe ser mayor que 3 caracteres en longitud",
482 },
483 {
484 ns: "Test.GtNumber",
485 expected: "GtNumber debe ser mayor que 5,56",
486 },
487 {
488 ns: "Test.GtMultiple",
489 expected: "GtMultiple debe contener más de 2 elementos",
490 },
491 {
492 ns: "Test.GtTime",
493 expected: "GtTime debe ser después de la fecha y hora actual",
494 },
495 {
496 ns: "Test.LteString",
497 expected: "LteString debe tener un máximo de 3 caracteres de longitud",
498 },
499 {
500 ns: "Test.LteNumber",
501 expected: "LteNumber debe ser 5,56 o menos",
502 },
503 {
504 ns: "Test.LteMultiple",
505 expected: "LteMultiple debe contener como máximo 2 elementos",
506 },
507 {
508 ns: "Test.LteTime",
509 expected: "LteTime debe ser antes o durante la fecha y hora actual",
510 },
511 {
512 ns: "Test.LtString",
513 expected: "LtString debe tener menos de 3 caracteres de longitud",
514 },
515 {
516 ns: "Test.LtNumber",
517 expected: "LtNumber debe ser menos de 5,56",
518 },
519 {
520 ns: "Test.LtMultiple",
521 expected: "LtMultiple debe contener menos de 2 elementos",
522 },
523 {
524 ns: "Test.LtTime",
525 expected: "LtTime debe ser antes de la fecha y hora actual",
526 },
527 {
528 ns: "Test.NeString",
529 expected: "NeString no debería ser igual a ",
530 },
531 {
532 ns: "Test.NeNumber",
533 expected: "NeNumber no debería ser igual a 0.00",
534 },
535 {
536 ns: "Test.NeMultiple",
537 expected: "NeMultiple no debería ser igual a 0",
538 },
539 {
540 ns: "Test.EqString",
541 expected: "EqString no es igual a 3",
542 },
543 {
544 ns: "Test.EqNumber",
545 expected: "EqNumber no es igual a 2.33",
546 },
547 {
548 ns: "Test.EqMultiple",
549 expected: "EqMultiple no es igual a 7",
550 },
551 {
552 ns: "Test.MaxString",
553 expected: "MaxString debe tener un máximo de 3 caracteres de longitud",
554 },
555 {
556 ns: "Test.MaxNumber",
557 expected: "MaxNumber debe ser 1.113,00 o menos",
558 },
559 {
560 ns: "Test.MaxMultiple",
561 expected: "MaxMultiple debe contener como máximo 7 elementos",
562 },
563 {
564 ns: "Test.MinString",
565 expected: "MinString debe tener al menos 1 carácter de longitud",
566 },
567 {
568 ns: "Test.MinNumber",
569 expected: "MinNumber debe ser 1.113,00 o más",
570 },
571 {
572 ns: "Test.MinMultiple",
573 expected: "MinMultiple debe contener al menos 7 elementos",
574 },
575 {
576 ns: "Test.LenString",
577 expected: "LenString debe tener 1 carácter de longitud",
578 },
579 {
580 ns: "Test.LenNumber",
581 expected: "LenNumber debe ser igual a 1.113,00",
582 },
583 {
584 ns: "Test.LenMultiple",
585 expected: "LenMultiple debe contener 7 elementos",
586 },
587 {
588 ns: "Test.RequiredString",
589 expected: "RequiredString es un campo requerido",
590 },
591 {
592 ns: "Test.RequiredNumber",
593 expected: "RequiredNumber es un campo requerido",
594 },
595 {
596 ns: "Test.RequiredMultiple",
597 expected: "RequiredMultiple es un campo requerido",
598 },
599 {
600 ns: "Test.StrPtrMinLen",
601 expected: "StrPtrMinLen debe tener al menos 10 caracteres de longitud",
602 },
603 {
604 ns: "Test.StrPtrMaxLen",
605 expected: "StrPtrMaxLen debe tener un máximo de 1 carácter de longitud",
606 },
607 {
608 ns: "Test.StrPtrLen",
609 expected: "StrPtrLen debe tener 2 caracteres de longitud",
610 },
611 {
612 ns: "Test.StrPtrLt",
613 expected: "StrPtrLt debe tener menos de 1 carácter de longitud",
614 },
615 {
616 ns: "Test.StrPtrLte",
617 expected: "StrPtrLte debe tener un máximo de 1 carácter de longitud",
618 },
619 {
620 ns: "Test.StrPtrGt",
621 expected: "StrPtrGt debe ser mayor que 10 caracteres en longitud",
622 },
623 {
624 ns: "Test.StrPtrGte",
625 expected: "StrPtrGte debe tener al menos 10 caracteres de longitud",
626 },
627 {
628 ns: "Test.OneOfString",
629 expected: "OneOfString debe ser uno de [red green]",
630 },
631 {
632 ns: "Test.OneOfInt",
633 expected: "OneOfInt debe ser uno de [5 63]",
634 },
635 {
636 ns: "Test.UniqueSlice",
637 expected: "UniqueSlice debe contener valores únicos",
638 },
639 {
640 ns: "Test.UniqueArray",
641 expected: "UniqueArray debe contener valores únicos",
642 },
643 {
644 ns: "Test.UniqueMap",
645 expected: "UniqueMap debe contener valores únicos",
646 },
647 {
648 ns: "Test.Image",
649 expected: "Image debe ser una imagen válida",
650 },
651 }
652
653 for _, tt := range tests {
654
655 var fe validator.FieldError
656
657 for _, e := range errs {
658 if tt.ns == e.Namespace() {
659 fe = e
660 break
661 }
662 }
663
664 NotEqual(t, fe, nil)
665 Equal(t, tt.expected, fe.Translate(trans))
666 }
667
668 }
669
View as plain text