forked from gobuffalo/pop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validations.go
92 lines (79 loc) · 1.91 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
package pop
import (
"github.com/markbates/validate"
"github.com/pkg/errors"
)
type beforeValidatable interface {
BeforeValidations(*Connection) error
}
type validateable interface {
Validate(*Connection) (*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(), errors.WithStack(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) {
verrs, err := m.validate(c)
if err != nil {
return verrs, errors.WithStack(err)
}
if x, ok := m.Value.(validateCreateable); ok {
vs, err := x.ValidateCreate(c)
if vs != nil {
verrs.Append(vs)
}
if err != nil {
return verrs, errors.WithStack(err)
}
}
return verrs, err
}
type validateSaveable interface {
ValidateSave(*Connection) (*validate.Errors, error)
}
func (m *Model) validateSave(c *Connection) (*validate.Errors, error) {
verrs, err := m.validate(c)
if err != nil {
return verrs, errors.WithStack(err)
}
if x, ok := m.Value.(validateSaveable); ok {
vs, err := x.ValidateSave(c)
if vs != nil {
verrs.Append(vs)
}
if err != nil {
return verrs, errors.WithStack(err)
}
}
return verrs, err
}
type validateUpdateable interface {
ValidateUpdate(*Connection) (*validate.Errors, error)
}
func (m *Model) validateUpdate(c *Connection) (*validate.Errors, error) {
verrs, err := m.validate(c)
if err != nil {
return verrs, errors.WithStack(err)
}
if x, ok := m.Value.(validateUpdateable); ok {
vs, err := x.ValidateUpdate(c)
if vs != nil {
verrs.Append(vs)
}
if err != nil {
return verrs, errors.WithStack(err)
}
}
return verrs, err
}