-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathint16.go
More file actions
418 lines (362 loc) · 10.9 KB
/
int16.go
File metadata and controls
418 lines (362 loc) · 10.9 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
package schema
import (
"encoding/json"
"math"
"github.com/nyxstack/i18n"
)
// Default error messages for int16 validation
var (
int16RequiredError = i18n.S("value is required")
int16TypeError = i18n.S("value must be a 16-bit integer")
int16EnumError = i18n.S("value must be one of the allowed values")
int16RangeError = i18n.S("value must be between -32768 and 32767")
)
// Default error message functions that take parameters
func int16MinimumError(min int16) i18n.TranslatedFunc {
return i18n.F("value must be at least %d", min)
}
func int16MaximumError(max int16) i18n.TranslatedFunc {
return i18n.F("value must be at most %d", max)
}
func int16MultipleOfError(multiple int16) i18n.TranslatedFunc {
return i18n.F("value must be a multiple of %d", multiple)
}
func int16ConstError(value int16) i18n.TranslatedFunc {
return i18n.F("value must be exactly: %d", value)
}
// Int16Schema represents a JSON Schema for int16 values
type Int16Schema struct {
Schema
// Int16-specific validation (private fields)
minimum *int16
maximum *int16
multipleOf *int16
nullable bool
// Error messages for validation failures (support i18n)
requiredError ErrorMessage
minimumError ErrorMessage
maximumError ErrorMessage
multipleOfError ErrorMessage
enumError ErrorMessage
constError ErrorMessage
typeMismatchError ErrorMessage
rangeError ErrorMessage
}
// Int16 creates a new int16 schema with optional type error message
func Int16(errorMessage ...interface{}) *Int16Schema {
schema := &Int16Schema{
Schema: Schema{
schemaType: "integer",
required: true, // Default to required
},
}
if len(errorMessage) > 0 {
schema.typeMismatchError = toErrorMessage(errorMessage[0])
}
return schema
}
// Core fluent API methods
// Title sets the title of the schema
func (s *Int16Schema) Title(title string) *Int16Schema {
s.Schema.title = title
return s
}
// Description sets the description of the schema
func (s *Int16Schema) Description(description string) *Int16Schema {
s.Schema.description = description
return s
}
// Default sets the default value
func (s *Int16Schema) Default(value interface{}) *Int16Schema {
s.Schema.defaultValue = value
return s
}
// Example adds an example value
func (s *Int16Schema) Example(example int16) *Int16Schema {
s.Schema.examples = append(s.Schema.examples, example)
return s
}
// Enum sets the allowed enum values with optional custom error message
func (s *Int16Schema) Enum(values []int16, errorMessage ...interface{}) *Int16Schema {
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
}
// Const sets a constant value with optional custom error message
func (s *Int16Schema) Const(value int16, errorMessage ...interface{}) *Int16Schema {
s.Schema.constVal = value
if len(errorMessage) > 0 {
s.constError = toErrorMessage(errorMessage[0])
}
return s
}
// Required/Optional/Nullable control
// Optional marks the schema as optional
func (s *Int16Schema) Optional() *Int16Schema {
s.Schema.required = false
return s
}
// Required marks the schema as required (default behavior) with optional custom error message
func (s *Int16Schema) Required(errorMessage ...interface{}) *Int16Schema {
s.Schema.required = true
if len(errorMessage) > 0 {
s.requiredError = toErrorMessage(errorMessage[0])
}
return s
}
// Nullable marks the schema as nullable (allows nil values)
func (s *Int16Schema) Nullable() *Int16Schema {
s.nullable = true
return s
}
// TypeError sets a custom error message for type mismatch validation
func (s *Int16Schema) TypeError(message string) *Int16Schema {
s.typeMismatchError = toErrorMessage(message)
return s
}
// Int16-specific fluent API methods
// Min sets the minimum value constraint with optional custom error message
func (s *Int16Schema) Min(min int16, errorMessage ...interface{}) *Int16Schema {
s.minimum = &min
if len(errorMessage) > 0 {
s.minimumError = toErrorMessage(errorMessage[0])
}
return s
}
// Max sets the maximum value constraint with optional custom error message
func (s *Int16Schema) Max(max int16, errorMessage ...interface{}) *Int16Schema {
s.maximum = &max
if len(errorMessage) > 0 {
s.maximumError = toErrorMessage(errorMessage[0])
}
return s
}
// Range sets both minimum and maximum values with optional custom error message
func (s *Int16Schema) Range(min, max int16, errorMessage ...interface{}) *Int16Schema {
s.minimum = &min
s.maximum = &max
if len(errorMessage) > 0 {
s.minimumError = toErrorMessage(errorMessage[0])
s.maximumError = toErrorMessage(errorMessage[0])
}
return s
}
// MultipleOf sets the multiple constraint with optional custom error message
func (s *Int16Schema) MultipleOf(multiple int16, errorMessage ...interface{}) *Int16Schema {
s.multipleOf = &multiple
if len(errorMessage) > 0 {
s.multipleOfError = toErrorMessage(errorMessage[0])
}
return s
}
// Getters for accessing private fields
// IsRequired returns whether the schema is marked as required
func (s *Int16Schema) IsRequired() bool {
return s.Schema.required
}
// IsOptional returns whether the schema is marked as optional
func (s *Int16Schema) IsOptional() bool {
return !s.Schema.required
}
// IsNullable returns whether the schema allows nil values
func (s *Int16Schema) IsNullable() bool {
return s.nullable
}
// GetMinimum returns the minimum value constraint
func (s *Int16Schema) GetMinimum() *int16 {
return s.minimum
}
// GetMaximum returns the maximum value constraint
func (s *Int16Schema) GetMaximum() *int16 {
return s.maximum
}
// GetMultipleOf returns the multiple constraint
func (s *Int16Schema) GetMultipleOf() *int16 {
return s.multipleOf
}
// GetDefault returns the default value as an int16
func (s *Int16Schema) GetDefaultInt16() *int16 {
if s.GetDefault() != nil {
if i, ok := s.GetDefault().(int16); ok {
return &i
}
}
return nil
}
// Validation
// Parse validates and parses an int16 value, returning the final parsed value
func (s *Int16Schema) Parse(value interface{}, ctx *ValidationContext) ParseResult {
var errors []ValidationError
// Handle nil values
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 := int16RequiredError(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}
}
// Type coercion and validation
var int16Value int16
var typeValid bool
switch v := value.(type) {
case int16:
int16Value = v
typeValid = true
case int8:
int16Value = int16(v)
typeValid = true
case int:
if v >= math.MinInt16 && v <= math.MaxInt16 {
int16Value = int16(v)
typeValid = true
}
case int32:
if v >= math.MinInt16 && v <= math.MaxInt16 {
int16Value = int16(v)
typeValid = true
}
case int64:
if v >= math.MinInt16 && v <= math.MaxInt16 {
int16Value = int16(v)
typeValid = true
}
case float32:
if v == float32(int(v)) && v >= math.MinInt16 && v <= math.MaxInt16 {
int16Value = int16(v)
typeValid = true
}
case float64:
if v == float64(int(v)) && v >= math.MinInt16 && v <= math.MaxInt16 {
int16Value = int16(v)
typeValid = true
}
}
if !typeValid {
message := int16TypeError(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 := int16Value
// Validation constraints
if s.minimum != nil && int16Value < *s.minimum {
message := int16MinimumError(*s.minimum)(ctx.Locale)
if !isEmptyErrorMessage(s.minimumError) {
message = resolveErrorMessage(s.minimumError, ctx)
}
errors = append(errors, NewPrimitiveError(int16Value, message, "minimum"))
}
if s.maximum != nil && int16Value > *s.maximum {
message := int16MaximumError(*s.maximum)(ctx.Locale)
if !isEmptyErrorMessage(s.maximumError) {
message = resolveErrorMessage(s.maximumError, ctx)
}
errors = append(errors, NewPrimitiveError(int16Value, message, "maximum"))
}
if s.multipleOf != nil && int16Value%*s.multipleOf != 0 {
message := int16MultipleOfError(*s.multipleOf)(ctx.Locale)
if !isEmptyErrorMessage(s.multipleOfError) {
message = resolveErrorMessage(s.multipleOfError, ctx)
}
errors = append(errors, NewPrimitiveError(int16Value, message, "multiple_of"))
}
if len(s.Schema.enum) > 0 {
valid := false
for _, enumValue := range s.Schema.enum {
if enumValue == int16Value {
valid = true
break
}
}
if !valid {
message := int16EnumError(ctx.Locale)
if !isEmptyErrorMessage(s.enumError) {
message = resolveErrorMessage(s.enumError, ctx)
}
errors = append(errors, NewPrimitiveError(int16Value, message, "enum"))
}
}
if s.Schema.constVal != nil {
if constInt16, ok := s.Schema.constVal.(int16); ok && constInt16 != int16Value {
message := int16ConstError(constInt16)(ctx.Locale)
if !isEmptyErrorMessage(s.constError) {
message = resolveErrorMessage(s.constError, ctx)
}
errors = append(errors, NewPrimitiveError(int16Value, message, "const"))
}
}
return ParseResult{
Valid: len(errors) == 0,
Value: finalValue,
Errors: errors,
}
}
// JSON generates JSON Schema representation
func (s *Int16Schema) 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"] = int(*s.minimum)
}
if s.maximum != nil {
schema["maximum"] = int(*s.maximum)
}
if s.multipleOf != nil {
schema["multipleOf"] = int(*s.multipleOf)
}
schema["format"] = "int16"
if s.nullable {
schema["type"] = []string{"integer", "null"}
}
return schema
}
// MarshalJSON implements json.Marshaler
func (s *Int16Schema) MarshalJSON() ([]byte, error) {
type jsonInt16Schema struct {
Schema
Minimum *int16 `json:"minimum,omitempty"`
Maximum *int16 `json:"maximum,omitempty"`
MultipleOf *int16 `json:"multipleOf,omitempty"`
Format string `json:"format"`
Nullable bool `json:"nullable,omitempty"`
}
return json.Marshal(jsonInt16Schema{
Schema: s.Schema,
Minimum: s.minimum,
Maximum: s.maximum,
MultipleOf: s.multipleOf,
Format: "int16",
Nullable: s.nullable,
})
}