-
-
Notifications
You must be signed in to change notification settings - Fork 242
/
validations.go
147 lines (126 loc) · 3.19 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
package pop
import (
"reflect"
"github.com/gobuffalo/validate/v3"
)
type beforeValidatable interface {
BeforeValidations(*Connection) error
}
type validateable interface {
Validate(*Connection) (*validate.Errors, error)
}
type modelIterableValidator func(*Model) (*validate.Errors, error)
func (m *Model) validate(c *Connection) (*validate.Errors, error) {
if x, ok := m.Value.(beforeValidatable); ok {
if err := x.BeforeValidations(c); err != nil {
return validate.NewErrors(), err
}
}
if x, ok := m.Value.(validateable); ok {
return x.Validate(c)
}
return validate.NewErrors(), nil
}
type validateCreateable interface {
ValidateCreate(*Connection) (*validate.Errors, error)
}
func (m *Model) validateCreate(c *Connection) (*validate.Errors, error) {
return m.iterateAndValidate(func(model *Model) (*validate.Errors, error) {
verrs, err := model.validate(c)
if err != nil {
return verrs, err
}
if x, ok := model.Value.(validateCreateable); ok {
vs, err := x.ValidateCreate(c)
if vs != nil {
verrs.Append(vs)
}
if err != nil {
return verrs, err
}
}
return verrs, err
})
}
func (m *Model) validateAndOnlyCreate(c *Connection) (*validate.Errors, error) {
return m.iterateAndValidate(func(model *Model) (*validate.Errors, error) {
id, err := model.fieldByName("ID")
if err != nil {
return nil, err
}
if !IsZeroOfUnderlyingType(id.Interface()) {
return validate.NewErrors(), nil
}
verrs, err := model.validate(c)
if err != nil {
return verrs, err
}
if x, ok := model.Value.(validateCreateable); ok {
vs, err := x.ValidateCreate(c)
if vs != nil {
verrs.Append(vs)
}
if err != nil {
return verrs, err
}
}
return verrs, err
})
}
type validateSaveable interface {
ValidateSave(*Connection) (*validate.Errors, error)
}
func (m *Model) validateSave(c *Connection) (*validate.Errors, error) {
return m.iterateAndValidate(func(model *Model) (*validate.Errors, error) {
verrs, err := model.validate(c)
if err != nil {
return verrs, err
}
if x, ok := model.Value.(validateSaveable); ok {
vs, err := x.ValidateSave(c)
if vs != nil {
verrs.Append(vs)
}
if err != nil {
return verrs, err
}
}
return verrs, err
})
}
type validateUpdateable interface {
ValidateUpdate(*Connection) (*validate.Errors, error)
}
func (m *Model) validateUpdate(c *Connection) (*validate.Errors, error) {
return m.iterateAndValidate(func(model *Model) (*validate.Errors, error) {
verrs, err := model.validate(c)
if err != nil {
return verrs, err
}
if x, ok := model.Value.(validateUpdateable); ok {
vs, err := x.ValidateUpdate(c)
if vs != nil {
verrs.Append(vs)
}
if err != nil {
return verrs, err
}
}
return verrs, err
})
}
func (m *Model) iterateAndValidate(fn modelIterableValidator) (*validate.Errors, error) {
v := reflect.Indirect(reflect.ValueOf(m.Value))
if v.Kind() == reflect.Slice || v.Kind() == reflect.Array {
for i := 0; i < v.Len(); i++ {
val := v.Index(i)
newModel := NewModel(val.Addr().Interface(), m.ctx)
verrs, err := fn(newModel)
if err != nil || verrs.HasAny() {
return verrs, err
}
}
return validate.NewErrors(), nil
}
return fn(m)
}