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