Переглянути джерело

Experimenting with new validation library!!!

Manu Mtz-Almeida 10 роки тому
батько
коміт
ee3b67eda1
6 змінених файлів з 13 додано та 88 видалено
  1. 7 1
      binding/binding.go
  2. 1 1
      binding/get_form.go
  3. 2 3
      binding/json.go
  4. 1 1
      binding/post_form.go
  5. 0 79
      binding/validate.go
  6. 2 3
      binding/xml.go

+ 7 - 1
binding/binding.go

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

+ 1 - 1
binding/get_form.go

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

+ 2 - 3
binding/json.go

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

+ 1 - 1
binding/post_form.go

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

+ 0 - 79
binding/validate.go

@@ -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
-}

+ 2 - 3
binding/xml.go

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