-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvalidate.go
165 lines (141 loc) · 3.13 KB
/
validate.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package gocqrs
import (
"errors"
"github.com/diegogub/lib"
"gopkg.in/asaskevich/govalidator.v4"
"log"
)
type Validator interface {
GetName() string
Validate(e Entity) error
}
type SimpleValidator struct {
validators []*EntityProperty
}
func (sv SimpleValidator) GetName() string {
return "simple-validator"
}
func (sv SimpleValidator) Validate(e Entity) error {
var err error
for _, v := range sv.validators {
for _, ep := range v.Validations {
err = ep(e.Data[v.Name])
if err != nil {
return err
}
}
}
return err
}
func NewSimpleValidator(ep ...*EntityProperty) *SimpleValidator {
var sv SimpleValidator
sv.validators = make([]*EntityProperty, 0)
for _, e := range ep {
sv.validators = append(sv.validators, e)
}
return &sv
}
type EntityProperty struct {
Name string
Validations []func(interface{}) error
}
func NewProperty(name string) *EntityProperty {
var ep EntityProperty
if name == "" {
log.Fatal("invalid property")
}
ep.Name = name
ep.Validations = make([]func(interface{}) error, 0)
return &ep
}
func (ep *EntityProperty) String() *EntityProperty {
valid := func(i interface{}) error {
switch i.(type) {
case string:
default:
return errors.New(ep.Name + " invalid string")
}
return nil
}
ep.Validations = append(ep.Validations, valid)
return ep
}
func (ep *EntityProperty) ID() *EntityProperty {
valid := func(i interface{}) error {
switch i.(type) {
case string:
err := lib.ValidID(i.(string), true)
if err != nil {
return errors.New(ep.Name + " invalid id")
}
default:
return errors.New(ep.Name + " invalid id")
}
return nil
}
ep.Validations = append(ep.Validations, valid)
return ep
}
func (ep *EntityProperty) Email() *EntityProperty {
valid := func(i interface{}) error {
switch i.(type) {
case string:
isEmail := govalidator.IsEmail(i.(string))
if !isEmail {
return errors.New(ep.Name + " invalid email")
}
default:
return errors.New(ep.Name + " invalid email")
}
return nil
}
ep.Validations = append(ep.Validations, valid)
return ep
}
func (ep *EntityProperty) IP() *EntityProperty {
valid := func(i interface{}) error {
switch i.(type) {
case string:
isIP := govalidator.IsIP(i.(string))
if !isIP {
return errors.New(ep.Name + " invalid IP")
}
default:
return errors.New(ep.Name + " invalid IP")
}
return nil
}
ep.Validations = append(ep.Validations, valid)
return ep
}
func (ep *EntityProperty) URL() *EntityProperty {
valid := func(i interface{}) error {
switch i.(type) {
case string:
isURL := govalidator.IsURL(i.(string))
if !isURL {
return errors.New(ep.Name + " invalid url")
}
default:
return errors.New(ep.Name + " invalid url")
}
return nil
}
ep.Validations = append(ep.Validations, valid)
return ep
}
func (ep *EntityProperty) NotNull() *EntityProperty {
valid := func(i interface{}) error {
switch v := i.(type) {
case string, []byte:
if len(v.(string)) == 0 {
return errors.New(ep.Name + " invalid value")
}
default:
return errors.New(ep.Name + " invalid string")
}
return nil
}
ep.Validations = append(ep.Validations, valid)
return ep
}