zh_tw_test.go 17 KB

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