-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtuple.go
More file actions
411 lines (351 loc) · 11.6 KB
/
tuple.go
File metadata and controls
411 lines (351 loc) · 11.6 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
package schema
import (
"encoding/json"
"fmt"
"reflect"
"github.com/nyxstack/i18n"
)
// Default error messages for tuple validation
var (
tupleRequiredError = i18n.S("value is required")
tupleTypeError = i18n.S("value must be an array")
tupleUniqueError = i18n.S("tuple items must be unique")
)
func tupleLengthError(expected int) i18n.TranslatedFunc {
return i18n.F("tuple must have exactly %d items", expected)
}
func tupleMinLengthError(min int) i18n.TranslatedFunc {
return i18n.F("tuple must have at least %d items", min)
}
func tupleItemError(index int) i18n.TranslatedFunc {
return i18n.F("tuple item at index %d is invalid", index)
}
// TupleSchema represents a JSON Schema for fixed-length arrays with position-specific types
type TupleSchema struct {
Schema
// Tuple-specific validation
itemSchemas []Parseable // Schemas for each position (order matters)
additionalItems bool // Allow additional items beyond defined positions
uniqueItems bool // Items must be unique
nullable bool // Allow null values
// Error messages for validation failures (support i18n)
requiredError ErrorMessage
lengthError ErrorMessage
uniqueItemsError ErrorMessage
itemError ErrorMessage
typeMismatchError ErrorMessage
}
// Tuple creates a new tuple schema with position-specific item schemas
func Tuple(itemSchemas ...Parseable) *TupleSchema {
schema := &TupleSchema{
Schema: Schema{
schemaType: "array",
required: true, // Default to required
},
itemSchemas: itemSchemas,
additionalItems: false, // Strict by default - exact length required
}
return schema
}
// Core fluent API methods
// Title sets the title of the schema
func (s *TupleSchema) Title(title string) *TupleSchema {
s.Schema.title = title
return s
}
// Description sets the description of the schema
func (s *TupleSchema) Description(description string) *TupleSchema {
s.Schema.description = description
return s
}
// Default sets the default value
func (s *TupleSchema) Default(value interface{}) *TupleSchema {
s.Schema.defaultValue = value
return s
}
// Example adds an example value
func (s *TupleSchema) Example(example []interface{}) *TupleSchema {
s.Schema.examples = append(s.Schema.examples, example)
return s
}
// Tuple-specific validation
// AllowAdditionalItems allows extra items beyond the defined positions
func (s *TupleSchema) AllowAdditionalItems() *TupleSchema {
s.additionalItems = true
return s
}
// Strict requires exact length matching (default behavior)
func (s *TupleSchema) Strict() *TupleSchema {
s.additionalItems = false
return s
}
// UniqueItems requires all items to be unique with optional custom error message
func (s *TupleSchema) UniqueItems(errorMessage ...interface{}) *TupleSchema {
s.uniqueItems = true
if len(errorMessage) > 0 {
s.uniqueItemsError = toErrorMessage(errorMessage[0])
}
return s
}
// Required/Optional/Nullable control
// Optional marks the schema as optional
func (s *TupleSchema) Optional() *TupleSchema {
s.Schema.required = false
return s
}
// Required marks the schema as required (default behavior) with optional custom error message
func (s *TupleSchema) Required(errorMessage ...interface{}) *TupleSchema {
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 *TupleSchema) Nullable() *TupleSchema {
s.nullable = true
return s
}
// Error customization
// TypeError sets a custom error message for type mismatch validation
func (s *TupleSchema) TypeError(message string) *TupleSchema {
s.typeMismatchError = toErrorMessage(message)
return s
}
// LengthError sets a custom error message for length validation
func (s *TupleSchema) LengthError(message string) *TupleSchema {
s.lengthError = toErrorMessage(message)
return s
}
// ItemError sets a custom error message for item validation failures
func (s *TupleSchema) ItemError(message string) *TupleSchema {
s.itemError = toErrorMessage(message)
return s
}
// Getters for accessing private fields
// IsRequired returns whether the schema is marked as required
func (s *TupleSchema) IsRequired() bool {
return s.Schema.required
}
// IsOptional returns whether the schema is marked as optional
func (s *TupleSchema) IsOptional() bool {
return !s.Schema.required
}
// IsNullable returns whether the schema allows nil values
func (s *TupleSchema) IsNullable() bool {
return s.nullable
}
// GetItemSchemas returns the schemas for each tuple position
func (s *TupleSchema) GetItemSchemas() []Parseable {
return s.itemSchemas
}
// GetExpectedLength returns the expected tuple length
func (s *TupleSchema) GetExpectedLength() int {
return len(s.itemSchemas)
}
// AllowsAdditionalItems returns whether additional items are allowed
func (s *TupleSchema) AllowsAdditionalItems() bool {
return s.additionalItems
}
// IsUniqueItems returns whether items must be unique
func (s *TupleSchema) IsUniqueItems() bool {
return s.uniqueItems
}
// Validation helpers
// isUnique checks if all items in a slice are unique
func isTupleUnique(slice []interface{}) bool {
seen := make(map[interface{}]bool)
for _, item := range slice {
key := getTupleComparableKey(item)
if seen[key] {
return false
}
seen[key] = true
}
return true
}
// getTupleComparableKey converts an interface{} to a comparable key
func getTupleComparableKey(item interface{}) interface{} {
if item == nil {
return nil
}
v := reflect.ValueOf(item)
switch v.Kind() {
case reflect.Slice, reflect.Map, reflect.Func:
// These types aren't directly comparable, use their string representation
return v.String()
default:
return item
}
}
// Validation
// Parse validates and parses a tuple value, returning the final parsed value
func (s *TupleSchema) Parse(value interface{}, ctx *ValidationContext) ParseResult {
var errors []ValidationError
// Handle nil values
if value == nil {
if s.nullable {
// For nullable schemas, nil is a valid value
return ParseResult{Valid: true, Value: nil, Errors: nil}
}
if s.Schema.required {
// Check if we have a default value to use instead
if defaultVal := s.GetDefault(); defaultVal != nil {
// Use default value and re-parse it
return s.Parse(defaultVal, ctx)
}
// No default, required field is missing
message := tupleRequiredError(ctx.Locale)
if !isEmptyErrorMessage(s.requiredError) {
message = resolveErrorMessage(s.requiredError, ctx)
}
return ParseResult{
Valid: false,
Value: nil,
Errors: []ValidationError{NewPrimitiveError(value, message, "required")},
}
}
// Optional field, use default if available
if defaultVal := s.GetDefault(); defaultVal != nil {
return s.Parse(defaultVal, ctx)
}
// Optional field with no default
return ParseResult{Valid: true, Value: nil, Errors: nil}
}
// Type check - convert to slice
var tupleValue []interface{}
v := reflect.ValueOf(value)
if v.Kind() != reflect.Slice && v.Kind() != reflect.Array {
message := tupleTypeError(ctx.Locale)
if !isEmptyErrorMessage(s.typeMismatchError) {
message = resolveErrorMessage(s.typeMismatchError, ctx)
}
return ParseResult{
Valid: false,
Value: nil,
Errors: []ValidationError{NewPrimitiveError(value, message, "invalid_type")},
}
}
// Convert to []interface{}
tupleValue = make([]interface{}, v.Len())
for i := 0; i < v.Len(); i++ {
tupleValue[i] = v.Index(i).Interface()
}
// Validate length constraints
actualLength := len(tupleValue)
expectedLength := len(s.itemSchemas)
if !s.additionalItems && actualLength != expectedLength {
message := tupleLengthError(expectedLength)(ctx.Locale)
if !isEmptyErrorMessage(s.lengthError) {
message = resolveErrorMessage(s.lengthError, ctx)
}
errors = append(errors, NewPrimitiveError(tupleValue, message, "tuple_length"))
}
if s.additionalItems && actualLength < expectedLength {
message := tupleMinLengthError(expectedLength)(ctx.Locale)
if !isEmptyErrorMessage(s.lengthError) {
message = resolveErrorMessage(s.lengthError, ctx)
}
errors = append(errors, NewPrimitiveError(tupleValue, message, "min_length"))
}
// Prepare final value array
finalValue := make([]interface{}, len(tupleValue))
// Validate each item at its position using the corresponding schema
for i, item := range tupleValue {
if i < len(s.itemSchemas) {
// Validate using position-specific schema
itemResult := s.itemSchemas[i].Parse(item, ctx)
if !itemResult.Valid {
// Create error for this item
message := tupleItemError(i)(ctx.Locale)
if !isEmptyErrorMessage(s.itemError) {
message = resolveErrorMessage(s.itemError, ctx)
}
// Add the main item error
errors = append(errors, NewFieldError([]string{fmt.Sprintf("[%d]", i)}, item, message, "item_invalid"))
// Also add the specific validation errors for this item
for _, itemErr := range itemResult.Errors {
// Prefix the path with tuple index
errors = append(errors, NewFieldError(append([]string{fmt.Sprintf("[%d]", i)}, itemErr.Path...), itemErr.Value, itemErr.Message, itemErr.Code))
}
} else {
// Use the parsed value from item validation
finalValue[i] = itemResult.Value
}
} else if s.additionalItems {
// Additional items beyond defined positions - accept as-is
finalValue[i] = item
}
}
// Check uniqueness constraint
if s.uniqueItems && !isTupleUnique(tupleValue) {
message := tupleUniqueError(ctx.Locale)
if !isEmptyErrorMessage(s.uniqueItemsError) {
message = resolveErrorMessage(s.uniqueItemsError, ctx)
}
errors = append(errors, NewPrimitiveError(tupleValue, message, "unique_items"))
}
return ParseResult{
Valid: len(errors) == 0,
Value: finalValue,
Errors: errors,
}
}
// JSON generates JSON Schema representation
func (s *TupleSchema) JSON() map[string]interface{} {
schema := baseJSONSchema("array")
// Add base schema fields
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())
// Add tuple-specific fields using "items" as array of schemas
if len(s.itemSchemas) > 0 {
items := make([]interface{}, len(s.itemSchemas))
for i, itemSchema := range s.itemSchemas {
if jsonSchema, ok := itemSchema.(interface{ JSON() map[string]interface{} }); ok {
items[i] = jsonSchema.JSON()
} else {
items[i] = map[string]interface{}{"type": "unknown"}
}
}
schema["items"] = items
}
// Add additionalItems
schema["additionalItems"] = s.additionalItems
// Add uniqueItems if true
if s.uniqueItems {
schema["uniqueItems"] = true
}
// Set exact length constraints for strict tuples
if !s.additionalItems && len(s.itemSchemas) > 0 {
schema["minItems"] = len(s.itemSchemas)
schema["maxItems"] = len(s.itemSchemas)
} else if len(s.itemSchemas) > 0 {
schema["minItems"] = len(s.itemSchemas)
}
// Add nullable if true
if s.nullable {
schema["type"] = []string{"array", "null"}
}
return schema
}
// MarshalJSON implements json.Marshaler to properly serialize TupleSchema for JSON schema generation
func (s *TupleSchema) MarshalJSON() ([]byte, error) {
type jsonTupleSchema struct {
Schema
ItemSchemas []Parseable `json:"itemSchemas"`
AdditionalItems bool `json:"additionalItems"`
UniqueItems bool `json:"uniqueItems,omitempty"`
Nullable bool `json:"nullable,omitempty"`
}
return json.Marshal(jsonTupleSchema{
Schema: s.Schema,
ItemSchemas: s.itemSchemas,
AdditionalItems: s.additionalItems,
UniqueItems: s.uniqueItems,
Nullable: s.nullable,
})
}