gin/binding/default_validator.go

86 lines
2.0 KiB
Go
Raw Normal View History

2017-06-12 09:04:52 +03:00
// 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.
2015-05-31 17:18:50 +03:00
package binding
import (
"fmt"
2015-05-31 17:18:50 +03:00
"reflect"
"strings"
2015-05-31 17:18:50 +03:00
"sync"
"github.com/go-playground/validator/v10"
2015-05-31 17:18:50 +03:00
)
type defaultValidator struct {
once sync.Once
validate *validator.Validate
}
type sliceValidateError []error
func (err sliceValidateError) Error() string {
var errMsgs []string
for i, e := range err {
if e == nil {
continue
}
errMsgs = append(errMsgs, fmt.Sprintf("[%d]: %s", i, e.Error()))
}
return strings.Join(errMsgs, "\n")
}
2015-05-31 17:18:50 +03:00
var _ StructValidator = &defaultValidator{}
// ValidateStruct receives any kind of type, but only performed struct or pointer to struct type.
2015-05-31 17:18:50 +03:00
func (v *defaultValidator) ValidateStruct(obj interface{}) error {
if obj == nil {
return nil
}
value := reflect.ValueOf(obj)
switch value.Kind() {
case reflect.Ptr:
return v.ValidateStruct(value.Elem().Interface())
case reflect.Struct:
return v.validateStruct(obj)
case reflect.Slice, reflect.Array:
count := value.Len()
validateRet := make(sliceValidateError, 0)
for i := 0; i < count; i++ {
if err := v.ValidateStruct(value.Index(i).Interface()); err != nil {
validateRet = append(validateRet, err)
}
}
if len(validateRet) == 0 {
return nil
2015-05-31 17:30:00 +03:00
}
return validateRet
default:
return nil
2015-05-31 17:18:50 +03:00
}
}
// validateStruct receives struct type
func (v *defaultValidator) validateStruct(obj interface{}) error {
v.lazyinit()
return v.validate.Struct(obj)
2015-05-31 17:18:50 +03:00
}
// 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
}
2015-05-31 17:18:50 +03:00
func (v *defaultValidator) lazyinit() {
v.once.Do(func() {
v.validate = validator.New()
v.validate.SetTagName("binding")
2015-05-31 17:18:50 +03:00
})
}