-
Notifications
You must be signed in to change notification settings - Fork 6
/
zod_test.go
906 lines (769 loc) · 20.2 KB
/
zod_test.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
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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
package supervillain
import (
"encoding/json"
"fmt"
"reflect"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestFieldName(t *testing.T) {
assert.Equal(t,
fieldName(reflect.StructField{Name: "RCONPassword"}),
"RCONPassword",
)
assert.Equal(t,
fieldName(reflect.StructField{Name: "LANMode"}),
"LANMode",
)
assert.Equal(t,
fieldName(reflect.StructField{Name: "ABC"}),
"ABC",
)
}
func TestFieldNameJsonTag(t *testing.T) {
type S struct {
NotTheFieldName string `json:"fieldName"`
}
assert.Equal(t,
fieldName(reflect.TypeOf(S{}).Field((0))),
"fieldName",
)
}
func TestFieldNameJsonTagOmitEmpty(t *testing.T) {
type S struct {
NotTheFieldName string `json:"fieldName,omitempty"`
}
assert.Equal(t,
fieldName(reflect.TypeOf(S{}).Field((0))),
"fieldName",
)
}
func TestSchemaName(t *testing.T) {
assert.Equal(t,
schemaName("", "User"),
"UserSchema",
)
assert.Equal(t,
schemaName("Bot", "User"),
"BotUserSchema",
)
}
func TestStructSimple(t *testing.T) {
type User struct {
Name string
Age int
Height float64
}
assert.Equal(t,
`export const UserSchema = z.object({
Name: z.string(),
Age: z.number(),
Height: z.number(),
})
export type User = z.infer<typeof UserSchema>
`,
StructToZodSchema(User{}))
}
func TestStructSimpleWithOmittedField(t *testing.T) {
type User struct {
Name string
Age int
Height float64
NotExported string `json:"-"`
}
assert.Equal(t,
`export const UserSchema = z.object({
Name: z.string(),
Age: z.number(),
Height: z.number(),
})
export type User = z.infer<typeof UserSchema>
`,
StructToZodSchema(User{}))
}
func TestStructSimplePrefix(t *testing.T) {
type User struct {
Name string
Age int
Height float64
}
assert.Equal(t,
`export const BotUserSchema = z.object({
Name: z.string(),
Age: z.number(),
Height: z.number(),
})
export type BotUser = z.infer<typeof BotUserSchema>
`,
StructToZodSchemaWithPrefix("Bot", User{}))
}
func TestStringArray(t *testing.T) {
type User struct {
Tags []string
}
assert.Equal(t,
`export const UserSchema = z.object({
Tags: z.string().array().nullable(),
})
export type User = z.infer<typeof UserSchema>
`,
StructToZodSchema(User{}))
}
func TestStructArray(t *testing.T) {
type User struct {
Favourites []struct {
Name string
}
}
assert.Equal(t,
`export const UserSchema = z.object({
Favourites: z.object({
Name: z.string(),
}).array().nullable(),
})
export type User = z.infer<typeof UserSchema>
`,
StructToZodSchema(User{}))
}
func TestStructArrayOptional(t *testing.T) {
type User struct {
Favourites []struct {
Name string
} `json:",omitempty"`
}
assert.Equal(t,
`export const UserSchema = z.object({
Favourites: z.object({
Name: z.string(),
}).array().optional(),
})
export type User = z.infer<typeof UserSchema>
`,
StructToZodSchema(User{}))
}
func TestStructArrayOptionalNullable(t *testing.T) {
type User struct {
Favourites *[]struct {
Name string
} `json:",omitempty"`
}
assert.Equal(t,
`export const UserSchema = z.object({
Favourites: z.object({
Name: z.string(),
}).array().optional().nullable(),
})
export type User = z.infer<typeof UserSchema>
`,
StructToZodSchema(User{}))
}
func TestStringOptional(t *testing.T) {
type User struct {
Name string
Nickname string `json:",omitempty"`
}
assert.Equal(t,
`export const UserSchema = z.object({
Name: z.string(),
Nickname: z.string().optional(),
})
export type User = z.infer<typeof UserSchema>
`,
StructToZodSchema(User{}))
}
func TestStringNullable(t *testing.T) {
type User struct {
Name string
Nickname *string
}
assert.Equal(t,
`export const UserSchema = z.object({
Name: z.string(),
Nickname: z.string().nullable(),
})
export type User = z.infer<typeof UserSchema>
`,
StructToZodSchema(User{}))
}
func TestStringOptionalNotNullable(t *testing.T) {
type User struct {
Name string
Nickname *string `json:",omitempty"` // nil values are omitted
}
assert.Equal(t,
`export const UserSchema = z.object({
Name: z.string(),
Nickname: z.string().optional(),
})
export type User = z.infer<typeof UserSchema>
`,
StructToZodSchema(User{}))
}
func TestStringOptionalNullable(t *testing.T) {
type User struct {
Name string
Nickname **string `json:",omitempty"` // nil values are omitted
}
assert.Equal(t,
`export const UserSchema = z.object({
Name: z.string(),
Nickname: z.string().optional().nullable(),
})
export type User = z.infer<typeof UserSchema>
`,
StructToZodSchema(User{}))
}
func TestStringArrayNullable(t *testing.T) {
type User struct {
Name string
Tags []*string
}
assert.Equal(t,
`export const UserSchema = z.object({
Name: z.string(),
Tags: z.string().array().nullable(),
})
export type User = z.infer<typeof UserSchema>
`,
StructToZodSchema(User{}))
}
func TestBytes(t *testing.T) {
type Message struct {
Data []byte
}
assert.Equal(t,
`export const MessageSchema = z.object({
Data: z.string().nullable(),
})
export type Message = z.infer<typeof MessageSchema>
`,
StructToZodSchema(Message{}))
}
func TestInterfaceAny(t *testing.T) {
type User struct {
Name string
Metadata interface{}
}
assert.Equal(t,
`export const UserSchema = z.object({
Name: z.string(),
Metadata: z.any(),
})
export type User = z.infer<typeof UserSchema>
`,
StructToZodSchema(User{}))
}
func TestInterfacePointerAny(t *testing.T) {
type User struct {
Name string
Metadata *interface{}
}
assert.Equal(t,
`export const UserSchema = z.object({
Name: z.string(),
Metadata: z.any(),
})
export type User = z.infer<typeof UserSchema>
`,
StructToZodSchema(User{}))
}
func TestInterfaceEmptyAny(t *testing.T) {
type User struct {
Name string
Metadata interface{} `json:",omitempty"`
}
assert.Equal(t,
`export const UserSchema = z.object({
Name: z.string(),
Metadata: z.any(),
})
export type User = z.infer<typeof UserSchema>
`,
StructToZodSchema(User{}))
}
func TestInterfacePointerEmptyAny(t *testing.T) {
type User struct {
Name string
Metadata *interface{} `json:",omitempty"`
}
assert.Equal(t,
`export const UserSchema = z.object({
Name: z.string(),
Metadata: z.any(),
})
export type User = z.infer<typeof UserSchema>
`,
StructToZodSchema(User{}))
}
func TestMapStringToString(t *testing.T) {
type User struct {
Name string
Metadata map[string]string
}
assert.Equal(t,
`export const UserSchema = z.object({
Name: z.string(),
Metadata: z.record(z.string(), z.string()).nullable(),
})
export type User = z.infer<typeof UserSchema>
`,
StructToZodSchema(User{}))
}
func TestMapStringToInterface(t *testing.T) {
type User struct {
Name string
Metadata map[string]interface{}
}
assert.Equal(t,
`export const UserSchema = z.object({
Name: z.string(),
Metadata: z.record(z.string(), z.any()).nullable(),
})
export type User = z.infer<typeof UserSchema>
`,
StructToZodSchema(User{}))
}
func TestMapWithStruct(t *testing.T) {
type PostWithMetaData struct {
Title string
}
type User struct {
MapWithStruct map[string]PostWithMetaData
}
assert.Equal(t,
`export const PostWithMetaDataSchema = z.object({
Title: z.string(),
})
export type PostWithMetaData = z.infer<typeof PostWithMetaDataSchema>
export const UserSchema = z.object({
MapWithStruct: z.record(z.string(), PostWithMetaDataSchema).nullable(),
})
export type User = z.infer<typeof UserSchema>
`, StructToZodSchema(User{}))
}
func TestEverything(t *testing.T) {
// The order matters PostWithMetaData needs to be declared after post otherwise it will raise a
// `Block-scoped variable 'Post' used before its declaration.` typescript error.
type Post struct {
Title string
}
type PostWithMetaData struct {
Title string
Post Post
}
type User struct {
Name string
Nickname *string // pointers become optional
Age int
Height float64
OldPostWithMetaData PostWithMetaData
Tags []string
TagsOptional []string `json:",omitempty"` // slices with omitempty cannot be null
TagsOptionalNullable *[]string `json:",omitempty"` // pointers to slices with omitempty can be null or undefined
Favourites []struct { // nested structs are kept inline
Name string
}
Posts []Post // external structs are emitted as separate exports
Post Post `json:",omitempty"` // this tag is ignored because structs don't have an empty value
PostOptional *Post `json:",omitempty"` // single struct pointers with omitempty cannot be null
PostOptionalNullable **Post `json:",omitempty"` // double struct pointers with omitempty can be null
Metadata map[string]string // maps can be null
MetadataOptional map[string]string `json:",omitempty"` // maps with omitempty cannot be null
MetadataOptionalNullable *map[string]string `json:",omitempty"` // pointers to maps with omitempty can be null or undefined
ExtendedProps interface{} // interfaces are just "any" even though they can be null
ExtendedPropsOptional interface{} `json:",omitempty"` // interfaces with omitempty are still just "any"
ExtendedPropsNullable *interface{} // pointers to interfaces are just "any"
ExtendedPropsOptionalNullable *interface{} `json:",omitempty"` // pointers to interfaces with omitempty are also just "any"
ExtendedPropsVeryIndirect ****interface{} // interfaces are always "any" no matter the levels of indirection
NewPostWithMetaData PostWithMetaData
VeryNewPost Post
MapWithStruct map[string]PostWithMetaData
}
assert.Equal(t,
`export const PostSchema = z.object({
Title: z.string(),
})
export type Post = z.infer<typeof PostSchema>
export const PostWithMetaDataSchema = z.object({
Title: z.string(),
Post: PostSchema,
})
export type PostWithMetaData = z.infer<typeof PostWithMetaDataSchema>
export const UserSchema = z.object({
Name: z.string(),
Nickname: z.string().nullable(),
Age: z.number(),
Height: z.number(),
OldPostWithMetaData: PostWithMetaDataSchema,
Tags: z.string().array().nullable(),
TagsOptional: z.string().array().optional(),
TagsOptionalNullable: z.string().array().optional().nullable(),
Favourites: z.object({
Name: z.string(),
}).array().nullable(),
Posts: PostSchema.array().nullable(),
Post: PostSchema,
PostOptional: PostSchema.optional(),
PostOptionalNullable: PostSchema.optional().nullable(),
Metadata: z.record(z.string(), z.string()).nullable(),
MetadataOptional: z.record(z.string(), z.string()).optional(),
MetadataOptionalNullable: z.record(z.string(), z.string()).optional().nullable(),
ExtendedProps: z.any(),
ExtendedPropsOptional: z.any(),
ExtendedPropsNullable: z.any(),
ExtendedPropsOptionalNullable: z.any(),
ExtendedPropsVeryIndirect: z.any(),
NewPostWithMetaData: PostWithMetaDataSchema,
VeryNewPost: PostSchema,
MapWithStruct: z.record(z.string(), PostWithMetaDataSchema).nullable(),
})
export type User = z.infer<typeof UserSchema>
`, StructToZodSchema(User{}))
}
func TestConvertSlice(t *testing.T) {
type Foo struct {
Bar string
Baz string
Quz string
}
type Zip struct {
Zap *Foo
}
type Whim struct {
Wham *Foo
}
c := NewConverter(map[string]CustomFn{})
types := []interface{}{
Zip{},
Whim{},
}
assert.Equal(t,
`export const ZipSchema = z.object({
Zap: FooSchema.nullable(),
})
export type Zip = z.infer<typeof ZipSchema>
export const FooSchema = z.object({
Bar: z.string(),
Baz: z.string(),
Quz: z.string(),
})
export type Foo = z.infer<typeof FooSchema>
export const WhimSchema = z.object({
Wham: FooSchema.nullable(),
})
export type Whim = z.infer<typeof WhimSchema>
`, c.ConvertSlice(types))
}
func TestStructTime(t *testing.T) {
type User struct {
Name string
When time.Time
}
assert.Equal(t,
`export const UserSchema = z.object({
Name: z.string(),
When: z.string(),
})
export type User = z.infer<typeof UserSchema>
`,
StructToZodSchema(User{}))
}
func TestCustom(t *testing.T) {
c := NewConverter(map[string]CustomFn{
"github.com/Southclaws/supervillain.Decimal": func(c *Converter, t reflect.Type, s, g string, i int) string {
return "z.string()"
},
})
type Decimal struct {
value int
exponent int
}
type User struct {
Name string
Money Decimal
}
assert.Equal(t,
`export const UserSchema = z.object({
Name: z.string(),
Money: z.string(),
})
export type User = z.infer<typeof UserSchema>
`,
c.Convert(User{}))
}
type State int
func (s State) ZodSchema() string {
return "z.string()"
}
func TestZodSchemaConstant(t *testing.T) {
type Job struct {
State State
}
assert.Equal(t,
`export const JobSchema = z.object({
State: z.string(),
})
export type Job = z.infer<typeof JobSchema>
`,
StructToZodSchema(Job{}))
}
type StatePtrReceiver int
func (s *StatePtrReceiver) ZodSchema() string {
return "z.string()"
}
func TestZodSchemaConstantPtrReceiver(t *testing.T) {
type Job struct {
State StatePtrReceiver
}
assert.Equal(t,
`export const JobSchema = z.object({
State: z.string(),
})
export type Job = z.infer<typeof JobSchema>
`,
StructToZodSchema(Job{}))
}
type Set[T comparable] map[T]struct{}
func (s Set[T]) ZodSchema(c *Converter, t reflect.Type, name, generic string, indent int) string {
return fmt.Sprintf("%s.array()", c.ConvertType(t.Key(), name, indent))
}
func TestZodSchemaDynamic(t *testing.T) {
type User struct {
Nicknames Set[string]
FavoriteNumbers Set[int]
}
assert.Equal(t,
`export const UserSchema = z.object({
Nicknames: z.string().array(),
FavoriteNumbers: z.number().array(),
})
export type User = z.infer<typeof UserSchema>
`,
StructToZodSchema(User{}))
}
type Set2[T comparable] map[T]struct{}
func (s Set2[T]) ZodSchema(convert func(t reflect.Type, name string, indent int) string, t reflect.Type, name, generic string, indent int) string {
return fmt.Sprintf("%s.array()", convert(t.Key(), name, indent))
}
func TestZodSchemaDynamicFunction(t *testing.T) {
type User struct {
Nicknames Set2[string]
FavoriteNumbers Set2[int]
}
assert.Equal(t,
`export const UserSchema = z.object({
Nicknames: z.string().array(),
FavoriteNumbers: z.number().array(),
})
export type User = z.infer<typeof UserSchema>
`,
StructToZodSchema(User{}))
}
type Strange int
func (s Strange) ZodSchema(weird int, signature string) int {
return int(s)
}
func TestZodSchemaUnexpected(t *testing.T) {
type User struct {
Strange Strange
}
assert.Panics(t, func() {
StructToZodSchema(User{})
})
}
type StateWithoutSchema int
func (s StateWithoutSchema) MarshalJSON() ([]byte, error) {
return json.Marshal(s.String())
}
func (s StateWithoutSchema) String() string {
return fmt.Sprint(int(s))
}
func TestStrictCustom(t *testing.T) {
type Job struct {
State StateWithoutSchema
}
c := NewConverter(map[string]CustomFn{}, WithStrictCustomSchemas(true))
assert.Panics(t, func() {
c.Convert(Job{})
})
c2 := NewConverter(map[string]CustomFn{}, WithStrictCustomSchemas(false))
assert.Equal(t,
`export const JobSchema = z.object({
State: z.number(),
})
export type Job = z.infer<typeof JobSchema>
`, c2.Convert(Job{}))
c3 := NewConverter(map[string]CustomFn{} /* defaults to false */)
assert.Equal(t,
`export const JobSchema = z.object({
State: z.number(),
})
export type Job = z.infer<typeof JobSchema>
`, c3.Convert(Job{}))
}
type StateWithoutSchemaPtrReceiver int
func (s *StateWithoutSchemaPtrReceiver) MarshalJSON() ([]byte, error) {
return json.Marshal(s.String())
}
func (s *StateWithoutSchemaPtrReceiver) String() string {
return fmt.Sprint(int(*s))
}
func TestStrictCustomPtrReceiver(t *testing.T) {
type Job struct {
State StateWithoutSchemaPtrReceiver
}
c := NewConverter(map[string]CustomFn{}, WithStrictCustomSchemas(true))
assert.Panics(t, func() {
c.Convert(Job{})
})
c2 := NewConverter(map[string]CustomFn{}, WithStrictCustomSchemas(false))
assert.Equal(t,
`export const JobSchema = z.object({
State: z.number(),
})
export type Job = z.infer<typeof JobSchema>
`, c2.Convert(Job{}))
c3 := NewConverter(map[string]CustomFn{} /* defaults to false */)
assert.Equal(t,
`export const JobSchema = z.object({
State: z.number(),
})
export type Job = z.infer<typeof JobSchema>
`, c3.Convert(Job{}))
}
func TestInlineStructField(t *testing.T) {
type TestInline struct {
InlineField1 string `json:"inlineField1"`
InlineField2 *string `json:"inlineField2,omitempty"`
}
type Embedded struct {
EmbeddedField1 string `json:"embeddedField1"`
}
type TestInline2 struct {
InlineField3 string `json:"inlineField3"`
Embedded `json:"embedded"`
}
type Test struct {
*TestInline `json:",inline"`
TestInline2 `json:",inline"`
TestField string `json:"testField"`
}
assert.Equal(t,
`export const EmbeddedSchema = z.object({
embeddedField1: z.string(),
})
export type Embedded = z.infer<typeof EmbeddedSchema>
export const TestSchema = z.object({
inlineField1: z.string(),
inlineField2: z.string().optional(),
inlineField3: z.string(),
embedded: EmbeddedSchema,
testField: z.string(),
})
export type Test = z.infer<typeof TestSchema>
`, StructToZodSchema(Test{}))
}
func TestDeeplyNestedInlineStructs(t *testing.T) {
type BaseStruct struct {
MainField string `json:"mainField"`
OptionalInfo *string `json:"optionalInfo,omitempty"`
}
type ExtendedStruct struct {
*BaseStruct `json:",inline"`
ExtraField1 *int `json:"extraField1,omitempty"`
ExtraField2 *string `json:"extraField2,omitempty"`
}
type NestedStruct struct {
*ExtendedStruct `json:",inline"`
UniqueField string `json:"uniqueField"`
}
type RootStruct struct {
*BaseStruct `json:",inline"`
NestedArray []NestedStruct `json:"nestedArray"`
}
assert.Equal(t,
`export const NestedStructSchema = z.object({
mainField: z.string(),
optionalInfo: z.string().optional(),
extraField1: z.number().optional(),
extraField2: z.string().optional(),
uniqueField: z.string(),
})
export type NestedStruct = z.infer<typeof NestedStructSchema>
export const RootStructSchema = z.object({
mainField: z.string(),
optionalInfo: z.string().optional(),
nestedArray: NestedStructSchema.array().nullable(),
})
export type RootStruct = z.infer<typeof RootStructSchema>
`, StructToZodSchema(RootStruct{}))
}
func TestMultipleLevelsInlineEmbedding(t *testing.T) {
type BaseStruct struct {
BaseField string `json:"baseField"`
}
type MiddleStruct struct {
*BaseStruct
MiddleField int `json:"middleField"`
}
type TopStruct struct {
MiddleStruct
TopField bool `json:"topField"`
}
assert.Equal(t,
`export const BaseStructSchema = z.object({
baseField: z.string(),
})
export type BaseStruct = z.infer<typeof BaseStructSchema>
export const MiddleStructSchema = z.object({
BaseStruct: BaseStructSchema.nullable(),
middleField: z.number(),
})
export type MiddleStruct = z.infer<typeof MiddleStructSchema>
export const TopStructSchema = z.object({
MiddleStruct: MiddleStructSchema,
topField: z.boolean(),
})
export type TopStruct = z.infer<typeof TopStructSchema>
`, StructToZodSchema(TopStruct{}))
}
func TestDuplicatedInlineFields(t *testing.T) {
type InlineStruct struct {
Field string `json:"field,omitempty"`
AnotherField string `json:"anotherField,omitempty"`
}
type BaseStruct struct {
Field string `json:"field"`
InlineStruct `json:",inline"`
}
assert.Equal(t,
`export const BaseStructSchema = z.object({
field: z.string(),
anotherField: z.string().optional(),
})
export type BaseStruct = z.infer<typeof BaseStructSchema>
`, StructToZodSchema(BaseStruct{}))
}
func TestDuplicatedInlineStructs(t *testing.T) {
type Struct struct {
Field string `json:"field"`
}
type InlineStruct struct {
Struct *Struct `json:"struct,omitempty"`
}
type BaseStruct struct {
InlineStruct `json:",inline"`
Struct *Struct `json:"struct,omitempty"`
}
assert.Equal(t,
`export const StructSchema = z.object({
field: z.string(),
})
export type Struct = z.infer<typeof StructSchema>
export const BaseStructSchema = z.object({
struct: StructSchema.optional(),
})
export type BaseStruct = z.infer<typeof BaseStructSchema>
`, StructToZodSchema(BaseStruct{}))
}