OSDN Git Service

Merge branch 'master' into wallet-store-interface-account
[bytom/vapor.git] / vendor / github.com / gin-gonic / gin / binding / default_validator.go
diff --git a/vendor/github.com/gin-gonic/gin/binding/default_validator.go b/vendor/github.com/gin-gonic/gin/binding/default_validator.go
new file mode 100644 (file)
index 0000000..e7a302d
--- /dev/null
@@ -0,0 +1,51 @@
+// Copyright 2017 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 (
+       "reflect"
+       "sync"
+
+       "gopkg.in/go-playground/validator.v8"
+)
+
+type defaultValidator struct {
+       once     sync.Once
+       validate *validator.Validate
+}
+
+var _ StructValidator = &defaultValidator{}
+
+// ValidateStruct receives any kind of type, but only performed struct or pointer to struct type.
+func (v *defaultValidator) ValidateStruct(obj interface{}) error {
+       value := reflect.ValueOf(obj)
+       valueType := value.Kind()
+       if valueType == reflect.Ptr {
+               valueType = value.Elem().Kind()
+       }
+       if valueType == reflect.Struct {
+               v.lazyinit()
+               if err := v.validate.Struct(obj); err != nil {
+                       return err
+               }
+       }
+       return nil
+}
+
+// Engine returns the underlying validator engine which powers the default
+// Validator instance. This is useful if you want to register custom validations
+// or struct level validations. See validator GoDoc for more info -
+// https://godoc.org/gopkg.in/go-playground/validator.v8
+func (v *defaultValidator) Engine() interface{} {
+       v.lazyinit()
+       return v.validate
+}
+
+func (v *defaultValidator) lazyinit() {
+       v.once.Do(func() {
+               config := &validator.Config{TagName: "binding"}
+               v.validate = validator.New(config)
+       })
+}