Skip to content

Commit

Permalink
Updates Validator + unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
manucorporat committed Jan 26, 2016
1 parent 60f6691 commit ffb5c04
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 54 deletions.
33 changes: 15 additions & 18 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions binding/default_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"reflect"
"sync"

"gopkg.in/bluesuncorp/validator.v5"
"gopkg.in/go-playground/validator.v8"
)

type defaultValidator struct {
Expand All @@ -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)
})
}

Expand Down
191 changes: 157 additions & 34 deletions binding/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
type mapNoValidationSub map[string]substruct_noValidation

type struct_noValidation_values struct {
substruct_noValidation

Boolean bool

Uinteger uint
Integer int
String string
BasicSlice []int
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"`
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
}

IntSlice []int
IntPointerSlice []*int
StructPointerSlice []*substruct_noValidation
StructSlice []substruct_noValidation
InterfaceSlice []testInterface

UniversalInterface interface{}
CustomInterface testInterface

FloatMap map[string]float32
StructMap mapNoValidationSub
}

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"},
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,
},
StructMap: mapNoValidationSub{
"foo": substruct_noValidation{},
"bar": substruct_noValidation{},
},
RequiredBoolean: true,
// 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 Object map[string]interface{}
type MyObjects []Object
type struct_noValidation_pointer struct {
substruct_noValidation

Boolean bool

func TestValidateSlice(t *testing.T) {
var obj MyObjects
var obj2 Object
var nu = 10
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{}

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")
}

0 comments on commit ffb5c04

Please sign in to comment.