-
Notifications
You must be signed in to change notification settings - Fork 100
/
validations.go
215 lines (192 loc) · 7.12 KB
/
validations.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package spec
// CommonValidations describe common JSON-schema validations
type CommonValidations struct {
Maximum *float64 `json:"maximum,omitempty"`
ExclusiveMaximum bool `json:"exclusiveMaximum,omitempty"`
Minimum *float64 `json:"minimum,omitempty"`
ExclusiveMinimum bool `json:"exclusiveMinimum,omitempty"`
MaxLength *int64 `json:"maxLength,omitempty"`
MinLength *int64 `json:"minLength,omitempty"`
Pattern string `json:"pattern,omitempty"`
MaxItems *int64 `json:"maxItems,omitempty"`
MinItems *int64 `json:"minItems,omitempty"`
UniqueItems bool `json:"uniqueItems,omitempty"`
MultipleOf *float64 `json:"multipleOf,omitempty"`
Enum []interface{} `json:"enum,omitempty"`
}
// SetValidations defines all validations for a simple schema.
//
// NOTE: the input is the larger set of validations available for schemas.
// For simple schemas, MinProperties and MaxProperties are ignored.
func (v *CommonValidations) SetValidations(val SchemaValidations) {
v.Maximum = val.Maximum
v.ExclusiveMaximum = val.ExclusiveMaximum
v.Minimum = val.Minimum
v.ExclusiveMinimum = val.ExclusiveMinimum
v.MaxLength = val.MaxLength
v.MinLength = val.MinLength
v.Pattern = val.Pattern
v.MaxItems = val.MaxItems
v.MinItems = val.MinItems
v.UniqueItems = val.UniqueItems
v.MultipleOf = val.MultipleOf
v.Enum = val.Enum
}
type clearedValidation struct {
Validation string
Value interface{}
}
type clearedValidations []clearedValidation
func (c clearedValidations) apply(cbs []func(string, interface{})) {
for _, cb := range cbs {
for _, cleared := range c {
cb(cleared.Validation, cleared.Value)
}
}
}
// ClearNumberValidations clears all number validations.
//
// Some callbacks may be set by the caller to capture changed values.
func (v *CommonValidations) ClearNumberValidations(cbs ...func(string, interface{})) {
done := make(clearedValidations, 0, 5)
defer func() {
done.apply(cbs)
}()
if v.Minimum != nil {
done = append(done, clearedValidation{Validation: "minimum", Value: v.Minimum})
v.Minimum = nil
}
if v.Maximum != nil {
done = append(done, clearedValidation{Validation: "maximum", Value: v.Maximum})
v.Maximum = nil
}
if v.ExclusiveMaximum {
done = append(done, clearedValidation{Validation: "exclusiveMaximum", Value: v.ExclusiveMaximum})
v.ExclusiveMaximum = false
}
if v.ExclusiveMinimum {
done = append(done, clearedValidation{Validation: "exclusiveMinimum", Value: v.ExclusiveMinimum})
v.ExclusiveMinimum = false
}
if v.MultipleOf != nil {
done = append(done, clearedValidation{Validation: "multipleOf", Value: v.MultipleOf})
v.MultipleOf = nil
}
}
// ClearStringValidations clears all string validations.
//
// Some callbacks may be set by the caller to capture changed values.
func (v *CommonValidations) ClearStringValidations(cbs ...func(string, interface{})) {
done := make(clearedValidations, 0, 3)
defer func() {
done.apply(cbs)
}()
if v.Pattern != "" {
done = append(done, clearedValidation{Validation: "pattern", Value: v.Pattern})
v.Pattern = ""
}
if v.MinLength != nil {
done = append(done, clearedValidation{Validation: "minLength", Value: v.MinLength})
v.MinLength = nil
}
if v.MaxLength != nil {
done = append(done, clearedValidation{Validation: "maxLength", Value: v.MaxLength})
v.MaxLength = nil
}
}
// ClearArrayValidations clears all array validations.
//
// Some callbacks may be set by the caller to capture changed values.
func (v *CommonValidations) ClearArrayValidations(cbs ...func(string, interface{})) {
done := make(clearedValidations, 0, 3)
defer func() {
done.apply(cbs)
}()
if v.MaxItems != nil {
done = append(done, clearedValidation{Validation: "maxItems", Value: v.MaxItems})
v.MaxItems = nil
}
if v.MinItems != nil {
done = append(done, clearedValidation{Validation: "minItems", Value: v.MinItems})
v.MinItems = nil
}
if v.UniqueItems {
done = append(done, clearedValidation{Validation: "uniqueItems", Value: v.UniqueItems})
v.UniqueItems = false
}
}
// Validations returns a clone of the validations for a simple schema.
//
// NOTE: in the context of simple schema objects, MinProperties, MaxProperties
// and PatternProperties remain unset.
func (v CommonValidations) Validations() SchemaValidations {
return SchemaValidations{
CommonValidations: v,
}
}
// HasNumberValidations indicates if the validations are for numbers or integers
func (v CommonValidations) HasNumberValidations() bool {
return v.Maximum != nil || v.Minimum != nil || v.MultipleOf != nil
}
// HasStringValidations indicates if the validations are for strings
func (v CommonValidations) HasStringValidations() bool {
return v.MaxLength != nil || v.MinLength != nil || v.Pattern != ""
}
// HasArrayValidations indicates if the validations are for arrays
func (v CommonValidations) HasArrayValidations() bool {
return v.MaxItems != nil || v.MinItems != nil || v.UniqueItems
}
// HasEnum indicates if the validation includes some enum constraint
func (v CommonValidations) HasEnum() bool {
return len(v.Enum) > 0
}
// SchemaValidations describes the validation properties of a schema
//
// NOTE: at this moment, this is not embedded in SchemaProps because this would induce a breaking change
// in the exported members: all initializers using litterals would fail.
type SchemaValidations struct {
CommonValidations
PatternProperties SchemaProperties `json:"patternProperties,omitempty"`
MaxProperties *int64 `json:"maxProperties,omitempty"`
MinProperties *int64 `json:"minProperties,omitempty"`
}
// HasObjectValidations indicates if the validations are for objects
func (v SchemaValidations) HasObjectValidations() bool {
return v.MaxProperties != nil || v.MinProperties != nil || v.PatternProperties != nil
}
// SetValidations for schema validations
func (v *SchemaValidations) SetValidations(val SchemaValidations) {
v.CommonValidations.SetValidations(val)
v.PatternProperties = val.PatternProperties
v.MaxProperties = val.MaxProperties
v.MinProperties = val.MinProperties
}
// Validations for a schema
func (v SchemaValidations) Validations() SchemaValidations {
val := v.CommonValidations.Validations()
val.PatternProperties = v.PatternProperties
val.MinProperties = v.MinProperties
val.MaxProperties = v.MaxProperties
return val
}
// ClearObjectValidations returns a clone of the validations with all object validations cleared.
//
// Some callbacks may be set by the caller to capture changed values.
func (v *SchemaValidations) ClearObjectValidations(cbs ...func(string, interface{})) {
done := make(clearedValidations, 0, 3)
defer func() {
done.apply(cbs)
}()
if v.MaxProperties != nil {
done = append(done, clearedValidation{Validation: "maxProperties", Value: v.MaxProperties})
v.MaxProperties = nil
}
if v.MinProperties != nil {
done = append(done, clearedValidation{Validation: "minProperties", Value: v.MinProperties})
v.MinProperties = nil
}
if v.PatternProperties != nil {
done = append(done, clearedValidation{Validation: "patternProperties", Value: v.PatternProperties})
v.PatternProperties = nil
}
}