-
Notifications
You must be signed in to change notification settings - Fork 16
/
validate.go
274 lines (199 loc) · 6.51 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package address
import (
"regexp"
"strings"
"github.com/hashicorp/go-multierror"
)
// Validate checks and address to determine if it is valid.
// If you want to create valid addresses, the `address.NewValid()` function does it in one call.
func Validate(address Address) error {
var result *multierror.Error
if !generated.hasCountry(address.Country) {
result = multierror.Append(result, ErrInvalidCountryCode)
return result
}
countryData := generated.getCountry(address.Country)
if err := checkRequiredFields(address, countryData.RequiredFields); err != nil {
result = multierror.Append(result, err)
}
if err := checkAllowedFields(address, countryData.AllowedFields); err != nil {
result = multierror.Append(result, err)
}
if len(countryData.AdministrativeAreas) > 0 {
if administrativeAreaData, ok := countryData.AdministrativeAreas[countryData.DefaultLanguage]; ok {
err := checkSubdivisions(address, administrativeAreaData)
if err != nil {
result = multierror.Append(result, err.(*multierror.Error).Errors...)
}
}
}
if address.PostCode != "" {
err := checkPostCode(address, countryData.PostCodeRegex)
if err != nil {
result = multierror.Append(result, err.(*multierror.Error).Errors...)
}
}
return result.ErrorOrNil()
}
func checkRequiredFields(address Address, requiredFields map[Field]struct{}) error {
errors := ErrMissingRequiredFields{
country: address.Country,
}
for field := range requiredFields {
switch field {
case Name:
if strings.TrimSpace(address.Name) == "" {
errors.Fields = append(errors.Fields, Name)
}
case Organization:
if strings.TrimSpace(address.Organization) == "" {
errors.Fields = append(errors.Fields, Organization)
}
case StreetAddress:
isEmpty := true
for _, addressLine := range address.StreetAddress {
if strings.TrimSpace(addressLine) != "" {
isEmpty = false
break
}
}
if isEmpty {
errors.Fields = append(errors.Fields, StreetAddress)
}
case DependentLocality:
if strings.TrimSpace(address.DependentLocality) == "" {
errors.Fields = append(errors.Fields, DependentLocality)
}
case Locality:
if strings.TrimSpace(address.Locality) == "" {
errors.Fields = append(errors.Fields, Locality)
}
case AdministrativeArea:
if strings.TrimSpace(address.AdministrativeArea) == "" {
errors.Fields = append(errors.Fields, AdministrativeArea)
}
case PostCode:
if strings.TrimSpace(address.PostCode) == "" {
errors.Fields = append(errors.Fields, PostCode)
}
case SortingCode:
if strings.TrimSpace(address.SortingCode) == "" {
errors.Fields = append(errors.Fields, SortingCode)
}
}
}
if len(errors.Fields) <= 0 {
return nil
}
return errors
}
func checkAllowedFields(address Address, allowedFields map[Field]struct{}) error {
errors := ErrUnsupportedFields{
country: address.Country,
}
if _, ok := allowedFields[Name]; address.Name != "" && !ok {
errors.Fields = append(errors.Fields, Name)
}
if _, ok := allowedFields[Organization]; address.Organization != "" && !ok {
errors.Fields = append(errors.Fields, Organization)
}
if _, ok := allowedFields[StreetAddress]; len(address.StreetAddress) > 0 && !ok {
errors.Fields = append(errors.Fields, StreetAddress)
}
if _, ok := allowedFields[DependentLocality]; address.DependentLocality != "" && !ok {
errors.Fields = append(errors.Fields, DependentLocality)
}
if _, ok := allowedFields[Locality]; address.Locality != "" && !ok {
errors.Fields = append(errors.Fields, Locality)
}
if _, ok := allowedFields[AdministrativeArea]; address.AdministrativeArea != "" && !ok {
errors.Fields = append(errors.Fields, AdministrativeArea)
}
if _, ok := allowedFields[PostCode]; address.PostCode != "" && !ok {
errors.Fields = append(errors.Fields, PostCode)
}
if _, ok := allowedFields[SortingCode]; address.SortingCode != "" && !ok {
errors.Fields = append(errors.Fields, SortingCode)
}
if len(errors.Fields) <= 0 {
return nil
}
return errors
}
func checkSubdivisions(address Address, administrativeAreaData []administrativeArea) error {
var err *multierror.Error
if address.AdministrativeArea != "" {
adminAreaIdx := -1
for i, adminArea := range administrativeAreaData {
if adminArea.ID == address.AdministrativeArea {
adminAreaIdx = i
}
}
if adminAreaIdx == -1 {
err = multierror.Append(err, ErrInvalidAdministrativeArea)
return err.ErrorOrNil()
}
localities := administrativeAreaData[adminAreaIdx].Localities
localityIdx := -1
if address.Locality == "" || len(localities) <= 0 {
return err.ErrorOrNil()
}
for i, locality := range localities {
if locality.ID == address.Locality {
localityIdx = i
}
}
if localityIdx == -1 {
err = multierror.Append(err, ErrInvalidLocality)
return err.ErrorOrNil()
}
dependentLocalities := localities[localityIdx].DependentLocalities
dependentLocalitiesIdx := -1
if address.DependentLocality == "" || len(dependentLocalities) <= 0 {
return err.ErrorOrNil()
}
for i, dl := range dependentLocalities {
if dl.ID == address.DependentLocality {
dependentLocalitiesIdx = i
}
}
if dependentLocalitiesIdx == -1 {
err = multierror.Append(err, ErrInvalidDependentLocality)
return err.ErrorOrNil()
}
}
return err.ErrorOrNil()
}
func checkPostCode(address Address, regex postCodeRegex) error {
var err *multierror.Error
if address.PostCode != "" && regex.regex != "" {
country := regex
countryRegex := regexp.MustCompile(country.regex)
if !countryRegex.MatchString(address.PostCode) {
err = multierror.Append(err, ErrInvalidPostCode)
return err.ErrorOrNil()
}
if adminArea, ok := country.subdivisionRegex[address.AdministrativeArea]; ok {
adminAreaRegex := regexp.MustCompile(adminArea.regex)
if !adminAreaRegex.MatchString(address.PostCode) {
err = multierror.Append(err, ErrInvalidPostCode)
return err.ErrorOrNil()
}
if locality, ok := adminArea.subdivisionRegex[address.Locality]; ok {
localityRegex := regexp.MustCompile(locality.regex)
if !localityRegex.MatchString(address.PostCode) {
err = multierror.Append(err, ErrInvalidPostCode)
return err.ErrorOrNil()
}
if dependentLocality, ok := locality.subdivisionRegex[address.DependentLocality]; ok {
dependentLocalityRegex := regexp.MustCompile(dependentLocality.regex)
if !dependentLocalityRegex.MatchString(address.PostCode) {
err = multierror.Append(err, ErrInvalidPostCode)
return err.ErrorOrNil()
}
}
}
}
}
return err.ErrorOrNil()
}