/
validator.go
76 lines (63 loc) · 1.46 KB
/
validator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package lib
import (
"errors"
"fmt"
"net/url"
"reflect"
"strings"
"github.com/go-playground/validator/v10"
"github.com/gorilla/schema"
)
var decoder = schema.NewDecoder()
var validate *validator.Validate
func init() {
validate = validator.New()
validate.RegisterTagNameFunc(func(fld reflect.StructField) string {
fieldName := fld.Tag.Get("json")
if fieldName == "-" {
return ""
}
return fieldName
})
}
type CustomValidator interface {
Validate() error
}
var (
ErrSchemaDecode = errors.New("failed to decode values")
)
type ValidationError []validator.FieldError
func (err ValidationError) Error() string {
msgs := make([]string, len(err))
for i, field := range err {
msgs[i] = fmt.Sprintf(
"validation failed on field %q for constraint %q",
field.Field(),
field.Tag(),
)
}
return strings.Join(msgs, "\n")
}
// Validate validates an object using the global validator.
func Validate(p interface{}) error {
if err := validate.Struct(p); err != nil {
if fields, ok := err.(validator.ValidationErrors); ok {
err = ValidationError(fields)
}
return err
}
if val, ok := p.(CustomValidator); ok {
if err := val.Validate(); err != nil {
return err
}
}
return nil
}
// ValidateQuery decodes and validates an object from query values using the
// global decoder and validator.
func ValidateQuery(p interface{}, v url.Values) error {
if err := decoder.Decode(p, v); err != nil {
return ErrSchemaDecode
}
return Validate(p)
}