1
0

validation_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. // Copyright 2014 beego Author. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package validation
  15. import (
  16. "regexp"
  17. "testing"
  18. "time"
  19. )
  20. func TestRequired(t *testing.T) {
  21. valid := Validation{}
  22. if valid.Required(nil, "nil").Ok {
  23. t.Error("nil object should be false")
  24. }
  25. if !valid.Required(true, "bool").Ok {
  26. t.Error("Bool value should always return true")
  27. }
  28. if !valid.Required(false, "bool").Ok {
  29. t.Error("Bool value should always return true")
  30. }
  31. if valid.Required("", "string").Ok {
  32. t.Error("\"'\" string should be false")
  33. }
  34. if !valid.Required("astaxie", "string").Ok {
  35. t.Error("string should be true")
  36. }
  37. if valid.Required(0, "zero").Ok {
  38. t.Error("Integer should not be equal 0")
  39. }
  40. if !valid.Required(1, "int").Ok {
  41. t.Error("Integer except 0 should be true")
  42. }
  43. if !valid.Required(time.Now(), "time").Ok {
  44. t.Error("time should be true")
  45. }
  46. if valid.Required([]string{}, "emptySlice").Ok {
  47. t.Error("empty slice should be false")
  48. }
  49. if !valid.Required([]interface{}{"ok"}, "slice").Ok {
  50. t.Error("slice should be true")
  51. }
  52. }
  53. func TestMin(t *testing.T) {
  54. valid := Validation{}
  55. if valid.Min(-1, 0, "min0").Ok {
  56. t.Error("-1 is less than the minimum value of 0 should be false")
  57. }
  58. if !valid.Min(1, 0, "min0").Ok {
  59. t.Error("1 is greater or equal than the minimum value of 0 should be true")
  60. }
  61. }
  62. func TestMax(t *testing.T) {
  63. valid := Validation{}
  64. if valid.Max(1, 0, "max0").Ok {
  65. t.Error("1 is greater than the minimum value of 0 should be false")
  66. }
  67. if !valid.Max(-1, 0, "max0").Ok {
  68. t.Error("-1 is less or equal than the maximum value of 0 should be true")
  69. }
  70. }
  71. func TestRange(t *testing.T) {
  72. valid := Validation{}
  73. if valid.Range(-1, 0, 1, "range0_1").Ok {
  74. t.Error("-1 is between 0 and 1 should be false")
  75. }
  76. if !valid.Range(1, 0, 1, "range0_1").Ok {
  77. t.Error("1 is between 0 and 1 should be true")
  78. }
  79. }
  80. func TestMinSize(t *testing.T) {
  81. valid := Validation{}
  82. if valid.MinSize("", 1, "minSize1").Ok {
  83. t.Error("the length of \"\" is less than the minimum value of 1 should be false")
  84. }
  85. if !valid.MinSize("ok", 1, "minSize1").Ok {
  86. t.Error("the length of \"ok\" is greater or equal than the minimum value of 1 should be true")
  87. }
  88. if valid.MinSize([]string{}, 1, "minSize1").Ok {
  89. t.Error("the length of empty slice is less than the minimum value of 1 should be false")
  90. }
  91. if !valid.MinSize([]interface{}{"ok"}, 1, "minSize1").Ok {
  92. t.Error("the length of [\"ok\"] is greater or equal than the minimum value of 1 should be true")
  93. }
  94. }
  95. func TestMaxSize(t *testing.T) {
  96. valid := Validation{}
  97. if valid.MaxSize("ok", 1, "maxSize1").Ok {
  98. t.Error("the length of \"ok\" is greater than the maximum value of 1 should be false")
  99. }
  100. if !valid.MaxSize("", 1, "maxSize1").Ok {
  101. t.Error("the length of \"\" is less or equal than the maximum value of 1 should be true")
  102. }
  103. if valid.MaxSize([]interface{}{"ok", false}, 1, "maxSize1").Ok {
  104. t.Error("the length of [\"ok\", false] is greater than the maximum value of 1 should be false")
  105. }
  106. if !valid.MaxSize([]string{}, 1, "maxSize1").Ok {
  107. t.Error("the length of empty slice is less or equal than the maximum value of 1 should be true")
  108. }
  109. }
  110. func TestLength(t *testing.T) {
  111. valid := Validation{}
  112. if valid.Length("", 1, "length1").Ok {
  113. t.Error("the length of \"\" must equal 1 should be false")
  114. }
  115. if !valid.Length("1", 1, "length1").Ok {
  116. t.Error("the length of \"1\" must equal 1 should be true")
  117. }
  118. if valid.Length([]string{}, 1, "length1").Ok {
  119. t.Error("the length of empty slice must equal 1 should be false")
  120. }
  121. if !valid.Length([]interface{}{"ok"}, 1, "length1").Ok {
  122. t.Error("the length of [\"ok\"] must equal 1 should be true")
  123. }
  124. }
  125. func TestAlpha(t *testing.T) {
  126. valid := Validation{}
  127. if valid.Alpha("a,1-@ $", "alpha").Ok {
  128. t.Error("\"a,1-@ $\" are valid alpha characters should be false")
  129. }
  130. if !valid.Alpha("abCD", "alpha").Ok {
  131. t.Error("\"abCD\" are valid alpha characters should be true")
  132. }
  133. }
  134. func TestNumeric(t *testing.T) {
  135. valid := Validation{}
  136. if valid.Numeric("a,1-@ $", "numeric").Ok {
  137. t.Error("\"a,1-@ $\" are valid numeric characters should be false")
  138. }
  139. if !valid.Numeric("1234", "numeric").Ok {
  140. t.Error("\"1234\" are valid numeric characters should be true")
  141. }
  142. }
  143. func TestAlphaNumeric(t *testing.T) {
  144. valid := Validation{}
  145. if valid.AlphaNumeric("a,1-@ $", "alphaNumeric").Ok {
  146. t.Error("\"a,1-@ $\" are valid alpha or numeric characters should be false")
  147. }
  148. if !valid.AlphaNumeric("1234aB", "alphaNumeric").Ok {
  149. t.Error("\"1234aB\" are valid alpha or numeric characters should be true")
  150. }
  151. }
  152. func TestMatch(t *testing.T) {
  153. valid := Validation{}
  154. if valid.Match("suchuangji@gmail", regexp.MustCompile("^\\w+@\\w+\\.\\w+$"), "match").Ok {
  155. t.Error("\"suchuangji@gmail\" match \"^\\w+@\\w+\\.\\w+$\" should be false")
  156. }
  157. if !valid.Match("suchuangji@gmail.com", regexp.MustCompile("^\\w+@\\w+\\.\\w+$"), "match").Ok {
  158. t.Error("\"suchuangji@gmail\" match \"^\\w+@\\w+\\.\\w+$\" should be true")
  159. }
  160. }
  161. func TestNoMatch(t *testing.T) {
  162. valid := Validation{}
  163. if valid.NoMatch("123@gmail", regexp.MustCompile("[^\\w\\d]"), "nomatch").Ok {
  164. t.Error("\"123@gmail\" not match \"[^\\w\\d]\" should be false")
  165. }
  166. if !valid.NoMatch("123gmail", regexp.MustCompile("[^\\w\\d]"), "match").Ok {
  167. t.Error("\"123@gmail\" not match \"[^\\w\\d@]\" should be true")
  168. }
  169. }
  170. func TestAlphaDash(t *testing.T) {
  171. valid := Validation{}
  172. if valid.AlphaDash("a,1-@ $", "alphaDash").Ok {
  173. t.Error("\"a,1-@ $\" are valid alpha or numeric or dash(-_) characters should be false")
  174. }
  175. if !valid.AlphaDash("1234aB-_", "alphaDash").Ok {
  176. t.Error("\"1234aB\" are valid alpha or numeric or dash(-_) characters should be true")
  177. }
  178. }
  179. func TestEmail(t *testing.T) {
  180. valid := Validation{}
  181. if valid.Email("not@a email", "email").Ok {
  182. t.Error("\"not@a email\" is a valid email address should be false")
  183. }
  184. if !valid.Email("suchuangji@gmail.com", "email").Ok {
  185. t.Error("\"suchuangji@gmail.com\" is a valid email address should be true")
  186. }
  187. }
  188. func TestIP(t *testing.T) {
  189. valid := Validation{}
  190. if valid.IP("11.255.255.256", "IP").Ok {
  191. t.Error("\"11.255.255.256\" is a valid ip address should be false")
  192. }
  193. if !valid.IP("01.11.11.11", "IP").Ok {
  194. t.Error("\"suchuangji@gmail.com\" is a valid ip address should be true")
  195. }
  196. }
  197. func TestBase64(t *testing.T) {
  198. valid := Validation{}
  199. if valid.Base64("suchuangji@gmail.com", "base64").Ok {
  200. t.Error("\"suchuangji@gmail.com\" are a valid base64 characters should be false")
  201. }
  202. if !valid.Base64("c3VjaHVhbmdqaUBnbWFpbC5jb20=", "base64").Ok {
  203. t.Error("\"c3VjaHVhbmdqaUBnbWFpbC5jb20=\" are a valid base64 characters should be true")
  204. }
  205. }
  206. func TestMobile(t *testing.T) {
  207. valid := Validation{}
  208. if valid.Mobile("19800008888", "mobile").Ok {
  209. t.Error("\"19800008888\" is a valid mobile phone number should be false")
  210. }
  211. if !valid.Mobile("18800008888", "mobile").Ok {
  212. t.Error("\"18800008888\" is a valid mobile phone number should be true")
  213. }
  214. if !valid.Mobile("18000008888", "mobile").Ok {
  215. t.Error("\"18000008888\" is a valid mobile phone number should be true")
  216. }
  217. if !valid.Mobile("8618300008888", "mobile").Ok {
  218. t.Error("\"8618300008888\" is a valid mobile phone number should be true")
  219. }
  220. if !valid.Mobile("+8614700008888", "mobile").Ok {
  221. t.Error("\"+8614700008888\" is a valid mobile phone number should be true")
  222. }
  223. }
  224. func TestTel(t *testing.T) {
  225. valid := Validation{}
  226. if valid.Tel("222-00008888", "telephone").Ok {
  227. t.Error("\"222-00008888\" is a valid telephone number should be false")
  228. }
  229. if !valid.Tel("022-70008888", "telephone").Ok {
  230. t.Error("\"022-70008888\" is a valid telephone number should be true")
  231. }
  232. if !valid.Tel("02270008888", "telephone").Ok {
  233. t.Error("\"02270008888\" is a valid telephone number should be true")
  234. }
  235. if !valid.Tel("70008888", "telephone").Ok {
  236. t.Error("\"70008888\" is a valid telephone number should be true")
  237. }
  238. }
  239. func TestPhone(t *testing.T) {
  240. valid := Validation{}
  241. if valid.Phone("222-00008888", "phone").Ok {
  242. t.Error("\"222-00008888\" is a valid phone number should be false")
  243. }
  244. if !valid.Mobile("+8614700008888", "phone").Ok {
  245. t.Error("\"+8614700008888\" is a valid phone number should be true")
  246. }
  247. if !valid.Tel("02270008888", "phone").Ok {
  248. t.Error("\"02270008888\" is a valid phone number should be true")
  249. }
  250. }
  251. func TestZipCode(t *testing.T) {
  252. valid := Validation{}
  253. if valid.ZipCode("", "zipcode").Ok {
  254. t.Error("\"00008888\" is a valid zipcode should be false")
  255. }
  256. if !valid.ZipCode("536000", "zipcode").Ok {
  257. t.Error("\"536000\" is a valid zipcode should be true")
  258. }
  259. }
  260. func TestValid(t *testing.T) {
  261. type user struct {
  262. ID int
  263. Name string `valid:"Required;Match(/^(test)?\\w*@(/test/);com$/)"`
  264. Age int `valid:"Required;Range(1, 140)"`
  265. }
  266. valid := Validation{}
  267. u := user{Name: "test@/test/;com", Age: 40}
  268. b, err := valid.Valid(u)
  269. if err != nil {
  270. t.Fatal(err)
  271. }
  272. if !b {
  273. t.Error("validation should be passed")
  274. }
  275. uptr := &user{Name: "test", Age: 40}
  276. valid.Clear()
  277. b, err = valid.Valid(uptr)
  278. if err != nil {
  279. t.Fatal(err)
  280. }
  281. if b {
  282. t.Error("validation should not be passed")
  283. }
  284. if len(valid.Errors) != 1 {
  285. t.Fatalf("valid errors len should be 1 but got %d", len(valid.Errors))
  286. }
  287. if valid.Errors[0].Key != "Name.Match" {
  288. t.Errorf("Message key should be `Name.Match` but got %s", valid.Errors[0].Key)
  289. }
  290. u = user{Name: "test@/test/;com", Age: 180}
  291. valid.Clear()
  292. b, err = valid.Valid(u)
  293. if err != nil {
  294. t.Fatal(err)
  295. }
  296. if b {
  297. t.Error("validation should not be passed")
  298. }
  299. if len(valid.Errors) != 1 {
  300. t.Fatalf("valid errors len should be 1 but got %d", len(valid.Errors))
  301. }
  302. if valid.Errors[0].Key != "Age.Range" {
  303. t.Errorf("Message key should be `Name.Match` but got %s", valid.Errors[0].Key)
  304. }
  305. }
  306. func TestRecursiveValid(t *testing.T) {
  307. type User struct {
  308. ID int
  309. Name string `valid:"Required;Match(/^(test)?\\w*@(/test/);com$/)"`
  310. Age int `valid:"Required;Range(1, 140)"`
  311. }
  312. type AnonymouseUser struct {
  313. ID2 int
  314. Name2 string `valid:"Required;Match(/^(test)?\\w*@(/test/);com$/)"`
  315. Age2 int `valid:"Required;Range(1, 140)"`
  316. }
  317. type Account struct {
  318. Password string `valid:"Required"`
  319. U User
  320. AnonymouseUser
  321. }
  322. valid := Validation{}
  323. u := Account{Password: "abc123_", U: User{}}
  324. b, err := valid.RecursiveValid(u)
  325. if err != nil {
  326. t.Fatal(err)
  327. }
  328. if b {
  329. t.Error("validation should not be passed")
  330. }
  331. }