1 package id
2
3 import (
4 "testing"
5 "time"
6
7 . "github.com/go-playground/assert/v2"
8 indonesia "github.com/go-playground/locales/id"
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 idn := indonesia.New()
16 uni := ut.New(idn, idn)
17 trans, _ := uni.GetTranslator("id")
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=tujuan"`
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=merah hijau"`
142 OneOfInt int `validate:"oneof=5 63"`
143 Image string `validate:"image"`
144 }
145
146 var test Test
147
148 test.Inner.EqCSFieldString = "1234"
149 test.Inner.GtCSFieldString = "1234"
150 test.Inner.GteCSFieldString = "1234"
151
152 test.MaxString = "1234"
153 test.MaxNumber = 2000
154 test.MaxMultiple = make([]string, 9)
155
156 test.LtString = "1234"
157 test.LtNumber = 6
158 test.LtMultiple = make([]string, 3)
159 test.LtTime = time.Now().Add(time.Hour * 24)
160
161 test.LteString = "1234"
162 test.LteNumber = 6
163 test.LteMultiple = make([]string, 3)
164 test.LteTime = time.Now().Add(time.Hour * 24)
165
166 test.LtFieldString = "12345"
167 test.LteFieldString = "12345"
168
169 test.LtCSFieldString = "1234"
170 test.LteCSFieldString = "1234"
171
172 test.AlphaString = "abc3"
173 test.AlphanumString = "abc3!"
174 test.NumericString = "12E.00"
175 test.NumberString = "12E"
176
177 test.Excludes = "this is some test text"
178 test.ExcludesAll = "This is Great!"
179 test.ExcludesRune = "Love it ☻"
180
181 test.ASCII = "カタカナ"
182 test.PrintableASCII = "カタカナ"
183
184 test.MultiByte = "1234feerf"
185
186 s := "toolong"
187 test.StrPtrMaxLen = &s
188 test.StrPtrLen = &s
189
190 err = validate.Struct(test)
191 NotEqual(t, err, nil)
192
193 errs, ok := err.(validator.ValidationErrors)
194 Equal(t, ok, true)
195
196 tests := []struct {
197 ns string
198 expected string
199 }{
200 {
201 ns: "Test.IsColor",
202 expected: "IsColor harus berupa warna yang valid",
203 },
204 {
205 ns: "Test.MAC",
206 expected: "MAC harus berisi alamat MAC yang valid",
207 },
208 {
209 ns: "Test.IPAddr",
210 expected: "IPAddr harus berupa alamat IP yang dapat dipecahkan",
211 },
212 {
213 ns: "Test.IPAddrv4",
214 expected: "IPAddrv4 harus berupa alamat IPv4 yang dapat diatasi",
215 },
216 {
217 ns: "Test.IPAddrv6",
218 expected: "IPAddrv6 harus berupa alamat IPv6 yang dapat diatasi",
219 },
220 {
221 ns: "Test.UDPAddr",
222 expected: "UDPAddr harus berupa alamat UDP yang valid",
223 },
224 {
225 ns: "Test.UDPAddrv4",
226 expected: "UDPAddrv4 harus berupa alamat IPv4 UDP yang valid",
227 },
228 {
229 ns: "Test.UDPAddrv6",
230 expected: "UDPAddrv6 harus berupa alamat IPv6 UDP yang valid",
231 },
232 {
233 ns: "Test.TCPAddr",
234 expected: "TCPAddr harus berupa alamat TCP yang valid",
235 },
236 {
237 ns: "Test.TCPAddrv4",
238 expected: "TCPAddrv4 harus berupa alamat TCP IPv4 yang valid",
239 },
240 {
241 ns: "Test.TCPAddrv6",
242 expected: "TCPAddrv6 harus berupa alamat TCP IPv6 yang valid",
243 },
244 {
245 ns: "Test.CIDR",
246 expected: "CIDR harus berisi notasi CIDR yang valid",
247 },
248 {
249 ns: "Test.CIDRv4",
250 expected: "CIDRv4 harus berisi notasi CIDR yang valid untuk alamat IPv4",
251 },
252 {
253 ns: "Test.CIDRv6",
254 expected: "CIDRv6 harus berisi notasi CIDR yang valid untuk alamat IPv6",
255 },
256 {
257 ns: "Test.SSN",
258 expected: "SSN harus berupa nomor SSN yang valid",
259 },
260 {
261 ns: "Test.IP",
262 expected: "IP harus berupa alamat IP yang valid",
263 },
264 {
265 ns: "Test.IPv4",
266 expected: "IPv4 harus berupa alamat IPv4 yang valid",
267 },
268 {
269 ns: "Test.IPv6",
270 expected: "IPv6 harus berupa alamat IPv6 yang valid",
271 },
272 {
273 ns: "Test.DataURI",
274 expected: "DataURI harus berisi URI Data yang valid",
275 },
276 {
277 ns: "Test.Latitude",
278 expected: "Latitude harus berisi koordinat lintang yang valid",
279 },
280 {
281 ns: "Test.Longitude",
282 expected: "Longitude harus berisi koordinat bujur yang valid",
283 },
284 {
285 ns: "Test.MultiByte",
286 expected: "MultiByte harus berisi karakter multibyte",
287 },
288 {
289 ns: "Test.ASCII",
290 expected: "ASCII hanya boleh berisi karakter ascii",
291 },
292 {
293 ns: "Test.PrintableASCII",
294 expected: "PrintableASCII hanya boleh berisi karakter ascii yang dapat dicetak",
295 },
296 {
297 ns: "Test.UUID",
298 expected: "UUID harus berupa UUID yang valid",
299 },
300 {
301 ns: "Test.UUID3",
302 expected: "UUID3 harus berupa UUID versi 3 yang valid",
303 },
304 {
305 ns: "Test.UUID4",
306 expected: "UUID4 harus berupa UUID versi 4 yang valid",
307 },
308 {
309 ns: "Test.UUID5",
310 expected: "UUID5 harus berupa UUID versi 5 yang valid",
311 },
312 {
313 ns: "Test.ULID",
314 expected: "ULID harus berupa ULID yang valid",
315 },
316 {
317 ns: "Test.ISBN",
318 expected: "ISBN harus berupa nomor ISBN yang valid",
319 },
320 {
321 ns: "Test.ISBN10",
322 expected: "ISBN10 harus berupa nomor ISBN-10 yang valid",
323 },
324 {
325 ns: "Test.ISBN13",
326 expected: "ISBN13 harus berupa nomor ISBN-13 yang valid",
327 },
328 {
329 ns: "Test.ISSN",
330 expected: "ISSN harus berupa nomor ISSN yang valid",
331 },
332 {
333 ns: "Test.Excludes",
334 expected: "Excludes tidak boleh berisi teks 'text'",
335 },
336 {
337 ns: "Test.ExcludesAll",
338 expected: "ExcludesAll tidak boleh berisi salah satu karakter berikut '!@#$'",
339 },
340 {
341 ns: "Test.ExcludesRune",
342 expected: "ExcludesRune tidak boleh berisi '☻'",
343 },
344 {
345 ns: "Test.ContainsAny",
346 expected: "ContainsAny harus berisi setidaknya salah satu karakter berikut '!@#$'",
347 },
348 {
349 ns: "Test.Contains",
350 expected: "Contains harus berisi teks 'tujuan'",
351 },
352 {
353 ns: "Test.Base64",
354 expected: "Base64 harus berupa string Base64 yang valid",
355 },
356 {
357 ns: "Test.Email",
358 expected: "Email harus berupa alamat email yang valid",
359 },
360 {
361 ns: "Test.URL",
362 expected: "URL harus berupa URL yang valid",
363 },
364 {
365 ns: "Test.URI",
366 expected: "URI harus berupa URI yang valid",
367 },
368 {
369 ns: "Test.RGBColorString",
370 expected: "RGBColorString harus berupa warna RGB yang valid",
371 },
372 {
373 ns: "Test.RGBAColorString",
374 expected: "RGBAColorString harus berupa warna RGBA yang valid",
375 },
376 {
377 ns: "Test.HSLColorString",
378 expected: "HSLColorString harus berupa warna HSL yang valid",
379 },
380 {
381 ns: "Test.HSLAColorString",
382 expected: "HSLAColorString harus berupa warna HSLA yang valid",
383 },
384 {
385 ns: "Test.HexadecimalString",
386 expected: "HexadecimalString harus berupa heksadesimal yang valid",
387 },
388 {
389 ns: "Test.HexColorString",
390 expected: "HexColorString harus berupa warna HEX yang valid",
391 },
392 {
393 ns: "Test.NumberString",
394 expected: "NumberString harus berupa angka yang valid",
395 },
396 {
397 ns: "Test.NumericString",
398 expected: "NumericString harus berupa nilai numerik yang valid",
399 },
400 {
401 ns: "Test.AlphanumString",
402 expected: "AlphanumString hanya dapat berisi karakter alfanumerik",
403 },
404 {
405 ns: "Test.AlphaString",
406 expected: "AlphaString hanya dapat berisi karakter abjad",
407 },
408 {
409 ns: "Test.LtFieldString",
410 expected: "LtFieldString harus kurang dari MaxString",
411 },
412 {
413 ns: "Test.LteFieldString",
414 expected: "LteFieldString harus kurang dari atau sama dengan MaxString",
415 },
416 {
417 ns: "Test.GtFieldString",
418 expected: "GtFieldString harus lebih besar dari MaxString",
419 },
420 {
421 ns: "Test.GteFieldString",
422 expected: "GteFieldString harus lebih besar dari atau sama dengan MaxString",
423 },
424 {
425 ns: "Test.NeFieldString",
426 expected: "NeFieldString tidak sama dengan EqFieldString",
427 },
428 {
429 ns: "Test.LtCSFieldString",
430 expected: "LtCSFieldString harus kurang dari Inner.LtCSFieldString",
431 },
432 {
433 ns: "Test.LteCSFieldString",
434 expected: "LteCSFieldString harus kurang dari atau sama dengan Inner.LteCSFieldString",
435 },
436 {
437 ns: "Test.GtCSFieldString",
438 expected: "GtCSFieldString harus lebih besar dari Inner.GtCSFieldString",
439 },
440 {
441 ns: "Test.GteCSFieldString",
442 expected: "GteCSFieldString harus lebih besar dari atau sama dengan Inner.GteCSFieldString",
443 },
444 {
445 ns: "Test.NeCSFieldString",
446 expected: "NeCSFieldString tidak sama dengan Inner.NeCSFieldString",
447 },
448 {
449 ns: "Test.EqCSFieldString",
450 expected: "EqCSFieldString harus sama dengan Inner.EqCSFieldString",
451 },
452 {
453 ns: "Test.EqFieldString",
454 expected: "EqFieldString harus sama dengan MaxString",
455 },
456 {
457 ns: "Test.GteString",
458 expected: "panjang minimal GteString adalah 3 karakter",
459 },
460 {
461 ns: "Test.GteNumber",
462 expected: "GteNumber harus 5,56 atau lebih besar",
463 },
464 {
465 ns: "Test.GteMultiple",
466 expected: "GteMultiple harus berisi setidaknya 2 item",
467 },
468 {
469 ns: "Test.GteTime",
470 expected: "GteTime harus lebih besar dari atau sama dengan tanggal & waktu saat ini",
471 },
472 {
473 ns: "Test.GtString",
474 expected: "panjang GtString harus lebih dari 3 karakter",
475 },
476 {
477 ns: "Test.GtNumber",
478 expected: "GtNumber harus lebih besar dari 5,56",
479 },
480 {
481 ns: "Test.GtMultiple",
482 expected: "GtMultiple harus berisi lebih dari 2 item",
483 },
484 {
485 ns: "Test.GtTime",
486 expected: "GtTime harus lebih besar dari tanggal & waktu saat ini",
487 },
488 {
489 ns: "Test.LteString",
490 expected: "panjang maksimal LteString adalah 3 karakter",
491 },
492 {
493 ns: "Test.LteNumber",
494 expected: "LteNumber harus 5,56 atau kurang",
495 },
496 {
497 ns: "Test.LteMultiple",
498 expected: "LteMultiple harus berisi maksimal 2 item",
499 },
500 {
501 ns: "Test.LteTime",
502 expected: "LteTime harus kurang dari atau sama dengan tanggal & waktu saat ini",
503 },
504 {
505 ns: "Test.LtString",
506 expected: "panjang LtString harus kurang dari 3 karakter",
507 },
508 {
509 ns: "Test.LtNumber",
510 expected: "LtNumber harus kurang dari 5,56",
511 },
512 {
513 ns: "Test.LtMultiple",
514 expected: "LtMultiple harus berisi kurang dari 2 item",
515 },
516 {
517 ns: "Test.LtTime",
518 expected: "LtTime harus kurang dari tanggal & waktu saat ini",
519 },
520 {
521 ns: "Test.NeString",
522 expected: "NeString tidak sama dengan ",
523 },
524 {
525 ns: "Test.NeNumber",
526 expected: "NeNumber tidak sama dengan 0.00",
527 },
528 {
529 ns: "Test.NeMultiple",
530 expected: "NeMultiple tidak sama dengan 0",
531 },
532 {
533 ns: "Test.EqString",
534 expected: "EqString tidak sama dengan 3",
535 },
536 {
537 ns: "Test.EqNumber",
538 expected: "EqNumber tidak sama dengan 2.33",
539 },
540 {
541 ns: "Test.EqMultiple",
542 expected: "EqMultiple tidak sama dengan 7",
543 },
544 {
545 ns: "Test.MaxString",
546 expected: "panjang maksimal MaxString adalah 3 karakter",
547 },
548 {
549 ns: "Test.MaxNumber",
550 expected: "MaxNumber harus 1.113,00 atau kurang",
551 },
552 {
553 ns: "Test.MaxMultiple",
554 expected: "MaxMultiple harus berisi maksimal 7 item",
555 },
556 {
557 ns: "Test.MinString",
558 expected: "panjang minimal MinString adalah 1 karakter",
559 },
560 {
561 ns: "Test.MinNumber",
562 expected: "MinNumber harus 1.113,00 atau lebih besar",
563 },
564 {
565 ns: "Test.MinMultiple",
566 expected: "panjang minimal MinMultiple adalah 7 item",
567 },
568 {
569 ns: "Test.LenString",
570 expected: "panjang LenString harus 1 karakter",
571 },
572 {
573 ns: "Test.LenNumber",
574 expected: "LenNumber harus sama dengan 1.113,00",
575 },
576 {
577 ns: "Test.LenMultiple",
578 expected: "LenMultiple harus berisi 7 item",
579 },
580 {
581 ns: "Test.RequiredString",
582 expected: "RequiredString wajib diisi",
583 },
584 {
585 ns: "Test.RequiredNumber",
586 expected: "RequiredNumber wajib diisi",
587 },
588 {
589 ns: "Test.RequiredMultiple",
590 expected: "RequiredMultiple wajib diisi",
591 },
592 {
593 ns: "Test.StrPtrMinLen",
594 expected: "panjang minimal StrPtrMinLen adalah 10 karakter",
595 },
596 {
597 ns: "Test.StrPtrMaxLen",
598 expected: "panjang maksimal StrPtrMaxLen adalah 1 karakter",
599 },
600 {
601 ns: "Test.StrPtrLen",
602 expected: "panjang StrPtrLen harus 2 karakter",
603 },
604 {
605 ns: "Test.StrPtrLt",
606 expected: "panjang StrPtrLt harus kurang dari 1 karakter",
607 },
608 {
609 ns: "Test.StrPtrLte",
610 expected: "panjang maksimal StrPtrLte adalah 1 karakter",
611 },
612 {
613 ns: "Test.StrPtrGt",
614 expected: "panjang StrPtrGt harus lebih dari 10 karakter",
615 },
616 {
617 ns: "Test.StrPtrGte",
618 expected: "panjang minimal StrPtrGte adalah 10 karakter",
619 },
620 {
621 ns: "Test.OneOfString",
622 expected: "OneOfString harus berupa salah satu dari [merah hijau]",
623 },
624 {
625 ns: "Test.OneOfInt",
626 expected: "OneOfInt harus berupa salah satu dari [5 63]",
627 },
628 {
629 ns: "Test.Image",
630 expected: "Image harus berupa gambar yang valid",
631 },
632 }
633
634 for _, tt := range tests {
635
636 var fe validator.FieldError
637
638 for _, e := range errs {
639 if tt.ns == e.Namespace() {
640 fe = e
641 break
642 }
643 }
644
645 NotEqual(t, fe, nil)
646 Equal(t, tt.expected, fe.Translate(trans))
647 }
648
649 }
650
View as plain text