-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathint64.go
More file actions
278 lines (244 loc) · 7.53 KB
/
int64.go
File metadata and controls
278 lines (244 loc) · 7.53 KB
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
275
276
277
278
package schema
import (
"github.com/nyxstack/i18n"
)
var (
int64RequiredError = i18n.S("value is required")
int64TypeError = i18n.S("value must be a 64-bit integer")
int64EnumError = i18n.S("value must be one of the allowed values")
)
func int64MinimumError(min int64) i18n.TranslatedFunc {
return i18n.F("value must be at least %d", min)
}
func int64MaximumError(max int64) i18n.TranslatedFunc {
return i18n.F("value must be at most %d", max)
}
func int64MultipleOfError(multiple int64) i18n.TranslatedFunc {
return i18n.F("value must be a multiple of %d", multiple)
}
func int64ConstError(value int64) i18n.TranslatedFunc {
return i18n.F("value must be exactly: %d", value)
}
type Int64Schema struct {
Schema
minimum *int64
maximum *int64
multipleOf *int64
nullable bool
requiredError ErrorMessage
minimumError ErrorMessage
maximumError ErrorMessage
multipleOfError ErrorMessage
enumError ErrorMessage
constError ErrorMessage
typeMismatchError ErrorMessage
}
func Int64(errorMessage ...interface{}) *Int64Schema {
schema := &Int64Schema{
Schema: Schema{
schemaType: "integer",
required: true,
},
}
if len(errorMessage) > 0 {
schema.typeMismatchError = toErrorMessage(errorMessage[0])
}
return schema
}
func (s *Int64Schema) Title(title string) *Int64Schema { s.Schema.title = title; return s }
func (s *Int64Schema) Description(description string) *Int64Schema {
s.Schema.description = description
return s
}
func (s *Int64Schema) Default(value interface{}) *Int64Schema {
s.Schema.defaultValue = value
return s
}
func (s *Int64Schema) Example(example int64) *Int64Schema {
s.Schema.examples = append(s.Schema.examples, example)
return s
}
func (s *Int64Schema) Optional() *Int64Schema { s.Schema.required = false; return s }
func (s *Int64Schema) Nullable() *Int64Schema { s.nullable = true; return s }
func (s *Int64Schema) Enum(values []int64, errorMessage ...interface{}) *Int64Schema {
s.Schema.enum = make([]interface{}, len(values))
for i, v := range values {
s.Schema.enum[i] = v
}
if len(errorMessage) > 0 {
s.enumError = toErrorMessage(errorMessage[0])
}
return s
}
func (s *Int64Schema) Const(value int64, errorMessage ...interface{}) *Int64Schema {
s.Schema.constVal = value
if len(errorMessage) > 0 {
s.constError = toErrorMessage(errorMessage[0])
}
return s
}
func (s *Int64Schema) Min(min int64, errorMessage ...interface{}) *Int64Schema {
s.minimum = &min
if len(errorMessage) > 0 {
s.minimumError = toErrorMessage(errorMessage[0])
}
return s
}
func (s *Int64Schema) Max(max int64, errorMessage ...interface{}) *Int64Schema {
s.maximum = &max
if len(errorMessage) > 0 {
s.maximumError = toErrorMessage(errorMessage[0])
}
return s
}
func (s *Int64Schema) Range(min, max int64, errorMessage ...interface{}) *Int64Schema {
s.minimum = &min
s.maximum = &max
if len(errorMessage) > 0 {
s.minimumError = toErrorMessage(errorMessage[0])
s.maximumError = toErrorMessage(errorMessage[0])
}
return s
}
func (s *Int64Schema) MultipleOf(multiple int64, errorMessage ...interface{}) *Int64Schema {
s.multipleOf = &multiple
if len(errorMessage) > 0 {
s.multipleOfError = toErrorMessage(errorMessage[0])
}
return s
}
func (s *Int64Schema) IsRequired() bool { return s.Schema.required }
func (s *Int64Schema) IsOptional() bool { return !s.Schema.required }
func (s *Int64Schema) IsNullable() bool { return s.nullable }
func (s *Int64Schema) GetMinimum() *int64 { return s.minimum }
func (s *Int64Schema) GetMaximum() *int64 { return s.maximum }
func (s *Int64Schema) GetMultipleOf() *int64 { return s.multipleOf }
func (s *Int64Schema) Parse(value interface{}, ctx *ValidationContext) ParseResult {
var errors []ValidationError
if value == nil {
if s.nullable {
return ParseResult{Valid: true, Value: nil, Errors: nil}
}
if s.Schema.required {
if defaultVal := s.GetDefault(); defaultVal != nil {
return s.Parse(defaultVal, ctx)
}
message := int64RequiredError(ctx.Locale)
if !isEmptyErrorMessage(s.requiredError) {
message = resolveErrorMessage(s.requiredError, ctx)
}
return ParseResult{Valid: false, Value: nil, Errors: []ValidationError{NewPrimitiveError(value, message, "required")}}
}
if defaultVal := s.GetDefault(); defaultVal != nil {
return s.Parse(defaultVal, ctx)
}
return ParseResult{Valid: true, Value: nil, Errors: nil}
}
var int64Value int64
var typeValid bool
switch v := value.(type) {
case int64:
int64Value = v
typeValid = true
case int:
int64Value = int64(v)
typeValid = true
case int8:
int64Value = int64(v)
typeValid = true
case int16:
int64Value = int64(v)
typeValid = true
case int32:
int64Value = int64(v)
typeValid = true
case float32:
if v == float32(int64(v)) {
int64Value = int64(v)
typeValid = true
}
case float64:
if v == float64(int64(v)) {
int64Value = int64(v)
typeValid = true
}
}
if !typeValid {
message := int64TypeError(ctx.Locale)
if !isEmptyErrorMessage(s.typeMismatchError) {
message = resolveErrorMessage(s.typeMismatchError, ctx)
}
return ParseResult{Valid: false, Value: nil, Errors: []ValidationError{NewPrimitiveError(value, message, "invalid_type")}}
}
finalValue := int64Value
if s.minimum != nil && int64Value < *s.minimum {
message := int64MinimumError(*s.minimum)(ctx.Locale)
if !isEmptyErrorMessage(s.minimumError) {
message = resolveErrorMessage(s.minimumError, ctx)
}
errors = append(errors, NewPrimitiveError(int64Value, message, "minimum"))
}
if s.maximum != nil && int64Value > *s.maximum {
message := int64MaximumError(*s.maximum)(ctx.Locale)
if !isEmptyErrorMessage(s.maximumError) {
message = resolveErrorMessage(s.maximumError, ctx)
}
errors = append(errors, NewPrimitiveError(int64Value, message, "maximum"))
}
if s.multipleOf != nil && int64Value%*s.multipleOf != 0 {
message := int64MultipleOfError(*s.multipleOf)(ctx.Locale)
if !isEmptyErrorMessage(s.multipleOfError) {
message = resolveErrorMessage(s.multipleOfError, ctx)
}
errors = append(errors, NewPrimitiveError(int64Value, message, "multiple_of"))
}
if len(s.Schema.enum) > 0 {
valid := false
for _, enumValue := range s.Schema.enum {
if enumValue == int64Value {
valid = true
break
}
}
if !valid {
message := int64EnumError(ctx.Locale)
if !isEmptyErrorMessage(s.enumError) {
message = resolveErrorMessage(s.enumError, ctx)
}
errors = append(errors, NewPrimitiveError(int64Value, message, "enum"))
}
}
if s.Schema.constVal != nil {
if constInt64, ok := s.Schema.constVal.(int64); ok && constInt64 != int64Value {
message := int64ConstError(constInt64)(ctx.Locale)
if !isEmptyErrorMessage(s.constError) {
message = resolveErrorMessage(s.constError, ctx)
}
errors = append(errors, NewPrimitiveError(int64Value, message, "const"))
}
}
return ParseResult{Valid: len(errors) == 0, Value: finalValue, Errors: errors}
}
func (s *Int64Schema) JSON() map[string]interface{} {
schema := baseJSONSchema("integer")
addTitle(schema, s.GetTitle())
addDescription(schema, s.GetDescription())
addOptionalField(schema, "default", s.GetDefault())
addOptionalArray(schema, "examples", s.GetExamples())
addOptionalArray(schema, "enum", s.GetEnum())
addOptionalField(schema, "const", s.GetConst())
if s.minimum != nil {
schema["minimum"] = *s.minimum
}
if s.maximum != nil {
schema["maximum"] = *s.maximum
}
if s.multipleOf != nil {
schema["multipleOf"] = *s.multipleOf
}
schema["format"] = "int64"
if s.nullable {
schema["type"] = []string{"integer", "null"}
}
return schema
}