forked from contentful-labs/contentful-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content_type_field_validations.go
263 lines (205 loc) · 6.99 KB
/
content_type_field_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
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
package contentful
import (
"encoding/json"
"time"
)
// FieldValidation interface
type FieldValidation interface{}
// FieldValidationLink model
type FieldValidationLink struct {
LinkContentType []string `json:"linkContentType,omitempty"`
}
const (
// MimeTypeAttachment mime type validation for content type field
MimeTypeAttachment = "attachment"
// MimeTypePlainText mime type validation for content type field
MimeTypePlainText = "plaintext"
// MimeTypeImage mime type validation for content type field
MimeTypeImage = "image"
// MimeTypeAudio mime type validation for content type field
MimeTypeAudio = "audio"
// MimeTypeVideo mime type validation for content type field
MimeTypeVideo = "video"
// MimeTypeRichText mime type validation for content type field
MimeTypeRichText = "richtext"
// MimeTypePresentation mime type validation for content type field
MimeTypePresentation = "presentation"
// MimeTypeSpreadSheet mime type validation for content type field
MimeTypeSpreadSheet = "spreadsheet"
// MimeTypePDF mime type validation for content type field
MimeTypePDF = "pdfdocument"
// MimeTypeArchive mime type validation for content type field
MimeTypeArchive = "archive"
// MimeTypeCode mime type validation for content type field
MimeTypeCode = "code"
// MimeTypeMarkup mime type validation for content type field
MimeTypeMarkup = "markup"
)
// FieldValidationMimeType model
type FieldValidationMimeType struct {
MimeTypes []string `json:"linkMimetypeGroup,omitempty"`
}
// MinMax model
type MinMax struct {
Min float64 `json:"min,omitempty"`
Max float64 `json:"max,omitempty"`
}
// DateMinMax model
type DateMinMax struct {
Min time.Time `json:"min,omitempty"`
Max time.Time `json:"max,omitempty"`
}
// FieldValidationDimension model
type FieldValidationDimension struct {
Width *MinMax `json:"width,omitempty"`
Height *MinMax `json:"height,omitempty"`
ErrorMessage string `json:"message,omitempty"`
}
// MarshalJSON for custom json marshaling
func (v *FieldValidationDimension) MarshalJSON() ([]byte, error) {
type dimension struct {
Width *MinMax `json:"width,omitempty"`
Height *MinMax `json:"height,omitempty"`
}
return json.Marshal(&struct {
AssetImageDimensions *dimension `json:"assetImageDimensions,omitempty"`
Message string `json:"message,omitempty"`
}{
AssetImageDimensions: &dimension{
Width: v.Width,
Height: v.Height,
},
Message: v.ErrorMessage,
})
}
// UnmarshalJSON for custom json unmarshaling
func (v *FieldValidationDimension) UnmarshalJSON(data []byte) error {
payload := map[string]interface{}{}
if err := json.Unmarshal(data, &payload); err != nil {
return err
}
dimensionData := payload["assetImageDimensions"].(map[string]interface{})
if width, ok := dimensionData["width"].(map[string]interface{}); ok {
v.Width = &MinMax{}
if min, ok := width["min"].(float64); ok {
v.Width.Min = min
}
if max, ok := width["min"].(float64); ok {
v.Width.Max = max
}
}
if height, ok := dimensionData["height"].(map[string]interface{}); ok {
v.Height = &MinMax{}
if min, ok := height["min"].(float64); ok {
v.Height.Min = min
}
if max, ok := height["max"].(float64); ok {
v.Height.Max = max
}
}
if val, ok := payload["message"].(string); ok {
v.ErrorMessage = val
}
return nil
}
// FieldValidationFileSize model
type FieldValidationFileSize struct {
Size *MinMax `json:"assetFileSize,omitempty"`
ErrorMessage string `json:"message,omitempty"`
}
// FieldValidationUnique model
type FieldValidationUnique struct {
Unique bool `json:"unique"`
}
// FieldValidationPredefinedValues model
type FieldValidationPredefinedValues struct {
In []interface{} `json:"in,omitempty"`
ErrorMessage string `json:"message"`
}
// FieldValidationRange model
type FieldValidationRange struct {
Range *MinMax `json:"range,omitempty"`
ErrorMessage string `json:"message,omitempty"`
}
// FieldValidationDate model
type FieldValidationDate struct {
Range *DateMinMax `json:"dateRange,omitempty"`
ErrorMessage string `json:"message,omitempty"`
}
// MarshalJSON for custom json marshaling
func (v *FieldValidationDate) MarshalJSON() ([]byte, error) {
type dateRange struct {
Min string `json:"min,omitempty"`
Max string `json:"max,omitempty"`
}
return json.Marshal(&struct {
DateRange *dateRange `json:"dateRange,omitempty"`
Message string `json:"message,omitempty"`
}{
DateRange: &dateRange{
Min: v.Range.Max.Format("2006-01-02T03:04:05"),
Max: v.Range.Max.Format("2006-01-02T03:04:05"),
},
Message: v.ErrorMessage,
})
}
// UnmarshalJSON for custom json unmarshaling
func (v *FieldValidationDate) UnmarshalJSON(data []byte) error {
payload := map[string]interface{}{}
if err := json.Unmarshal(data, &payload); err != nil {
return err
}
dateRangeData := payload["dateRange"].(map[string]interface{})
v.Range = &DateMinMax{}
if min, ok := dateRangeData["min"].(string); ok {
minDate, err := time.Parse("2006-01-02T03:04:05", min)
if err != nil {
return err
}
v.Range.Min = minDate
}
if max, ok := dateRangeData["max"].(string); ok {
maxDate, err := time.Parse("2006-01-02T03:04:05", max)
if err != nil {
return err
}
v.Range.Max = maxDate
}
if val, ok := payload["message"].(string); ok {
v.ErrorMessage = val
}
return nil
}
// FieldValidationSize model
type FieldValidationSize struct {
Size *MinMax `json:"size,omitempty"`
ErrorMessage string `json:"message,omitempty"`
}
const (
// FieldValidationRegexPatternEmail email validation
FieldValidationRegexPatternEmail = `^\w[\w.-]*@([\w-]+\.)+[\w-]+$`
// FieldValidationRegexPatternURL url validation
FieldValidationRegexPatternURL = `^(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$`
// FieldValidationRegexPatternUSDate us date validation
FieldValidationRegexPatternUSDate = `^(0?[1-9]|[12][0-9]|3[01])[- \/.](0?[1-9]|1[012])[- \/.](19|20)?\d\d$`
// FieldValidationRegexPatternEuorpeanDate euorpean date validation
FieldValidationRegexPatternEuorpeanDate = `^(0?[1-9]|[12][0-9]|3[01])[- \/.](0?[1-9]|1[012])[- \/.](19|20)?\d\d$`
// FieldValidationRegexPattern12HourTime 12-hour time validation
FieldValidationRegexPattern12HourTime = `^(0?[1-9]|1[012]):[0-5][0-9](:[0-5][0-9])?\s*[aApP][mM]$`
// FieldValidationRegexPattern24HourTime 24-hour time validation
FieldValidationRegexPattern24HourTime = `^(0?[0-9]|1[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$`
// FieldValidationRegexPatternUSPhoneNumber us phone number validation
FieldValidationRegexPatternUSPhoneNumber = `^\d[ -.]?\(?\d\d\d\)?[ -.]?\d\d\d[ -.]?\d\d\d\d$`
// FieldValidationRegexPatternUSZipCode us zip code validation
FieldValidationRegexPatternUSZipCode = `^\d{5}$|^\d{5}-\d{4}$}`
)
// Regex model
type Regex struct {
Pattern string `json:"pattern,omitempty"`
Flags string `json:"flags,omitempty"`
}
// FieldValidationRegex model
type FieldValidationRegex struct {
Regex *Regex `json:"regexp,omitempty"`
ErrorMessage string `json:"message,omitempty"`
}