-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvalidator_options.go
191 lines (167 loc) · 7.02 KB
/
validator_options.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package valix
type Option interface {
Apply(on *Validator) error
}
// ValidatorForOptions is used by ValidatorFor and MustCompileValidatorFor to set
// the initial overall validator for the struct
//
// Note: This struct is not deprecated, but is retained for backward compatibility - it is
// now easier to use individual Option's when using ValidatorFor()
type ValidatorForOptions struct {
// IgnoreUnknownProperties is whether to ignore unknown properties (default false)
//
// Set this to `true` if you want to allow unknown properties
IgnoreUnknownProperties bool
// Constraints is an optional slice of Constraint items to be checked on the object/array
//
// * These are checked in the order specified and prior to property validator & unknown property checks
Constraints Constraints
// AllowNullJson forces validator to accept a request body that is null JSON (i.e. a body containing just `null`)
AllowNullJson bool
// AllowArray denotes, when true (default is false), that this validator will allow a JSON array - where each
// item in the array can be validated as an object
AllowArray bool
// DisallowObject denotes, when set to true, that this validator will disallow JSON objects - i.e. that it
// expects JSON arrays (in which case the AllowArray should also be set to true)
DisallowObject bool
// StopOnFirst if set, instructs the validator to stop at the first violation found
StopOnFirst bool
// UseNumber forces RequestValidate method to use json.Number when decoding request body
UseNumber bool
// OrderedPropertyChecks determines whether properties should be checked in order - when set to true, properties
// are sorted by PropertyValidator.Order and property name
//
// When this is set to false (default) properties are checked in the order in which they appear in the properties map -
// which is unpredictable
OrderedPropertyChecks bool
// OasInfo is additional information (for OpenAPI Specification)
OasInfo *OasInfo
}
func (o *ValidatorForOptions) Apply(on *Validator) error {
on.IgnoreUnknownProperties = o.IgnoreUnknownProperties
on.Constraints = append(on.Constraints, o.Constraints...)
on.AllowNullJson = o.AllowNullJson
on.UseNumber = o.UseNumber
on.AllowArray = o.AllowArray
on.DisallowObject = o.DisallowObject
on.StopOnFirst = o.StopOnFirst
on.OrderedPropertyChecks = o.OrderedPropertyChecks
if o.OasInfo != nil {
on.OasInfo = o.OasInfo
}
return nil
}
var (
// OptionIgnoreOasTags option for ValidatorFor - ignores oas tags on struct fields
OptionIgnoreOasTags Option = _OptionIgnoreOasTags
// OptionConstraints option for ValidatorFor - adds constraints to the Validator
OptionConstraints = _OptionConstraints
// OptionIgnoreUnknownProperties option for ValidatorFor - sets Validator to ignore unknown properties
OptionIgnoreUnknownProperties Option = _OptionIgnoreUnknownProperties
// OptionDisallowUnknownProperties option for ValidatorFor - sets Validator to not ignore unknown properties
OptionDisallowUnknownProperties Option = _OptionDisallowUnknownProperties
// OptionAllowNullJson option for ValidatorFor - sets Validator to allow JSON null
OptionAllowNullJson Option = _OptionAllowNullJson
// OptionDisallowNullJson option for ValidatorFor - sets Validator to not allow JSON null
OptionDisallowNullJson Option = _OptionDisallowNullJson
// OptionAllowArray option for ValidatorFor - sets Validator to allow JSON array
OptionAllowArray Option = _OptionAllowArray
// OptionDisallowArray option for ValidatorFor - sets Validator to not allow JSON array
OptionDisallowArray Option = _OptionDisallowArray
// OptionAllowObject option for ValidatorFor - sets Validator to allow JSON object
OptionAllowObject Option = _OptionAllowObject
// OptionDisallowObject option for ValidatorFor - sets Validator to not allow JSON object
OptionDisallowObject Option = _OptionDisallowObject
// OptionStopOnFirst option for ValidatorFor - sets Validator to stop on first violation
OptionStopOnFirst Option = _OptionStopOnFirst
// OptionDontStopOnFirst option for ValidatorFor - sets Validator to not stop on first violation
OptionDontStopOnFirst Option = _OptionDontStopOnFirst
// OptionOrderedPropertyChecks option for ValidatorFor - sets Validator to do ordered property checks
OptionOrderedPropertyChecks Option = _OptionOrderedPropertyChecks
// OptionUnOrderedPropertyChecks option for ValidatorFor - sets Validator to not do ordered property checks
// Note that if the validator has any properties with a non-zero order, ordered property checks are always carried out
OptionUnOrderedPropertyChecks Option = _OptionUnOrderedPropertyChecks
)
var (
_OptionIgnoreOasTags = &optionIgnoreOasTags{}
_OptionConstraints = func(constraints ...Constraint) Option {
return &optionConstraints{
constraints: constraints,
}
}
_OptionIgnoreUnknownProperties = &optionIgnoreUnknownProperties{true}
_OptionDisallowUnknownProperties = &optionIgnoreUnknownProperties{false}
_OptionAllowNullJson = &optionAllowNullJson{true}
_OptionDisallowNullJson = &optionAllowNullJson{false}
_OptionAllowArray = &optionAllowArray{true}
_OptionDisallowArray = &optionAllowArray{false}
_OptionAllowObject = &optionDisallowObject{false}
_OptionDisallowObject = &optionDisallowObject{true}
_OptionStopOnFirst = &optionStopOnFirst{true}
_OptionDontStopOnFirst = &optionStopOnFirst{false}
_OptionOrderedPropertyChecks = &optionOrderedPropertyChecks{true}
_OptionUnOrderedPropertyChecks = &optionOrderedPropertyChecks{false}
)
type optionIgnoreOasTags struct {
}
func (o *optionIgnoreOasTags) Apply(on *Validator) error {
return nil
}
type optionIgnoreUnknownProperties struct {
setting bool
}
func (o *optionIgnoreUnknownProperties) Apply(on *Validator) error {
on.IgnoreUnknownProperties = o.setting
return nil
}
type optionConstraints struct {
constraints Constraints
}
func (o *optionConstraints) Apply(on *Validator) error {
on.Constraints = append(on.Constraints, o.constraints...)
return nil
}
type optionAllowNullJson struct {
setting bool
}
func (o *optionAllowNullJson) Apply(on *Validator) error {
on.AllowNullJson = o.setting
return nil
}
type optionAllowArray struct {
setting bool
}
func (o *optionAllowArray) Apply(on *Validator) error {
on.AllowArray = o.setting
return nil
}
type optionDisallowObject struct {
setting bool
}
func (o *optionDisallowObject) Apply(on *Validator) error {
on.DisallowObject = o.setting
return nil
}
type optionStopOnFirst struct {
setting bool
}
func (o *optionStopOnFirst) Apply(on *Validator) error {
on.StopOnFirst = o.setting
return nil
}
// OptionUseNumber option for ValidatorFor - sets Validator to use json.Number when decoding requests
var OptionUseNumber Option = &optionUseNumber{true}
type optionUseNumber struct {
setting bool
}
func (o *optionUseNumber) Apply(on *Validator) error {
on.UseNumber = o.setting
return nil
}
type optionOrderedPropertyChecks struct {
setting bool
}
func (o *optionOrderedPropertyChecks) Apply(on *Validator) error {
on.OrderedPropertyChecks = o.setting
return nil
}