Experimenting with new validation library!!!

This commit is contained in:
Manu Mtz-Almeida 2015-04-07 12:30:16 +02:00
parent ea962038e1
commit ee3b67eda1
6 changed files with 13 additions and 88 deletions

View File

@ -4,7 +4,11 @@
package binding package binding
import "net/http" import (
"net/http"
"gopkg.in/joeybloggs/go-validate-yourself.v4"
)
const ( const (
MIMEJSON = "application/json" MIMEJSON = "application/json"
@ -21,6 +25,8 @@ type Binding interface {
Bind(*http.Request, interface{}) error Bind(*http.Request, interface{}) error
} }
var _validator = validator.NewValidator("binding", validator.BakedInValidators)
var ( var (
JSON = jsonBinding{} JSON = jsonBinding{}
XML = xmlBinding{} XML = xmlBinding{}

View File

@ -19,5 +19,5 @@ func (_ getFormBinding) Bind(req *http.Request, obj interface{}) error {
if err := mapForm(obj, req.Form); err != nil { if err := mapForm(obj, req.Form); err != nil {
return err return err
} }
return Validate(obj) return _validator.ValidateStruct(obj)
} }

View File

@ -18,9 +18,8 @@ func (_ jsonBinding) Name() string {
func (_ jsonBinding) Bind(req *http.Request, obj interface{}) error { func (_ jsonBinding) Bind(req *http.Request, obj interface{}) error {
decoder := json.NewDecoder(req.Body) decoder := json.NewDecoder(req.Body)
if err := decoder.Decode(obj); err == nil { if err := decoder.Decode(obj); err != nil {
return Validate(obj)
} else {
return err return err
} }
return _validator.ValidateStruct(obj)
} }

View File

@ -19,5 +19,5 @@ func (_ postFormBinding) Bind(req *http.Request, obj interface{}) error {
if err := mapForm(obj, req.PostForm); err != nil { if err := mapForm(obj, req.PostForm); err != nil {
return err return err
} }
return Validate(obj) return _validator.ValidateStruct(obj)
} }

View File

@ -1,79 +0,0 @@
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package binding
import (
"errors"
"reflect"
"strings"
)
func Validate(obj interface{}) error {
return validate(obj, "{{ROOT}}")
}
func validate(obj interface{}, parent string) error {
typ, val := inspectObject(obj)
switch typ.Kind() {
case reflect.Struct:
return validateStruct(typ, val, parent)
case reflect.Slice:
return validateSlice(typ, val, parent)
default:
return errors.New("The object is not a slice or struct.")
}
}
func inspectObject(obj interface{}) (typ reflect.Type, val reflect.Value) {
typ = reflect.TypeOf(obj)
val = reflect.ValueOf(obj)
if typ.Kind() == reflect.Ptr {
typ = typ.Elem()
val = val.Elem()
}
return
}
func validateSlice(typ reflect.Type, val reflect.Value, parent string) error {
if typ.Elem().Kind() == reflect.Struct {
for i := 0; i < val.Len(); i++ {
itemValue := val.Index(i).Interface()
if err := validate(itemValue, parent); err != nil {
return err
}
}
}
return nil
}
func validateStruct(typ reflect.Type, val reflect.Value, parent string) error {
for i := 0; i < typ.NumField(); i++ {
field := typ.Field(i)
// Allow ignored and unexported fields in the struct
// TODO should include || field.Tag.Get("form") == "-"
if len(field.PkgPath) > 0 {
continue
}
fieldValue := val.Field(i).Interface()
requiredField := strings.Index(field.Tag.Get("binding"), "required") > -1
if requiredField {
zero := reflect.Zero(field.Type).Interface()
if reflect.DeepEqual(zero, fieldValue) {
return errors.New("Required " + field.Name + " in " + parent)
}
}
fieldType := field.Type.Kind()
if fieldType == reflect.Struct || fieldType == reflect.Slice {
if err := validate(fieldValue, field.Name); err != nil {
return err
}
}
}
return nil
}

View File

@ -17,9 +17,8 @@ func (_ xmlBinding) Name() string {
func (_ xmlBinding) Bind(req *http.Request, obj interface{}) error { func (_ xmlBinding) Bind(req *http.Request, obj interface{}) error {
decoder := xml.NewDecoder(req.Body) decoder := xml.NewDecoder(req.Body)
if err := decoder.Decode(obj); err == nil { if err := decoder.Decode(obj); err != nil {
return Validate(obj)
} else {
return err return err
} }
return _validator.ValidateStruct(obj)
} }