aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/Azure/azure-storage-blob-go/2018-03-28/azblob/zz_generated_validation.go
blob: 98a2614e606de98ab1b18e69c3ba722b015f3d13 (plain) (blame)
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package azblob

// Code generated by Microsoft (R) AutoRest Code Generator.
// Changes may cause incorrect behavior and will be lost if the code is regenerated.

import (
    "fmt"
    "github.com/Azure/azure-pipeline-go/pipeline"
    "reflect"
    "regexp"
    "strings"
)

// Constraint stores constraint name, target field name
// Rule and chain validations.
type constraint struct {
    // Target field name for validation.
    target string

    // Constraint name e.g. minLength, MaxLength, Pattern, etc.
    name string

    // Rule for constraint e.g. greater than 10, less than 5 etc.
    rule interface{}

    // Chain validations for struct type
    chain []constraint
}

// Validation stores parameter-wise validation.
type validation struct {
    targetValue interface{}
    constraints []constraint
}

// Constraint list
const (
    empty            = "Empty"
    null             = "Null"
    readOnly         = "ReadOnly"
    pattern          = "Pattern"
    maxLength        = "MaxLength"
    minLength        = "MinLength"
    maxItems         = "MaxItems"
    minItems         = "MinItems"
    multipleOf       = "MultipleOf"
    uniqueItems      = "UniqueItems"
    inclusiveMaximum = "InclusiveMaximum"
    exclusiveMaximum = "ExclusiveMaximum"
    exclusiveMinimum = "ExclusiveMinimum"
    inclusiveMinimum = "InclusiveMinimum"
)

// Validate method validates constraints on parameter
// passed in validation array.
func validate(m []validation) error {
    for _, item := range m {
        v := reflect.ValueOf(item.targetValue)
        for _, constraint := range item.constraints {
            var err error
            switch v.Kind() {
            case reflect.Ptr:
                err = validatePtr(v, constraint)
            case reflect.String:
                err = validateString(v, constraint)
            case reflect.Struct:
                err = validateStruct(v, constraint)
            case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
                err = validateInt(v, constraint)
            case reflect.Float32, reflect.Float64:
                err = validateFloat(v, constraint)
            case reflect.Array, reflect.Slice, reflect.Map:
                err = validateArrayMap(v, constraint)
            default:
                err = createError(v, constraint, fmt.Sprintf("unknown type %v", v.Kind()))
            }
            if err != nil {
                return err
            }
        }
    }
    return nil
}

func validateStruct(x reflect.Value, v constraint, name ...string) error {
    //Get field name from target name which is in format a.b.c
    s := strings.Split(v.target, ".")
    f := x.FieldByName(s[len(s)-1])
    if isZero(f) {
        return createError(x, v, fmt.Sprintf("field %q doesn't exist", v.target))
    }
    err := validate([]validation{
        {
            targetValue: getInterfaceValue(f),
            constraints: []constraint{v},
        },
    })
    return err
}

func validatePtr(x reflect.Value, v constraint) error {
    if v.name == readOnly {
        if !x.IsNil() {
            return createError(x.Elem(), v, "readonly parameter; must send as nil or empty in request")
        }
        return nil
    }
    if x.IsNil() {
        return checkNil(x, v)
    }
    if v.chain != nil {
        return validate([]validation{
            {
                targetValue: getInterfaceValue(x.Elem()),
                constraints: v.chain,
            },
        })
    }
    return nil
}

