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",
|
||||
"GoVersion": "go1.4.2",
|
||||
"Packages": [
|
||||
"./..."
|
||||
],
|
||||
"GoVersion": "go1.5.1",
|
||||
"Deps": [
|
||||
{
|
||||
"ImportPath": "github.com/dustin/go-broadcast",
|
||||
"Rev": "3bdf6d4a7164a50bc19d5f230e2981d87d2584f1"
|
||||
"ImportPath": "github.com/davecgh/go-spew/spew",
|
||||
"Rev": "5215b55f46b2b919f50a1df0eaa5886afe4e3b3d"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/golang/protobuf/proto",
|
||||
"Rev": "2402d76f3d41f928c7902a765dfc872356dd3aad"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/manucorporat/sse",
|
||||
"Rev": "c142f0f1baea5cef7f98a8a6c222f6134368c1f5"
|
||||
"Rev": "ee05b128a739a0fb76c7ebd3ae4810c1de808d6d"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/manucorporat/stats",
|
||||
"Rev": "8f2d6ace262eba462e9beb552382c98be51d807b"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/mattn/go-colorable",
|
||||
"Rev": "d67e0b7d1797975196499f79bcc322c08b9f218b"
|
||||
"ImportPath": "github.com/pmezard/go-difflib/difflib",
|
||||
"Rev": "792786c7400a136282c1664665ae0a8db921c6c2"
|
||||
},
|
||||
{
|
||||
"ImportPath": "github.com/stretchr/testify/assert",
|
||||
"Comment": "v1.0",
|
||||
"Rev": "232e8563676cd15c3a36ba5e675ad4312ac4cb11"
|
||||
"Comment": "v1.1.3",
|
||||
"Rev": "f390dcf405f7b83c997eac1b06768bb9f44dec18"
|
||||
},
|
||||
{
|
||||
"ImportPath": "golang.org/x/net/context",
|
||||
"Rev": "621fff363a1d9ad7fdd0bfa9d80a42881267deb4"
|
||||
"Rev": "f315505cf3349909cdf013ea56690da34e96a451"
|
||||
},
|
||||
{
|
||||
"ImportPath": "gopkg.in/bluesuncorp/validator.v5",
|
||||
"Comment": "v5.4",
|
||||
"Rev": "07cbdd2e6dfd947b002e83c13b775c7580fab2d5"
|
||||
"ImportPath": "gopkg.in/go-playground/validator.v8",
|
||||
"Comment": "v8.15.1",
|
||||
"Rev": "c193cecd124b5cc722d7ee5538e945bdb3348435"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ import (
|
|||
"reflect"
|
||||
"sync"
|
||||
|
||||
"gopkg.in/bluesuncorp/validator.v5"
|
||||
"gopkg.in/go-playground/validator.v8"
|
||||
)
|
||||
|
||||
type defaultValidator struct {
|
||||
|
@ -26,7 +26,8 @@ func (v *defaultValidator) ValidateStruct(obj interface{}) error {
|
|||
|
||||
func (v *defaultValidator) lazyinit() {
|
||||
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
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type struct1 struct {
|
||||
Value float64 `binding:"required"`
|
||||
type testInterface interface {
|
||||
String() string
|
||||
}
|
||||
|
||||
type struct2 struct {
|
||||
RequiredValue string `binding:"required"`
|
||||
Value float64
|
||||
type substruct_noValidation struct {
|
||||
I_String string
|
||||
I_Int int
|
||||
}
|
||||
|
||||
type struct3 struct {
|
||||
Integer int
|
||||
String string
|
||||
BasicSlice []int
|
||||
type mapNoValidationSub map[string]substruct_noValidation
|
||||
|
||||
type struct_noValidation_values struct {
|
||||
substruct_noValidation
|
||||
|
||||
Boolean bool
|
||||
|
||||
RequiredInteger int `binding:"required"`
|
||||
RequiredString string `binding:"required"`
|
||||
RequiredAnotherStruct struct1 `binding:"required"`
|
||||
RequiredBasicSlice []int `binding:"required"`
|
||||
RequiredComplexSlice []struct2 `binding:"required"`
|
||||
RequiredBoolean bool `binding:"required"`
|
||||
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
|
||||
InlinedStruct struct {
|
||||
String []string
|
||||
Integer int
|
||||
}
|
||||
|
||||
func createStruct() struct3 {
|
||||
return struct3{
|
||||
RequiredInteger: 2,
|
||||
RequiredString: "hello",
|
||||
RequiredAnotherStruct: struct1{1.5},
|
||||
RequiredBasicSlice: []int{1, 2, 3, 4},
|
||||
RequiredComplexSlice: []struct2{
|
||||
{RequiredValue: "A"},
|
||||
{RequiredValue: "B"},
|
||||
IntSlice []int
|
||||
IntPointerSlice []*int
|
||||
StructPointerSlice []*substruct_noValidation
|
||||
StructSlice []substruct_noValidation
|
||||
InterfaceSlice []testInterface
|
||||
|
||||
UniversalInterface interface{}
|
||||
CustomInterface testInterface
|
||||
|
||||
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) {
|
||||
test := createStruct()
|
||||
func TestValidateNoValidationValues(t *testing.T) {
|
||||
origin := createNoValidation_values()
|
||||
test := createNoValidation_values()
|
||||
empty := struct_noValidation_values{}
|
||||
|
||||
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 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.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))
|
||||
|
||||
nu := 10
|
||||
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