default_validator.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright 2017 Manu Martinez-Almeida. All rights reserved.
  2. // Use of this source code is governed by a MIT style
  3. // license that can be found in the LICENSE file.
  4. package binding
  5. //"gopkg.in/go-playground/validator.v8"
  6. /*
  7. type defaultValidator struct {
  8. once sync.Once
  9. validate *validator.Validate
  10. }
  11. var _ StructValidator = &defaultValidator{}
  12. // ValidateStruct receives any kind of type, but only performed struct or pointer to struct type.
  13. func (v *defaultValidator) ValidateStruct(obj interface{}) error {
  14. value := reflect.ValueOf(obj)
  15. valueType := value.Kind()
  16. if valueType == reflect.Ptr {
  17. valueType = value.Elem().Kind()
  18. }
  19. if valueType == reflect.Struct {
  20. v.lazyinit()
  21. if err := v.validate.Struct(obj); err != nil {
  22. return err
  23. }
  24. }
  25. return nil
  26. }
  27. // Engine returns the underlying validator engine which powers the default
  28. // Validator instance. This is useful if you want to register custom validations
  29. // or struct level validations. See validator GoDoc for more info -
  30. // https://godoc.org/gopkg.in/go-playground/validator.v8
  31. func (v *defaultValidator) Engine() interface{} {
  32. v.lazyinit()
  33. return v.validate
  34. }
  35. func (v *defaultValidator) lazyinit() {
  36. v.once.Do(func() {
  37. config := &validator.Config{TagName: "binding"}
  38. v.validate = validator.New(config)
  39. })
  40. }
  41. // */