A fast pacakge for validation
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
2.6 KiB

3 years ago
3 years ago
3 years ago
3 years ago
  1. package validate
  2. import (
  3. "reflect"
  4. "testing"
  5. )
  6. func TestAddRule(t *testing.T) {
  7. type s struct {
  8. A string `validate:"custom"`
  9. }
  10. AddRule(`custom`, nil, Kinds{
  11. reflect.String: func(rv reflect.Value, _ interface{}) bool {
  12. return rv.String() == `custom`
  13. },
  14. })
  15. var pass = s{`custom`}
  16. var fail = s{`somethingelse`}
  17. check(t, pass, 0)
  18. check(t, fail, 1)
  19. }
  20. func TestRuleRequired(t *testing.T) {
  21. type s struct {
  22. A *string `validate:"required"`
  23. B []int `validate:"required"`
  24. C []int `validate:"required"`
  25. D string `validate:"required"`
  26. E int `validate:"required"`
  27. F uint `validate:"required"`
  28. G float64 `validate:"required"`
  29. H interface{} `validate:"required"`
  30. I map[int]int `validate:"required"`
  31. }
  32. str := ``
  33. var pass = s{&str, make([]int, 1), make([]int, 1), ` `, -1, 1, 0.01, ``, map[int]int{0: 1}}
  34. var fail = s{nil, nil, make([]int, 0), ``, 0, 0, 0.000, nil, nil}
  35. check(t, pass, 0)
  36. check(t, fail, 9)
  37. }
  38. func TestRulePrefixSuffix(t *testing.T) {
  39. type s struct {
  40. A string `validate:"prefix=#"`
  41. B string `validate:"suffix=@"`
  42. }
  43. var pass = s{`#a`, `a@`}
  44. var fail = s{`a#`, `@a`}
  45. check(t, pass, 0)
  46. check(t, fail, 2)
  47. }
  48. func TestRuleContains(t *testing.T) {
  49. type s struct {
  50. A string `validate:"contains=%"`
  51. }
  52. var pass1 = s{`a%`}
  53. var pass2 = s{`%a`}
  54. var pass3 = s{`%`}
  55. var pass4 = s{`a%a`}
  56. var fail = s{`aa`}
  57. check(t, pass1, 0)
  58. check(t, pass2, 0)
  59. check(t, pass3, 0)
  60. check(t, pass4, 0)
  61. check(t, fail, 1)
  62. }
  63. func TestRuleRegexp(t *testing.T) {
  64. type s struct {
  65. A string `validate:"regexp=^[0-9]$"`
  66. }
  67. var pass1 = s{`0`}
  68. var pass2 = s{`7`}
  69. var fail1 = s{`A`}
  70. var fail2 = s{`11`}
  71. check(t, pass1, 0)
  72. check(t, pass2, 0)
  73. check(t, fail1, 1)
  74. check(t, fail2, 1)
  75. }
  76. func TestRuleEqGtLt(t *testing.T) {
  77. type s struct {
  78. A int `validate:"eq=3"`
  79. B float64 `validate:"gt=1e5"`
  80. C uint `validate:"lt=1"`
  81. }
  82. var pass = s{3, 100001, 0}
  83. var fail1 = s{2, 1e5, 1}
  84. var fail2 = s{4, 9999, 2}
  85. check(t, pass, 0)
  86. check(t, fail1, 3)
  87. check(t, fail2, 3)
  88. }
  89. func TestLenMinMax(t *testing.T) {
  90. type s struct {
  91. A string `validate:"len=3"`
  92. B []int `validate:"min=2"`
  93. C map[int]string `validate:"max=1"`
  94. }
  95. var pass = s{`abc`, []int{1, 2}, nil}
  96. var fail1 = s{`ab`, []int{1}, map[int]string{1: `a`, 2: `b`}}
  97. var fail2 = s{`abcd`, nil, nil}
  98. check(t, pass, 0)
  99. check(t, fail1, 3)
  100. check(t, fail2, 2)
  101. }
  102. func check(t *testing.T, c interface{}, errCount int) {
  103. t.Helper()
  104. errs := Validate(c)
  105. if len(errs) != errCount {
  106. t.Errorf(`Case %T(%v) should get %d errors, but got %v`, c, c, errCount, errs)
  107. }
  108. }