mirror of https://github.com/gin-gonic/gin.git
Updates Validator + unit tests
This commit is contained in:
parent
60f66918f8
commit
ffb5c0412a
|
@ -1,39 +1,36 @@
|
||||||
{
|
{
|
||||||
"ImportPath": "github.com/gin-gonic/gin",
|
"ImportPath": "github.com/gin-gonic/gin",
|
||||||
"GoVersion": "go1.4.2",
|
"GoVersion": "go1.5.1",
|
||||||
"Packages": [
|
|
||||||
"./..."
|
|
||||||
],
|
|
||||||
"Deps": [
|
"Deps": [
|
||||||
{
|
{
|
||||||
"ImportPath": "github.com/dustin/go-broadcast",
|
"ImportPath": "github.com/davecgh/go-spew/spew",
|
||||||
"Rev": "3bdf6d4a7164a50bc19d5f230e2981d87d2584f1"
|
"Rev": "5215b55f46b2b919f50a1df0eaa5886afe4e3b3d"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ImportPath": "github.com/golang/protobuf/proto",
|
||||||
|
"Rev": "2402d76f3d41f928c7902a765dfc872356dd3aad"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "github.com/manucorporat/sse",
|
"ImportPath": "github.com/manucorporat/sse",
|
||||||
"Rev": "c142f0f1baea5cef7f98a8a6c222f6134368c1f5"
|
"Rev": "ee05b128a739a0fb76c7ebd3ae4810c1de808d6d"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "github.com/manucorporat/stats",
|
"ImportPath": "github.com/pmezard/go-difflib/difflib",
|
||||||
"Rev": "8f2d6ace262eba462e9beb552382c98be51d807b"
|
"Rev": "792786c7400a136282c1664665ae0a8db921c6c2"
|
||||||
},
|
|
||||||
{
|
|
||||||
"ImportPath": "github.com/mattn/go-colorable",
|
|
||||||
"Rev": "d67e0b7d1797975196499f79bcc322c08b9f218b"
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "github.com/stretchr/testify/assert",
|
"ImportPath": "github.com/stretchr/testify/assert",
|
||||||
"Comment": "v1.0",
|
"Comment": "v1.1.3",
|
||||||
"Rev": "232e8563676cd15c3a36ba5e675ad4312ac4cb11"
|
"Rev": "f390dcf405f7b83c997eac1b06768bb9f44dec18"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "golang.org/x/net/context",
|
"ImportPath": "golang.org/x/net/context",
|
||||||
"Rev": "621fff363a1d9ad7fdd0bfa9d80a42881267deb4"
|
"Rev": "f315505cf3349909cdf013ea56690da34e96a451"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "gopkg.in/bluesuncorp/validator.v5",
|
"ImportPath": "gopkg.in/go-playground/validator.v8",
|
||||||
"Comment": "v5.4",
|
"Comment": "v8.15.1",
|
||||||
"Rev": "07cbdd2e6dfd947b002e83c13b775c7580fab2d5"
|
"Rev": "c193cecd124b5cc722d7ee5538e945bdb3348435"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"gopkg.in/bluesuncorp/validator.v5"
|
"gopkg.in/go-playground/validator.v8"
|
||||||
)
|
)
|
||||||
|
|
||||||
type defaultValidator struct {
|
type defaultValidator struct {
|
||||||
|
@ -26,7 +26,8 @@ func (v *defaultValidator) ValidateStruct(obj interface{}) error {
|
||||||
|
|
||||||
func (v *defaultValidator) lazyinit() {
|
func (v *defaultValidator) lazyinit() {
|
||||||
v.once.Do(func() {
|
v.once.Do(func() {
|
||||||
v.validate = validator.New("binding", validator.BakedInValidators)
|
config := &validator.Config{TagName: "binding"}
|
||||||
|
v.validate = validator.New(config)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,65 +5,188 @@
|
||||||
package binding
|
package binding
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
type struct1 struct {
|
type testInterface interface {
|
||||||
Value float64 `binding:"required"`
|
String() string
|
||||||
}
|
}
|
||||||
|
|
||||||
type struct2 struct {
|
type substruct_noValidation struct {
|
||||||
RequiredValue string `binding:"required"`
|
I_String string
|
||||||
Value float64
|
I_Int int
|
||||||
}
|
}
|
||||||
|
|
||||||
type struct3 struct {
|
type mapNoValidationSub map[string]substruct_noValidation
|
||||||
Integer int
|
|
||||||
String string
|
type struct_noValidation_values struct {
|
||||||
BasicSlice []int
|
substruct_noValidation
|
||||||
|
|
||||||
Boolean bool
|
Boolean bool
|
||||||
|
|
||||||
RequiredInteger int `binding:"required"`
|
Uinteger uint
|
||||||
RequiredString string `binding:"required"`
|
Integer int
|
||||||
RequiredAnotherStruct struct1 `binding:"required"`
|
Integer8 int8
|
||||||
RequiredBasicSlice []int `binding:"required"`
|
Integer16 int16
|
||||||
RequiredComplexSlice []struct2 `binding:"required"`
|
Integer32 int32
|
||||||
RequiredBoolean bool `binding:"required"`
|
Integer64 int64
|
||||||
|
Uinteger8 uint8
|
||||||
|
Uinteger16 uint16
|
||||||
|
Uinteger32 uint32
|
||||||
|
Uinteger64 uint64
|
||||||
|
|
||||||
|
Float32 float32
|
||||||
|
Float64 float64
|
||||||
|
|
||||||
|
String string
|
||||||
|
|
||||||
|
Date time.Time
|
||||||
|
|
||||||
|
Struct substruct_noValidation
|
||||||
|
InlinedStruct struct {
|
||||||
|
String []string
|
||||||
|
Integer int
|
||||||
}
|
}
|
||||||
|
|
||||||
func createStruct() struct3 {
|
IntSlice []int
|
||||||
return struct3{
|
IntPointerSlice []*int
|
||||||
RequiredInteger: 2,
|
StructPointerSlice []*substruct_noValidation
|
||||||
RequiredString: "hello",
|
StructSlice []substruct_noValidation
|
||||||
RequiredAnotherStruct: struct1{1.5},
|
InterfaceSlice []testInterface
|
||||||
RequiredBasicSlice: []int{1, 2, 3, 4},
|
|
||||||
RequiredComplexSlice: []struct2{
|
UniversalInterface interface{}
|
||||||
{RequiredValue: "A"},
|
CustomInterface testInterface
|
||||||
{RequiredValue: "B"},
|
|
||||||
|
FloatMap map[string]float32
|
||||||
|
StructMap mapNoValidationSub
|
||||||
|
}
|
||||||
|
|
||||||
|
func createNoValidation_values() struct_noValidation_values {
|
||||||
|
integer := 1
|
||||||
|
s := struct_noValidation_values{
|
||||||
|
Boolean: true,
|
||||||
|
Uinteger: 1 << 29,
|
||||||
|
Integer: -10000,
|
||||||
|
Integer8: 120,
|
||||||
|
Integer16: -20000,
|
||||||
|
Integer32: 1 << 29,
|
||||||
|
Integer64: 1 << 61,
|
||||||
|
Uinteger8: 250,
|
||||||
|
Uinteger16: 50000,
|
||||||
|
Uinteger32: 1 << 31,
|
||||||
|
Uinteger64: 1 << 62,
|
||||||
|
Float32: 123.456,
|
||||||
|
Float64: 123.456789,
|
||||||
|
String: "text",
|
||||||
|
Date: time.Time{},
|
||||||
|
CustomInterface: &bytes.Buffer{},
|
||||||
|
Struct: substruct_noValidation{},
|
||||||
|
IntSlice: []int{-3, -2, 1, 0, 1, 2, 3},
|
||||||
|
IntPointerSlice: []*int{&integer},
|
||||||
|
StructSlice: []substruct_noValidation{},
|
||||||
|
UniversalInterface: 1.2,
|
||||||
|
FloatMap: map[string]float32{
|
||||||
|
"foo": 1.23,
|
||||||
|
"bar": 232.323,
|
||||||
},
|
},
|
||||||
RequiredBoolean: true,
|
StructMap: mapNoValidationSub{
|
||||||
|
"foo": substruct_noValidation{},
|
||||||
|
"bar": substruct_noValidation{},
|
||||||
|
},
|
||||||
|
// StructPointerSlice []noValidationSub
|
||||||
|
// InterfaceSlice []testInterface
|
||||||
}
|
}
|
||||||
|
s.InlinedStruct.Integer = 1000
|
||||||
|
s.InlinedStruct.String = []string{"first", "second"}
|
||||||
|
s.I_String = "substring"
|
||||||
|
s.I_Int = 987654
|
||||||
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestValidateGoodObject(t *testing.T) {
|
func TestValidateNoValidationValues(t *testing.T) {
|
||||||
test := createStruct()
|
origin := createNoValidation_values()
|
||||||
|
test := createNoValidation_values()
|
||||||
|
empty := struct_noValidation_values{}
|
||||||
|
|
||||||
|
assert.Nil(t, validate(test))
|
||||||
assert.Nil(t, validate(&test))
|
assert.Nil(t, validate(&test))
|
||||||
|
assert.Nil(t, validate(empty))
|
||||||
|
assert.Nil(t, validate(&empty))
|
||||||
|
|
||||||
|
assert.Equal(t, origin, test)
|
||||||
|
}
|
||||||
|
|
||||||
|
type struct_noValidation_pointer struct {
|
||||||
|
substruct_noValidation
|
||||||
|
|
||||||
|
Boolean bool
|
||||||
|
|
||||||
|
Uinteger *uint
|
||||||
|
Integer *int
|
||||||
|
Integer8 *int8
|
||||||
|
Integer16 *int16
|
||||||
|
Integer32 *int32
|
||||||
|
Integer64 *int64
|
||||||
|
Uinteger8 *uint8
|
||||||
|
Uinteger16 *uint16
|
||||||
|
Uinteger32 *uint32
|
||||||
|
Uinteger64 *uint64
|
||||||
|
|
||||||
|
Float32 *float32
|
||||||
|
Float64 *float64
|
||||||
|
|
||||||
|
String *string
|
||||||
|
|
||||||
|
Date *time.Time
|
||||||
|
|
||||||
|
Struct *substruct_noValidation
|
||||||
|
|
||||||
|
IntSlice *[]int
|
||||||
|
IntPointerSlice *[]*int
|
||||||
|
StructPointerSlice *[]*substruct_noValidation
|
||||||
|
StructSlice *[]substruct_noValidation
|
||||||
|
InterfaceSlice *[]testInterface
|
||||||
|
|
||||||
|
FloatMap *map[string]float32
|
||||||
|
StructMap *mapNoValidationSub
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestValidateNoValidationPointers(t *testing.T) {
|
||||||
|
//origin := createNoValidation_values()
|
||||||
|
//test := createNoValidation_values()
|
||||||
|
empty := struct_noValidation_pointer{}
|
||||||
|
|
||||||
|
//assert.Nil(t, validate(test))
|
||||||
|
//assert.Nil(t, validate(&test))
|
||||||
|
assert.Nil(t, validate(empty))
|
||||||
|
assert.Nil(t, validate(&empty))
|
||||||
|
|
||||||
|
//assert.Equal(t, origin, test)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Object map[string]interface{}
|
type Object map[string]interface{}
|
||||||
type MyObjects []Object
|
|
||||||
|
|
||||||
func TestValidateSlice(t *testing.T) {
|
|
||||||
var obj MyObjects
|
|
||||||
var obj2 Object
|
|
||||||
var nu = 10
|
|
||||||
|
|
||||||
|
func TestValidatePrimitives(t *testing.T) {
|
||||||
|
obj := Object{"foo": "bar", "bar": 1}
|
||||||
assert.NoError(t, validate(obj))
|
assert.NoError(t, validate(obj))
|
||||||
assert.NoError(t, validate(&obj))
|
assert.NoError(t, validate(&obj))
|
||||||
|
assert.Equal(t, obj, Object{"foo": "bar", "bar": 1})
|
||||||
|
|
||||||
|
obj2 := []Object{{"foo": "bar", "bar": 1}, {"foo": "bar", "bar": 1}}
|
||||||
assert.NoError(t, validate(obj2))
|
assert.NoError(t, validate(obj2))
|
||||||
assert.NoError(t, validate(&obj2))
|
assert.NoError(t, validate(&obj2))
|
||||||
|
|
||||||
|
nu := 10
|
||||||
assert.NoError(t, validate(nu))
|
assert.NoError(t, validate(nu))
|
||||||
assert.NoError(t, validate(&nu))
|
assert.NoError(t, validate(&nu))
|
||||||
|
assert.Equal(t, nu, 10)
|
||||||
|
|
||||||
|
str := "value"
|
||||||
|
assert.NoError(t, validate(str))
|
||||||
|
assert.NoError(t, validate(&str))
|
||||||
|
assert.Equal(t, str, "value")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue