[FIX] Validate()

This commit is contained in:
wuhuizuo 2019-12-25 20:57:25 +08:00 committed by wuhuizuo
parent 13ea8367dc
commit 93f9e4860d
2 changed files with 11 additions and 16 deletions

View File

@ -187,7 +187,8 @@ func TestBindingJSONSlice(t *testing.T) {
}() }()
testBodyBindingSlice(t, JSON, "json", "/", "/", `[]`, ``) testBodyBindingSlice(t, JSON, "json", "/", "/", `[]`, ``)
testBodyBindingSlice(t, JSON, "json", "/", "/", `[{}]`, `{}`) testBodyBindingSlice(t, JSON, "json", "/", "/", `[{"foo": "123"}]`, `[{}]`)
testBodyBindingSlice(t, JSON, "json", "/", "/", `[{"foo": "123"}]`, `[{"foo": ""}]`)
testBodyBindingSlice(t, JSON, "json", "/", "/", `[{"foo": "123"}]`, `[{"foo": 123}]`) testBodyBindingSlice(t, JSON, "json", "/", "/", `[{"foo": "123"}]`, `[{"foo": 123}]`)
testBodyBindingSlice(t, JSON, "json", "/", "/", `[{"foo": "123"}]`, `[{"bar": 123}]`) testBodyBindingSlice(t, JSON, "json", "/", "/", `[{"foo": "123"}]`, `[{"bar": 123}]`)
testBodyBindingSlice(t, JSON, "json", "/", "/", `[{"foo": "123"}]`, `[{"foo": "123456789012345678901234567890123"}]`) testBodyBindingSlice(t, JSON, "json", "/", "/", `[{"foo": "123"}]`, `[{"foo": "123456789012345678901234567890123"}]`)

View File

@ -5,14 +5,13 @@
package binding package binding
import ( import (
"reflect"
"fmt" "fmt"
"sync"
"strings"
"log" "log"
"reflect"
"strings"
"sync"
"github.com/go-playground/validator/v10" "github.com/go-playground/validator/v10"
// "github.com/ahmetb/go-linq"
) )
type defaultValidator struct { type defaultValidator struct {
@ -20,7 +19,7 @@ type defaultValidator struct {
validate *validator.Validate validate *validator.Validate
} }
type sliceValidateError []error type sliceValidateError []error
func (err sliceValidateError) Error() string { func (err sliceValidateError) Error() string {
var errMsgs []string var errMsgs []string
@ -28,7 +27,7 @@ func (err sliceValidateError) Error() string {
if e == nil { if e == nil {
continue continue
} }
errMsgs = append(errMsgs, fmt.Sprintf("[%d]: %s", i, err.Error())) errMsgs = append(errMsgs, fmt.Sprintf("[%d]: %s", i, e.Error()))
} }
return strings.Join(errMsgs, "\n") return strings.Join(errMsgs, "\n")
} }
@ -37,29 +36,24 @@ var _ ValidatorImp = &defaultValidator{}
// ValidateStruct receives any kind of type, but only performed struct or pointer to struct type. // ValidateStruct receives any kind of type, but only performed struct or pointer to struct type.
func (v *defaultValidator) Validate(obj interface{}) error { func (v *defaultValidator) Validate(obj interface{}) error {
log.Println("called")
if obj == nil { if obj == nil {
return nil return nil
} }
value := reflect.ValueOf(obj) value := reflect.ValueOf(obj)
valueType := value.Kind() valueType := value.Kind()
log.Printf("valueType: %v, %#v\n", valueType, valueType)
if valueType == reflect.Ptr { if valueType == reflect.Ptr {
value = value.Elem() return v.Validate(value.Elem().Interface())
valueType = value.Kind()
} }
switch valueType { switch valueType {
case reflect.Struct: case reflect.Struct:
log.Printf("goto validateStruct: %v, %#v\n", obj, obj)
return v.validateStruct(obj) return v.validateStruct(obj)
case reflect.Slice, reflect.Array: case reflect.Slice, reflect.Array:
count := value.Len() count := value.Len()
validateRet := make(sliceValidateError, 0) validateRet := make(sliceValidateError, 0)
for i := 0; i < count; i++ { for i := 0; i < count; i++ {
log.Println("called inside slice") if err := v.Validate(value.Index(i).Interface()); err != nil {
if err := v.Validate(value.Index(i)); err != nil {
validateRet = append(validateRet, err) validateRet = append(validateRet, err)
} }
} }
@ -67,7 +61,7 @@ func (v *defaultValidator) Validate(obj interface{}) error {
if len(validateRet) == 0 { if len(validateRet) == 0 {
return nil return nil
} }
return validateRet return validateRet
default: default:
return nil return nil
} }