OSDN Git Service

feat(warder): add warder backbone (#181)
[bytom/vapor.git] / vendor / github.com / gin-gonic / gin / binding / default_validator.go
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
5 package binding
6
7 import (
8         "reflect"
9         "sync"
10
11         "gopkg.in/go-playground/validator.v8"
12 )
13
14 type defaultValidator struct {
15         once     sync.Once
16         validate *validator.Validate
17 }
18
19 var _ StructValidator = &defaultValidator{}
20
21 // ValidateStruct receives any kind of type, but only performed struct or pointer to struct type.
22 func (v *defaultValidator) ValidateStruct(obj interface{}) error {
23         value := reflect.ValueOf(obj)
24         valueType := value.Kind()
25         if valueType == reflect.Ptr {
26                 valueType = value.Elem().Kind()
27         }
28         if valueType == reflect.Struct {
29                 v.lazyinit()
30                 if err := v.validate.Struct(obj); err != nil {
31                         return err
32                 }
33         }
34         return nil
35 }
36
37 // Engine returns the underlying validator engine which powers the default
38 // Validator instance. This is useful if you want to register custom validations
39 // or struct level validations. See validator GoDoc for more info -
40 // https://godoc.org/gopkg.in/go-playground/validator.v8
41 func (v *defaultValidator) Engine() interface{} {
42         v.lazyinit()
43         return v.validate
44 }
45
46 func (v *defaultValidator) lazyinit() {
47         v.once.Do(func() {
48                 config := &validator.Config{TagName: "binding"}
49                 v.validate = validator.New(config)
50         })
51 }