func validateInt(x reflect.Value, v constraint) error {
    i := x.Int()
    r, ok := v.rule.(int)
    if !ok {
        return createError(x, v, fmt.Sprintf("rule must be integer value for %v constraint; got: %v", v.name, v.rule))
    }
    switch v.name {
    case multipleOf:
        if i%int64(r) != 0 {
            return createError(x, v, fmt.Sprintf("value must be a multiple of %v", r))
        }
    case exclusiveMinimum:
        if i <= int64(r) {
            return createError(x, v, fmt.Sprintf("value must be greater than %v", r))
        }
    case exclusiveMaximum:
        if i >= int64(r) {
            return createError(x, v, fmt.Sprintf("value must be less than %v", r))
        }
    case inclusiveMinimum:
        if i < int64(r) {
            return createError(x, v, fmt.Sprintf("value must be greater than or equal to %v", r))
        }
    case inclusiveMaximum:
        if i > int64(r) {
            return createError(x, v, fmt.Sprintf("value must be less than or equal to %v", r))
        }
    default:
        return createError(x, v, fmt.Sprintf("constraint %v is not applicable for type integer", v.name))
    }
    return nil
}

func validateFloat(x reflect.Value, v constraint) error {
    f := x.Float()
    r, ok := v.rule.(float64)
    if !ok {
        return createError(x, v, fmt.Sprintf("rule must be float value for %v constraint; got: %v", v.name, v.rule))
    }
    switch v.name {
    case exclusiveMinimum:
        if f <= r {
            return createError(x, v, fmt.Sprintf("value must be greater than %v", r))
        }
    case exclusiveMaximum:
        if f >= r {
            return createError(x, v, fmt.Sprintf("value must be less than %v", r))
        }
    case inclusiveMinimum:
        if f < r {
            return createError(x, v, fmt.Sprintf("value must be greater than or equal to %v", r))
        }
    case inclusiveMaximum:
        if f > r {
            return createError(x, v, fmt.Sprintf("value must be less than or equal to %v", r))
        }
    default:
        return createError(x, v, fmt.Sprintf("constraint %s is not applicable for type float", v.name))
    }
    return nil
}

func validateString(x reflect.Value, v constraint) error {
    s := x.String()
    switch v.name {
    case empty:
        if len(s) == 0 {
            return checkEmpty(x, v)
        }
    case pattern:
        reg, err := regexp.Compile(v.rule.(string))
        if err != nil {
            return createError(x, v, err.Error())
        }
        if !reg.MatchString(s) {
            return createError(x, v, fmt.Sprintf("value doesn't match pattern %v", v.rule))
        }
    case maxLength:
        if _, ok := v.rule.(int); !ok {
            return createError(x, v, fmt.Sprintf("rule must be integer value for %v constraint; got: %v", v.name, v.rule))
        }
        if len(s) > v.rule.(int) {
            return createError(x, v, fmt.Sprintf("value length must be less than %v", v.rule))
        }
    case minLength:
        if _, ok := v.rule.(int); !ok {
            return createError(x, v, fmt.Sprintf("rule must be integer value for %v constraint; got: %v", v.name, v.rule))
        }
        if len(s) < v.rule.(int) {
            return createError(x, v, fmt.Sprintf("value length must be greater than %v", v.rule))
        }
    case readOnly:
        if len(s) > 0 {
            return createError(reflect.ValueOf(s), v, "readonly parameter; must send as nil or empty in request")
        }
    default:
        return createError(x, v, fmt.Sprintf("constraint %s is not applicable to string type", v.name))
    }
    if v.chain != nil {
        return validate([]validation{
            {
                targetValue: getInterfaceValue(x),
                constraints: v.chain,
            },
        })
    }
    return nil
}

