-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation.go
70 lines (62 loc) · 1.51 KB
/
validation.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
package chalk
import (
"fmt"
"reflect"
)
func isArray(v any) bool {
return reflect.TypeOf(v).Kind() == reflect.Array
}
func isScalar(v any) bool {
return !isArray(v)
}
func (p OnlineQueryParamsComplete) validateLists() BuilderErrors {
lengthList := -1
for k, v := range p.underlying.inputs {
if !isArray(v) {
continue
}
v = v.([]any)
if lengthList == -1 {
lengthList = len(v.([]any))
} else {
if lengthList != len(v.([]any)) {
return BuilderErrors{
&BuilderError{
Err: fmt.Errorf("all lists must be the same length - found length %d for feature '%s' but expected length %d", len(v.([]any)), k, lengthList),
Type: InvalidRequest,
ParamType: ParamInput,
Feature: nil,
Value: nil,
},
}
}
}
}
return BuilderErrors{}
}
func (p OnlineQueryParamsComplete) validateAllListsOrAllScalars() BuilderErrors {
allScalars := true
allLists := true
for _, v := range p.underlying.inputs {
allScalars = allScalars && isScalar(v)
allLists = allLists && isArray(v)
}
if allScalars || allLists {
return nil
}
return BuilderErrors{
&BuilderError{
Err: fmt.Errorf("inputs must be all scalars or all lists"),
Type: InvalidRequest,
ParamType: ParamInput,
Feature: nil,
Value: nil,
},
}
}
func (p OnlineQueryParamsComplete) validatePostBuild() BuilderErrors {
errors := BuilderErrors{}
errors = append(errors, p.validateLists()...)
errors = append(errors, p.validateAllListsOrAllScalars()...)
return errors
}