-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathanyof.go
105 lines (91 loc) · 2.37 KB
/
anyof.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
package schema
// AnyOf validates if any of the sub field validators validates. If any of the
// sub field validators implements the FieldSerializer interface, the *first*
// implementation which does not error will be used.
type AnyOf []FieldValidator
// Compile implements the Compiler interface.
func (v AnyOf) Compile(rc ReferenceChecker) error {
for _, sv := range v {
if c, ok := sv.(Compiler); ok {
if err := c.Compile(rc); err != nil {
return err
}
}
}
return nil
}
// ValidateQuery implements schema.FieldQueryValidator interface.
func (v AnyOf) ValidateQuery(value interface{}) (interface{}, error) {
var errs ErrorSlice
for _, validator := range v {
var err error
var val interface{}
if validatorQuery, ok := validator.(FieldQueryValidator); ok {
val, err = validatorQuery.ValidateQuery(value)
} else {
val, err = validator.Validate(value)
}
if err == nil {
return val, nil
}
errs = errs.Append(err)
}
if len(errs) > 0 {
return nil, errs
}
return nil, nil
}
// Validate ensures that at least one sub-validator validates.
func (v AnyOf) Validate(value interface{}) (interface{}, error) {
var errs ErrorSlice
for _, validator := range v {
value, err := validator.Validate(value)
if err == nil {
return value, nil
}
errs = errs.Append(err)
}
if len(errs) > 0 {
return nil, errs
}
return nil, nil
}
// Serialize attempts to serialize the value using the first available
// FieldSerializer which does not return an error. If no appropriate serializer
// is found, the input value is returned.
func (v AnyOf) Serialize(value interface{}) (interface{}, error) {
for _, serializer := range v {
s, ok := serializer.(FieldSerializer)
if !ok {
continue
}
v, err := s.Serialize(value)
if err != nil {
continue
}
return v, nil
}
return value, nil
}
// LessFunc implements the FieldComparator interface, and returns the first
// non-nil LessFunc or nil.
func (v AnyOf) LessFunc() LessFunc {
for _, comparable := range v {
if fc, ok := comparable.(FieldComparator); ok {
if less := fc.LessFunc(); less != nil {
return less
}
}
}
return nil
}
// GetField implements the FieldGetter interface. Note that it will return the
// first matching field only.
func (v AnyOf) GetField(name string) *Field {
for _, obj := range v {
if fg, ok := obj.(FieldGetter); ok {
return fg.GetField(name)
}
}
return nil
}