func validateArrayMap(x reflect.Value, v constraint) error {
    switch v.name {
    case null:
        if x.IsNil() {
            return checkNil(x, v)
        }
    case empty:
        if x.IsNil() || x.Len() == 0 {
            return checkEmpty(x, v)
        }
    case maxItems:
        if _, ok := v.rule.(int); !ok {
            return createError(x, v, fmt.Sprintf("rule must be integer for %v constraint; got: %v", v.name, v.rule))
        }
        if x.Len() > v.rule.(int) {
            return createError(x, v, fmt.Sprintf("maximum item limit is %v; got: %v", v.rule, x.Len()))
        }
    case minItems:
        if _, ok := v.rule.(int); !ok {
            return createError(x, v, fmt.Sprintf("rule must be integer for %v constraint; got: %v", v.name, v.rule))
        }
        if x.Len() < v.rule.(int) {
            return createError(x, v, fmt.Sprintf("minimum item limit is %v; got: %v", v.rule, x.Len()))
        }
    case uniqueItems:
        if x.Kind() == reflect.Array || x.Kind() == reflect.Slice {
            if !checkForUniqueInArray(x) {
                return createError(x, v, fmt.Sprintf("all items in parameter %q must be unique; got:%v", v.target, x))
            }
        } else if x.Kind() == reflect.Map {
            if !checkForUniqueInMap(x) {
                return createError(x, v, fmt.Sprintf("all items in parameter %q must be unique; got:%v", v.target, x))
            }
        } else {
            return createError(x, v, fmt.Sprintf("type must be array, slice or map for constraint %v; got: %v", v.name, x.Kind()))
        }
    case readOnly:
        if x.Len() != 0 {
            return createError(x, v, "readonly parameter; must send as nil or empty in request")
        }
    case pattern:
        reg, err := regexp.Compile(v.rule.(string))
        if err != nil {
            return createError(x, v, err.Error())
        }
        keys := x.MapKeys()
        for _, k := range keys {
            if !reg.MatchString(k.String()) {
                return createError(k, v, fmt.Sprintf("map key doesn't match pattern %v", v.rule))
            }
        }
    default:
        return createError(x, v, fmt.Sprintf("constraint %v is not applicable to array, slice and map type", v.name))
    }
    if v.chain != nil {
        return validate([]validation{
            {
                targetValue: getInterfaceValue(x),
                constraints: v.chain,
            },
        })
    }
    return nil
}

func checkNil(x reflect.Value, v constraint) error {
    if _, ok := v.rule.(bool); !ok {
        return createError(x, v, fmt.Sprintf("rule must be bool value for %v constraint; got: %v", v.name, v.rule))
    }
    if v.rule.(bool) {
        return createError(x, v, "value can not be null; required parameter")
    }
    return nil
}

func checkEmpty(x reflect.Value, v constraint) error {
    if _, ok := v.rule.(bool); !ok {
        return createError(x, v, fmt.Sprintf("rule must be bool value for %v constraint; got: %v", v.name, v.rule))
    }
    if v.rule.(bool) {
        return createError(x, v, "value can not be null or empty; required parameter")
    }
    return nil
}

func checkForUniqueInArray(x reflect.Value) bool {
    if x == reflect.Zero(reflect.TypeOf(x)) || x.Len() == 0 {
        return false
    }
    arrOfInterface := make([]interface{}, x.Len())
    for i := 0; i < x.Len(); i++ {
        arrOfInterface[i] = x.Index(i).Interface()
    }
    m := make(map[interface{}]bool)
    for _, val := range arrOfInterface {
        if m[val] {
            return false
        }
        m[val] = true
    }
    return true
}

func checkForUniqueInMap(x reflect.Value) bool {
    if x == reflect.Zero(reflect.TypeOf(x)) || x.Len() == 0 {
        return false
    }
    mapOfInterface := make(map[interface{}]interface{}, x.Len())
    keys := x.MapKeys()
    for _, k := range keys {
        mapOfInterface[k.Interface()] = x.MapIndex(k).Interface()
    }
    m := make(map[interface{}]bool)
    for _, val := range mapOfInterface {
        if m[val] {
            return false
        }
        m[val] = true
    }
    return true
}

func getInterfaceValue(x reflect.Value) interface{} {
    if x.Kind() == reflect.Invalid {
        return nil
    }
    return x.Interface()
}

func isZero(x interface{}) bool {
    return x == reflect.Zero(reflect.TypeOf(x)).Interface()
}

func createError(x reflect.Value, v constraint, message string) error {
    return pipeline.NewError(nil, fmt.Sprintf("validation failed: parameter=%s constraint=%s value=%#v details: %s",
        v.target, v.name, getInterfaceValue(x), message))
}