/
validators.go
190 lines (148 loc) · 3.51 KB
/
validators.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package models
import (
"encoding/json"
"errors"
"regexp"
"unicode/utf8"
"fmt"
"gopkg.in/mgo.v2/bson"
)
// Email helpers
type Email string
var (
ErrEmptyEmail = errors.New("empty email")
ErrWrongEmailFormat = errors.New("wrong email format")
)
//Long and strange regexp to validate email format.
var emailRegex = regexp.MustCompile(`^(([^<>()\[\]\\.,;:\s@“]+(\.[^<>()\[\]\\.,;:\s@“]+)*)|(“.+“))@((\[[0-9]{1,3}\.
[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$`)
// Validates email
func (e Email) Validate() error {
if len(e) == 0 {
return ErrEmptyEmail
}
if !emailRegex.MatchString(string(e)) {
return ErrWrongEmailFormat
}
return nil
}
// Password helpers
type Password string
var (
ErrEmptyPassword = errors.New("empty password")
ErrWrongPasswordFormat = errors.New("wrong password format")
)
var passwordRegex = regexp.MustCompile(`^[[:graph:]]{5,14}$`)
// Validates password
func (p Password) Validate() error {
if len(p) == 0 {
return ErrEmptyPassword
}
if !passwordRegex.MatchString(string(p)) {
return ErrWrongPasswordFormat
}
return nil
}
// Name
type Name string
var nameRegex = regexp.MustCompile(`^[a-zA-Z](.[ a-zA-Z0-9_-]*)$`)
var (
ErrEmptyName = errors.New("empty name")
ErrWrongNameFormat = errors.New("wrong name format")
)
// Validates names
func (n Name) Validate() error {
if len(n) == 0 {
return ErrEmptyName
}
if !nameRegex.MatchString(string(n)) {
return ErrWrongNameFormat
}
return nil
}
// Text
type Text string
var (
ErrTextTooLong = errors.New("text too long")
MaxTextLen = 500
)
// Validate text
func (t Text) Validate() error {
if utf8.RuneCountInString(string(t)) > MaxTextLen {
return ErrTextTooLong
}
return nil
}
// General Id helpers
type Id struct{ bson.ObjectId }
func (id Id) MarshalJSON() ([]byte, error) {
return json.Marshal(id.Hex())
}
func (id *Id) UnmarshalJSON(data []byte) error {
var result string
err := json.Unmarshal(data, &result)
id.ObjectId = bson.ObjectIdHex(result)
return err
}
func (id Id) GetBSON() (interface{}, error) {
return id.ObjectId, nil
}
func (id *Id) SetBSON(raw bson.Raw) error {
return raw.Unmarshal(&id.ObjectId)
}
var ErrInvalidId = errors.New("invalid id")
// Validates id
func (id Id) Validate() error {
// NOTE: By default id.Valid() checks only id len
// BTW we could pass id like: bson.ObjectId("12_bytes_len")
if !bson.IsObjectIdHex(id.Hex()) {
return ErrInvalidId
}
return nil
}
// AutoId helpers
type AutoId struct{ Id }
var ErrIdMustBeOmitted = errors.New("id must be omitted")
// Validates generated id
func (id AutoId) Validate() error {
if id.Hex() != "" {
return ErrIdMustBeOmitted
}
return nil
}
// New auto Id
func NewAutoId() AutoId {
return AutoId{Id: Id{ObjectId: bson.NewObjectId()}}
}
// RequiredId helpers
type RequiredId struct{ Id }
var ErrIdMustBePresent = errors.New("id must be present")
// Validates required id
func (id RequiredId) Validate() error {
if id.Hex() == "" {
return ErrIdMustBePresent
}
if err := id.Id.Validate(); err != nil {
return err
}
return nil
}
// New required id
func NewRequiredId(hex string) (RequiredId, error) {
if bson.IsObjectIdHex(hex) {
return RequiredId{Id: Id{ObjectId: bson.ObjectIdHex(hex)}}, nil
}
return RequiredId{}, fmt.Errorf("invalid id format: %s", hex)
}
// Optional Id helpers
type OptionalId struct{ Id }
// Validates optional id
func (id OptionalId) Validate() error {
if id.Hex() == "" {
return nil
}
if err := id.Id.Validate(); err != nil {
return err
}
return nil
}