validate_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // Copyright 2014 Manu Martinez-Almeida. All rights reserved.
  2. // Use of this source code is governed by a MIT style
  3. // license that can be found in the LICENSE file.
  4. package binding
  5. import (
  6. "bytes"
  7. "testing"
  8. "time"
  9. "github.com/stretchr/testify/assert"
  10. "gopkg.in/go-playground/validator.v9"
  11. )
  12. type testInterface interface {
  13. String() string
  14. }
  15. type substructNoValidation struct {
  16. IString string
  17. IInt int
  18. }
  19. type mapNoValidationSub map[string]substructNoValidation
  20. type structNoValidationValues struct {
  21. substructNoValidation
  22. Boolean bool
  23. Uinteger uint
  24. Integer int
  25. Integer8 int8
  26. Integer16 int16
  27. Integer32 int32
  28. Integer64 int64
  29. Uinteger8 uint8
  30. Uinteger16 uint16
  31. Uinteger32 uint32
  32. Uinteger64 uint64
  33. Float32 float32
  34. Float64 float64
  35. String string
  36. Date time.Time
  37. Struct substructNoValidation
  38. InlinedStruct struct {
  39. String []string
  40. Integer int
  41. }
  42. IntSlice []int
  43. IntPointerSlice []*int
  44. StructPointerSlice []*substructNoValidation
  45. StructSlice []substructNoValidation
  46. InterfaceSlice []testInterface
  47. UniversalInterface interface{}
  48. CustomInterface testInterface
  49. FloatMap map[string]float32
  50. StructMap mapNoValidationSub
  51. }
  52. func createNoValidationValues() structNoValidationValues {
  53. integer := 1
  54. s := structNoValidationValues{
  55. Boolean: true,
  56. Uinteger: 1 << 29,
  57. Integer: -10000,
  58. Integer8: 120,
  59. Integer16: -20000,
  60. Integer32: 1 << 29,
  61. Integer64: 1 << 61,
  62. Uinteger8: 250,
  63. Uinteger16: 50000,
  64. Uinteger32: 1 << 31,
  65. Uinteger64: 1 << 62,
  66. Float32: 123.456,
  67. Float64: 123.456789,
  68. String: "text",
  69. Date: time.Time{},
  70. CustomInterface: &bytes.Buffer{},
  71. Struct: substructNoValidation{},
  72. IntSlice: []int{-3, -2, 1, 0, 1, 2, 3},
  73. IntPointerSlice: []*int{&integer},
  74. StructSlice: []substructNoValidation{},
  75. UniversalInterface: 1.2,
  76. FloatMap: map[string]float32{
  77. "foo": 1.23,
  78. "bar": 232.323,
  79. },
  80. StructMap: mapNoValidationSub{
  81. "foo": substructNoValidation{},
  82. "bar": substructNoValidation{},
  83. },
  84. // StructPointerSlice []noValidationSub
  85. // InterfaceSlice []testInterface
  86. }
  87. s.InlinedStruct.Integer = 1000
  88. s.InlinedStruct.String = []string{"first", "second"}
  89. s.IString = "substring"
  90. s.IInt = 987654
  91. return s
  92. }
  93. func TestValidateNoValidationValues(t *testing.T) {
  94. origin := createNoValidationValues()
  95. test := createNoValidationValues()
  96. empty := structNoValidationValues{}
  97. assert.Nil(t, validate(test))
  98. assert.Nil(t, validate(&test))
  99. assert.Nil(t, validate(empty))
  100. assert.Nil(t, validate(&empty))
  101. assert.Equal(t, origin, test)
  102. }
  103. type structNoValidationPointer struct {
  104. substructNoValidation
  105. Boolean bool
  106. Uinteger *uint
  107. Integer *int
  108. Integer8 *int8
  109. Integer16 *int16
  110. Integer32 *int32
  111. Integer64 *int64
  112. Uinteger8 *uint8
  113. Uinteger16 *uint16
  114. Uinteger32 *uint32
  115. Uinteger64 *uint64
  116. Float32 *float32
  117. Float64 *float64
  118. String *string
  119. Date *time.Time
  120. Struct *substructNoValidation
  121. IntSlice *[]int
  122. IntPointerSlice *[]*int
  123. StructPointerSlice *[]*substructNoValidation
  124. StructSlice *[]substructNoValidation
  125. InterfaceSlice *[]testInterface
  126. FloatMap *map[string]float32
  127. StructMap *mapNoValidationSub
  128. }
  129. func TestValidateNoValidationPointers(t *testing.T) {
  130. //origin := createNoValidation_values()
  131. //test := createNoValidation_values()
  132. empty := structNoValidationPointer{}
  133. //assert.Nil(t, validate(test))
  134. //assert.Nil(t, validate(&test))
  135. assert.Nil(t, validate(empty))
  136. assert.Nil(t, validate(&empty))
  137. //assert.Equal(t, origin, test)
  138. }
  139. type Object map[string]interface{}
  140. func TestValidatePrimitives(t *testing.T) {
  141. obj := Object{"foo": "bar", "bar": 1}
  142. assert.NoError(t, validate(obj))
  143. assert.NoError(t, validate(&obj))
  144. assert.Equal(t, Object{"foo": "bar", "bar": 1}, obj)
  145. obj2 := []Object{{"foo": "bar", "bar": 1}, {"foo": "bar", "bar": 1}}
  146. assert.NoError(t, validate(obj2))
  147. assert.NoError(t, validate(&obj2))
  148. nu := 10
  149. assert.NoError(t, validate(nu))
  150. assert.NoError(t, validate(&nu))
  151. assert.Equal(t, 10, nu)
  152. str := "value"
  153. assert.NoError(t, validate(str))
  154. assert.NoError(t, validate(&str))
  155. assert.Equal(t, "value", str)
  156. }
  157. // structCustomValidation is a helper struct we use to check that
  158. // custom validation can be registered on it.
  159. // The `notone` binding directive is for custom validation and registered later.
  160. type structCustomValidation struct {
  161. Integer int `binding:"notone"`
  162. }
  163. func notOne(f1 validator.FieldLevel) bool {
  164. if val, ok := f1.Field().Interface().(int); ok {
  165. return val != 1
  166. }
  167. return false
  168. }
  169. func TestValidatorEngine(t *testing.T) {
  170. // This validates that the function `notOne` matches
  171. // the expected function signature by `defaultValidator`
  172. // and by extension the validator library.
  173. engine, ok := Validator.Engine().(*validator.Validate)
  174. assert.True(t, ok)
  175. err := engine.RegisterValidation("notone", notOne)
  176. // Check that we can register custom validation without error
  177. assert.Nil(t, err)
  178. // Create an instance which will fail validation
  179. withOne := structCustomValidation{Integer: 1}
  180. errs := validate(withOne)
  181. // Check that we got back non-nil errs
  182. assert.NotNil(t, errs)
  183. // Check that the error matches expectation
  184. assert.Error(t, errs, "", "", "notone")
  185. }