1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2025-01-05 10:20:36 +02:00
woodpecker/vendor/github.com/gin-gonic/gin/binding/default_validator.go

42 lines
781 B
Go
Raw Normal View History

2015-09-30 03:21:17 +02:00
package binding
import (
"reflect"
"sync"
2016-03-07 21:23:49 +02:00
"gopkg.in/go-playground/validator.v8"
2015-09-30 03:21:17 +02:00
)
type defaultValidator struct {
once sync.Once
validate *validator.Validate
}
var _ StructValidator = &defaultValidator{}
func (v *defaultValidator) ValidateStruct(obj interface{}) error {
if kindOfData(obj) == reflect.Struct {
v.lazyinit()
if err := v.validate.Struct(obj); err != nil {
return error(err)
}
}
return nil
}
func (v *defaultValidator) lazyinit() {
v.once.Do(func() {
2016-03-07 21:23:49 +02:00
config := &validator.Config{TagName: "binding"}
v.validate = validator.New(config)
2015-09-30 03:21:17 +02:00
})
}
func kindOfData(data interface{}) reflect.Kind {
value := reflect.ValueOf(data)
valueType := value.Kind()
if valueType == reflect.Ptr {
valueType = value.Elem().Kind()
}
return valueType
}