-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.go
More file actions
617 lines (527 loc) · 17.4 KB
/
object.go
File metadata and controls
617 lines (527 loc) · 17.4 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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
package schema
import (
"encoding/json"
"fmt"
"reflect"
"github.com/nyxstack/i18n"
)
// Default error messages for object validation
var (
objectRequiredError = i18n.S("value is required")
objectTypeError = i18n.S("value must be an object")
objectAdditionalPropsError = i18n.S("additional property is not allowed")
)
func objectMinPropsError(min int) i18n.TranslatedFunc {
return i18n.F("object must have at least %d properties", min)
}
func objectMaxPropsError(max int) i18n.TranslatedFunc {
return i18n.F("object must have at most %d properties", max)
}
func objectPropertyError(prop string) i18n.TranslatedFunc {
return i18n.F("property %s is invalid", prop)
}
func objectRequiredPropError(prop string) i18n.TranslatedFunc {
return i18n.F("property %s is required", prop)
}
// Shape represents a map of property names to their schemas for object construction
type Shape map[string]interface{}
// AsObject converts a Shape to an ObjectSchema
func (s Shape) AsObject() *ObjectSchema {
return Object(s)
}
// ObjectProperty represents a single property in an object schema
type ObjectProperty struct {
Schema Parseable // The schema validator for this property
Required bool // Whether this property is required
Name string // The property name
}
// ObjectSchema represents a JSON Schema for object values with structured properties
type ObjectSchema struct {
Schema
// Object-specific validation
properties map[string]ObjectProperty // Property schemas
requiredProps []string // List of required property names
additionalProps bool // Allow additional properties
minProps *int // Minimum number of properties
maxProps *int // Maximum number of properties
nullable bool // Allow null values
// Error messages for validation failures (support i18n)
requiredError ErrorMessage
minPropsError ErrorMessage
maxPropsError ErrorMessage
additionalPropsError ErrorMessage
propertyError ErrorMessage
typeMismatchError ErrorMessage
}
// Object creates a new object schema with optional Shape and error message
func Object(shapeAndError ...interface{}) *ObjectSchema {
schema := &ObjectSchema{
Schema: Schema{
schemaType: "object",
required: true, // Default to required
},
properties: make(map[string]ObjectProperty),
requiredProps: []string{},
additionalProps: false, // Strict by default
}
// Process optional parameters
for _, param := range shapeAndError {
switch p := param.(type) {
case Shape:
// Add properties from the shape (determine required from schema)
for name, schemaVal := range p {
schema.Property(name, schemaVal)
}
case string:
// Set custom type mismatch error message
schema.typeMismatchError = toErrorMessage(p)
}
}
return schema
}
// Core fluent API methods
// Title sets the title of the schema
func (s *ObjectSchema) Title(title string) *ObjectSchema {
s.Schema.title = title
return s
}
// Description sets the description of the schema
func (s *ObjectSchema) Description(description string) *ObjectSchema {
s.Schema.description = description
return s
}
// Default sets the default value
func (s *ObjectSchema) Default(value interface{}) *ObjectSchema {
s.Schema.defaultValue = value
return s
}
// Example adds an example value
func (s *ObjectSchema) Example(example map[string]interface{}) *ObjectSchema {
s.Schema.examples = append(s.Schema.examples, example)
return s
}
// Property definition methods
// Property adds a property to the object schema (infers required/optional from schema)
func (s *ObjectSchema) Property(name string, schema interface{}) *ObjectSchema {
// Convert to Parseable interface
var parseable Parseable
if p, ok := schema.(Parseable); ok {
parseable = p
} else {
// Try to wrap in a simple interface - this is a fallback
return s // Skip if not Parseable
}
// Check if the schema has IsRequired() method to determine if it's required
isRequired := true
if requiredChecker, ok := schema.(interface{ IsRequired() bool }); ok {
isRequired = requiredChecker.IsRequired()
}
s.properties[name] = ObjectProperty{
Schema: parseable,
Required: isRequired,
Name: name,
}
// Add to required list if required and not already there
if isRequired {
for _, req := range s.requiredProps {
if req == name {
return s
}
}
s.requiredProps = append(s.requiredProps, name)
}
return s
}
// OptionalProperty explicitly adds an optional property
func (s *ObjectSchema) OptionalProperty(name string, schema interface{}) *ObjectSchema {
var parseable Parseable
if p, ok := schema.(Parseable); ok {
parseable = p
} else {
return s // Skip if not Parseable
}
s.properties[name] = ObjectProperty{
Schema: parseable,
Required: false,
Name: name,
}
return s
}
// RequiredProperty explicitly adds a required property
func (s *ObjectSchema) RequiredProperty(name string, schema interface{}) *ObjectSchema {
var parseable Parseable
if p, ok := schema.(Parseable); ok {
parseable = p
} else {
return s // Skip if not Parseable
}
s.properties[name] = ObjectProperty{
Schema: parseable,
Required: true,
Name: name,
}
// Add to required list if not already there
for _, req := range s.requiredProps {
if req == name {
return s
}
}
s.requiredProps = append(s.requiredProps, name)
return s
}
// Object constraint methods
// MinProperties sets the minimum number of properties with optional custom error message
func (s *ObjectSchema) MinProperties(min int, errorMessage ...interface{}) *ObjectSchema {
s.minProps = &min
if len(errorMessage) > 0 {
s.minPropsError = toErrorMessage(errorMessage[0])
}
return s
}
// MaxProperties sets the maximum number of properties with optional custom error message
func (s *ObjectSchema) MaxProperties(max int, errorMessage ...interface{}) *ObjectSchema {
s.maxProps = &max
if len(errorMessage) > 0 {
s.maxPropsError = toErrorMessage(errorMessage[0])
}
return s
}
// PropertyRange sets both min and max property constraints
func (s *ObjectSchema) PropertyRange(min, max int, errorMessage ...interface{}) *ObjectSchema {
s.minProps = &min
s.maxProps = &max
if len(errorMessage) > 0 {
s.minPropsError = toErrorMessage(errorMessage[0])
s.maxPropsError = toErrorMessage(errorMessage[0])
}
return s
}
// Strict disallows additional properties (default behavior)
func (s *ObjectSchema) Strict() *ObjectSchema {
s.additionalProps = false
return s
}
// Passthrough allows additional properties
func (s *ObjectSchema) Passthrough() *ObjectSchema {
s.additionalProps = true
return s
}
// AdditionalProperties sets whether additional properties are allowed with optional custom error message
func (s *ObjectSchema) AdditionalProperties(allowed bool, errorMessage ...interface{}) *ObjectSchema {
s.additionalProps = allowed
if !allowed && len(errorMessage) > 0 {
s.additionalPropsError = toErrorMessage(errorMessage[0])
}
return s
}
// Required/Optional/Nullable control
// Optional marks the schema as optional
func (s *ObjectSchema) Optional() *ObjectSchema {
s.Schema.required = false
return s
}
// Required marks the schema as required (default behavior) with optional custom error message
func (s *ObjectSchema) Required(errorMessage ...interface{}) *ObjectSchema {
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 *ObjectSchema) Nullable() *ObjectSchema {
s.nullable = true
return s
}
// Error customization
// TypeError sets a custom error message for type mismatch validation
func (s *ObjectSchema) TypeError(message string) *ObjectSchema {
s.typeMismatchError = toErrorMessage(message)
return s
}
// PropertyError sets a custom error prefix for property validation errors
func (s *ObjectSchema) PropertyError(message string) *ObjectSchema {
s.propertyError = toErrorMessage(message)
return s
}
// Getters for accessing private fields
// IsRequired returns whether the schema is marked as required
func (s *ObjectSchema) IsRequired() bool {
return s.Schema.required
}
// IsOptional returns whether the schema is marked as optional
func (s *ObjectSchema) IsOptional() bool {
return !s.Schema.required
}
// IsNullable returns whether the schema allows nil values
func (s *ObjectSchema) IsNullable() bool {
return s.nullable
}
// GetProperties returns the object properties
func (s *ObjectSchema) GetProperties() map[string]ObjectProperty {
return s.properties
}
// GetRequiredProperties returns the list of required property names
func (s *ObjectSchema) GetRequiredProperties() []string {
return s.requiredProps
}
// AllowsAdditionalProperties returns whether additional properties are allowed
func (s *ObjectSchema) AllowsAdditionalProperties() bool {
return s.additionalProps
}
// GetMinProperties returns the minimum number of properties
func (s *ObjectSchema) GetMinProperties() *int {
return s.minProps
}
// GetMaxProperties returns the maximum number of properties
func (s *ObjectSchema) GetMaxProperties() *int {
return s.maxProps
}
// Helper methods for converting input to map[string]interface{}
// convertToMap converts various input types to map[string]interface{}
func convertToMap(value interface{}) (map[string]interface{}, bool) {
v := reflect.ValueOf(value)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
switch v.Kind() {
case reflect.Map:
// Convert map to map[string]interface{}
result := make(map[string]interface{})
for _, key := range v.MapKeys() {
keyStr := fmt.Sprintf("%v", key.Interface())
result[keyStr] = v.MapIndex(key).Interface()
}
return result, true
case reflect.Struct:
// Convert struct to map[string]interface{}
result := make(map[string]interface{})
structType := v.Type()
for i := 0; i < v.NumField(); i++ {
field := structType.Field(i)
if !field.IsExported() {
continue // Skip unexported fields
}
// Use json tag if available, otherwise use field name
fieldName := field.Name
if tag := field.Tag.Get("json"); tag != "" && tag != "-" {
// Handle "fieldname,omitempty" format
commaIdx := len(tag)
for idx := 0; idx < len(tag); idx++ {
if tag[idx] == ',' {
commaIdx = idx
break
}
}
fieldName = tag[:commaIdx]
}
result[fieldName] = v.Field(i).Interface()
}
return result, true
default:
return nil, false
}
}
// Validation
// Parse validates and parses an object value, returning the final parsed value
func (s *ObjectSchema) 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 := objectRequiredError(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 and convert to map
objectMap, ok := convertToMap(value)
if !ok {
message := objectTypeError(ctx.Locale)
if !isEmptyErrorMessage(s.typeMismatchError) {
message = resolveErrorMessage(s.typeMismatchError, ctx)
}
return ParseResult{
Valid: false,
Value: nil,
Errors: []ValidationError{NewPrimitiveError(value, message, "invalid_type")},
}
}
// Now validate the object against all constraints
finalValue := make(map[string]interface{}, len(objectMap)) // This will be our parsed object
// Validate property count constraints
propCount := len(objectMap)
if s.minProps != nil && propCount < *s.minProps {
message := objectMinPropsError(*s.minProps)(ctx.Locale)
if !isEmptyErrorMessage(s.minPropsError) {
message = resolveErrorMessage(s.minPropsError, ctx)
}
errors = append(errors, NewPrimitiveError(objectMap, message, "min_properties"))
}
if s.maxProps != nil && propCount > *s.maxProps {
message := objectMaxPropsError(*s.maxProps)(ctx.Locale)
if !isEmptyErrorMessage(s.maxPropsError) {
message = resolveErrorMessage(s.maxPropsError, ctx)
}
errors = append(errors, NewPrimitiveError(objectMap, message, "max_properties"))
}
// Check required properties
for _, requiredProp := range s.requiredProps {
if _, exists := objectMap[requiredProp]; !exists {
message := objectRequiredPropError(requiredProp)(ctx.Locale)
errors = append(errors, NewFieldError([]string{requiredProp}, "<missing>", message, "required"))
}
}
// Validate each property
for propName, propValue := range objectMap {
// Check if property is defined in schema
propSchema, isDefined := s.properties[propName]
if !isDefined {
if !s.additionalProps {
message := objectAdditionalPropsError(ctx.Locale)
if !isEmptyErrorMessage(s.additionalPropsError) {
message = resolveErrorMessage(s.additionalPropsError, ctx)
}
errors = append(errors, NewFieldError([]string{propName}, propValue, message, "additional_property"))
} else {
// Additional property allowed, use as-is
finalValue[propName] = propValue
}
continue
}
// Validate the property value using its schema
propResult := propSchema.Schema.Parse(propValue, ctx)
if !propResult.Valid {
// Property validation failed
message := objectPropertyError(propName)(ctx.Locale)
if !isEmptyErrorMessage(s.propertyError) {
message = resolveErrorMessage(s.propertyError, ctx)
}
// Add the main property error
errors = append(errors, NewFieldError([]string{propName}, propValue, message, "property_invalid"))
// Also add the specific validation errors for this property
for _, propErr := range propResult.Errors {
// Prefix the path with property name
errors = append(errors, NewFieldError(append([]string{propName}, propErr.Path...), propErr.Value, propErr.Message, propErr.Code))
}
} else {
// Use the parsed value from property validation
finalValue[propName] = propResult.Value
}
}
return ParseResult{
Valid: len(errors) == 0,
Value: finalValue,
Errors: errors,
}
}
// JSON generates JSON Schema representation
func (s *ObjectSchema) JSON() map[string]interface{} {
schema := baseJSONSchema("object")
// 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 object-specific fields
if len(s.properties) > 0 {
properties := make(map[string]interface{})
for name, prop := range s.properties {
if jsonSchema, ok := prop.Schema.(interface{ JSON() map[string]interface{} }); ok {
properties[name] = jsonSchema.JSON()
}
}
schema["properties"] = properties
}
if len(s.requiredProps) > 0 {
schema["required"] = s.requiredProps
}
schema["additionalProperties"] = s.additionalProps
if s.minProps != nil {
schema["minProperties"] = *s.minProps
}
if s.maxProps != nil {
schema["maxProperties"] = *s.maxProps
}
// Add nullable if true
if s.nullable {
schema["type"] = []string{"object", "null"}
}
return schema
}
// MarshalJSON implements json.Marshaler to properly serialize ObjectSchema for JSON schema generation
func (s *ObjectSchema) MarshalJSON() ([]byte, error) {
type jsonObjectSchema struct {
Schema
Properties map[string]ObjectProperty `json:"properties"`
RequiredProps []string `json:"required,omitempty"`
AdditionalProps bool `json:"additionalProperties"`
MinProps *int `json:"minProperties,omitempty"`
MaxProps *int `json:"maxProperties,omitempty"`
Nullable bool `json:"nullable,omitempty"`
}
return json.Marshal(jsonObjectSchema{
Schema: s.Schema,
Properties: s.properties,
RequiredProps: s.requiredProps,
AdditionalProps: s.additionalProps,
MinProps: s.minProps,
MaxProps: s.maxProps,
Nullable: s.nullable,
})
}
// Interface implementations for ObjectSchema
// SetTitle implements SetTitle interface
func (s *ObjectSchema) SetTitle(title string) {
s.Title(title)
}
// SetDescription implements SetDescription interface
func (s *ObjectSchema) SetDescription(description string) {
s.Description(description)
}
// SetRequired implements SetRequired interface
func (s *ObjectSchema) SetRequired() {
s.Required()
}
// SetOptional implements SetOptional interface
func (s *ObjectSchema) SetOptional() {
s.Optional()
}
// SetNullable implements SetNullable interface
func (s *ObjectSchema) SetNullable() {
s.Nullable()
}
// SetDefault implements SetDefault interface
func (s *ObjectSchema) SetDefault(value interface{}) {
s.Default(value)
}
// SetExample implements SetExample interface
func (s *ObjectSchema) SetExample(example interface{}) {
if val, ok := example.(map[string]interface{}); ok {
s.Example(val)
}
}