-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidator.go
112 lines (87 loc) · 2.82 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Package validator provides validation methods for validating structures.
package validator
import (
"github.com/typerandom/validator/core"
"github.com/typerandom/validator/validators"
"sync"
)
type Validator interface {
// The tag that is used for the field's display name.
// Default: Empty string that defaults to the field name.
SetDisplayNameTag(name string)
// Locale retrieves the locale for this validator.
Locale() *core.Locale
// Register registers a validator by name.
Register(name string, validator core.ValidatorFn)
// Validate validates fields of a structure, or structures of a map, slice or array.
Validate(value interface{}) core.ErrorList
// Copy deep copies the validator and returns a new instance.
Copy() Validator
}
// Validator represents a validator with it's own configuration set.
type validator struct {
displayNameTag *string
registry core.ValidatorRegistry
locale *core.Locale
lock sync.Mutex
}
func newValidator() *validator {
validator := &validator{
registry: core.NewValidatorRegistry(),
locale: core.NewLocale(),
}
validators.RegisterDefaultLocale(validator.locale)
validators.RegisterDefaultValidators(validator.registry)
return validator
}
func (this *validator) Copy() Validator {
newValidator := newValidator()
newValidator.displayNameTag = this.displayNameTag
newValidator.locale = this.locale.Copy()
newValidator.registry = this.registry
return newValidator
}
func (this *validator) Locale() *core.Locale {
return this.locale
}
func (this *validator) SetDisplayNameTag(tagName string) {
if len(tagName) == 0 {
this.displayNameTag = nil
} else {
this.displayNameTag = &tagName
}
}
func (this *validator) Register(name string, validator core.ValidatorFn) {
this.registry.Register(name, validator)
}
func (this *validator) Validate(value interface{}) core.ErrorList {
context := &context{
validator: this,
}
walkValidate(context, value, nil)
return context.errors
}
// CheckSyntax checks the validate tag syntax of a structure.
func CheckSyntax(value interface{}) error {
if _, err := core.GetStructFields(value, "validator", nil); err != nil {
return err
}
return nil
}
// New creates a new validator.
func New() Validator {
return newValidator()
}
// Default retrieves the default global validator (singleton).
// It's the same validator that is used when you call the global Validate() or Register() method.
func Default() Validator {
return getGlobalValidator()
}
// Register registers a validator method by name on the default validator.
func Register(name string, validator core.ValidatorFn) {
getGlobalValidator().Register(name, validator)
}
// Validate validates fields of a structure, or structures of a map, slice or array using the default validator.
func Validate(value interface{}) core.ErrorList {
return getGlobalValidator().Validate(value)
}