-
Notifications
You must be signed in to change notification settings - Fork 2
/
zod.go
1109 lines (957 loc) · 33.2 KB
/
zod.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
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package zen
import (
"fmt"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
)
// NewConverter initializes and returns a new converter instance. The custom handler
// function map should be keyed on the fully qualified type name (excluding generic
// type arguments), ie. package.typename.
func NewConverter(custom map[string]CustomFn) Converter {
c := Converter{
prefix: "",
outputs: make(map[string]entry),
custom: custom,
}
return c
}
// AddType converts a struct type to corresponding zod schema. AddType can be called
// multiple times, followed by Export to get the corresonding zod schemas.
func (c *Converter) AddType(input interface{}) {
t := reflect.TypeOf(input)
if t.Kind() != reflect.Struct {
panic("input must be a struct")
}
name := typeName(t)
if _, ok := c.outputs[name]; ok {
return
}
data := c.convertStructTopLevel(t)
order := c.structs
c.outputs[name] = entry{order, data}
c.structs = order + 1
}
// Convert returns zod schema corresponding to a struct type. Its a shorthand for
// call to AddType followed by Export. So calling Convert after other calls to
// AddType/Convert/ConvertSlice, returns schemas from those previous calls as well.
// Calling AddType followed by Export might be more appropriate in such scenarios.
func (c *Converter) Convert(input interface{}) string {
c.AddType(input)
return c.Export()
}
// ConvertSlice returns zod schemas corresponding to multiple struct types passed
// in the argument. Similar to Convert, calling ConvertSlice after other calls to
// AddType/Convert/ConvertSlice, returns schemas from those previous calls as well.
// Calling AddType followed by Export might be more appropriate in such scenarios.
func (c *Converter) ConvertSlice(inputs []interface{}) string {
for _, input := range inputs {
c.AddType(input)
}
return c.Export()
}
// StructToZodSchema returns zod schema corresponding to a struct type.
func StructToZodSchema(input interface{}) string {
c := Converter{
prefix: "",
outputs: make(map[string]entry),
}
return c.Convert(input)
}
// StructToZodSchemaWithPrefix returns zod schema corresponding to a struct type.
// The prefix is added to the generated schema and type names.
func StructToZodSchemaWithPrefix(prefix string, input interface{}) string {
c := Converter{
prefix: prefix,
outputs: make(map[string]entry),
}
return c.Convert(input)
}
var typeMapping = map[reflect.Kind]string{
reflect.Bool: "boolean",
reflect.Int: "number",
reflect.Int8: "number",
reflect.Int16: "number",
reflect.Int32: "number",
reflect.Int64: "number",
reflect.Uint: "number",
reflect.Uint8: "number",
reflect.Uint16: "number",
reflect.Uint32: "number",
reflect.Uint64: "number",
reflect.Uintptr: "number",
reflect.Float32: "number",
reflect.Float64: "number",
reflect.Complex64: "number",
reflect.Complex128: "number",
reflect.String: "string",
reflect.Interface: "any",
}
type entry struct {
order int
data string
}
type byOrder []entry
func (a byOrder) Len() int { return len(a) }
func (a byOrder) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byOrder) Less(i, j int) bool { return a[i].order < a[j].order }
type CustomFn func(c *Converter, t reflect.Type, validate string, indent int) string
type meta struct {
name string
selfRef bool
}
type Converter struct {
prefix string
structs int
outputs map[string]entry
custom map[string]CustomFn
stack []meta
ignores []string
}
func (c *Converter) addSchema(name string, data string) {
// First check if the object already exists. If it does do not replace. This is needed for second order
_, ok := c.outputs[name]
if !ok {
order := c.structs
c.outputs[name] = entry{order, data}
c.structs = order + 1
}
}
// Export returns the zod schemas corresponding to all types that have been
// converted so far.
func (c *Converter) Export() string {
output := strings.Builder{}
var sorted []entry
for _, ent := range c.outputs {
sorted = append(sorted, ent)
}
sort.Sort(byOrder(sorted))
for _, ent := range sorted {
output.WriteString(ent.data)
output.WriteString("\n\n")
}
return output.String()
}
func schemaName(prefix, name string) string {
return fmt.Sprintf("%s%sSchema", prefix, name)
}
func fieldName(input reflect.StructField) string {
if json := input.Tag.Get("json"); json != "" {
args := strings.Split(json, ",")
if len(args[0]) > 0 {
return args[0]
}
// This is also valid:
// json:",omitempty"
// so in this case, args[0] will be empty, so fall through to using the
// raw field name.
}
// When Golang marshals a struct to JSON, and it doesn't have any JSON tags
// that give the fields names, it defaults to just using the field's name.
return input.Name
}
func typeName(t reflect.Type) string {
if t.Kind() == reflect.Struct {
return getTypeNameWithGenerics(t.Name())
}
if t.Kind() == reflect.Ptr {
return typeName(t.Elem())
}
if t.Kind() == reflect.Slice {
return typeName(t.Elem())
}
if t.Kind() == reflect.Map {
return typeName(t.Elem())
}
return "UNKNOWN"
}
func (c *Converter) convertStructTopLevel(t reflect.Type) string {
output := strings.Builder{}
name := typeName(t)
c.stack = append(c.stack, meta{name, false})
data := c.convertStruct(t, 0)
fullName := c.prefix + name
top := c.stack[len(c.stack)-1]
if top.selfRef {
output.WriteString(fmt.Sprintf(`export type %s = %s
`, fullName, c.getTypeStruct(t, 0)))
output.WriteString(fmt.Sprintf(
`export const %s: z.ZodType<%s> = %s`, schemaName(c.prefix, name), fullName, data))
} else {
output.WriteString(fmt.Sprintf(
`export const %s = %s
`,
schemaName(c.prefix, name), data))
output.WriteString(fmt.Sprintf(`export type %s = z.infer<typeof %s>`,
fullName, schemaName(c.prefix, name)))
}
c.stack = c.stack[:len(c.stack)-1]
return output.String()
}
func (c *Converter) convertStruct(input reflect.Type, indent int) string {
output := strings.Builder{}
output.WriteString(`z.object({
`)
merges := []string{}
fields := input.NumField()
for i := 0; i < fields; i++ {
field := input.Field(i)
optional := isOptional(field)
nullable := isNullable(field)
line, shouldMerge := c.convertField(field, indent+1, optional, nullable, field.Anonymous)
if !shouldMerge {
output.WriteString(line)
} else {
merges = append(merges, line)
}
}
output.WriteString(indentation(indent))
output.WriteString(`})`)
if len(merges) > 0 {
for _, merge := range merges {
output.WriteString(merge)
}
}
return output.String()
}
func (c *Converter) getTypeStruct(input reflect.Type, indent int) string {
output := strings.Builder{}
output.WriteString(`{
`)
fields := input.NumField()
for i := 0; i < fields; i++ {
field := input.Field(i)
optional := isOptional(field)
nullable := isNullable(field)
line := c.getTypeField(field, indent+1, optional, nullable)
output.WriteString(line)
}
output.WriteString(indentation(indent))
output.WriteString(`}`)
return output.String()
}
var matchGenericTypeName = regexp.MustCompile(`(.+)\[(.+)]`)
// Checking if a reflected type is a generic isn't supported as far as I can see.
// So this simple check looks for a `[` character in the type name: `T1[T2]`.
func isGeneric(t reflect.Type) bool {
return strings.Contains(t.Name(), "[")
}
// Gets the full type name (package+type), stripping out generic type arguments.
func getFullName(t reflect.Type) string {
var typename string
if isGeneric(t) {
m := matchGenericTypeName.FindAllStringSubmatch(t.Name(), 1)[0]
typename = m[1]
} else {
typename = t.Name()
}
return fmt.Sprintf("%s.%s", t.PkgPath(), typename)
}
func (c *Converter) handleCustomType(t reflect.Type, validate string, indent int) (string, bool) {
fullName := getFullName(t)
custom, ok := c.custom[fullName]
if ok {
return custom(c, t, validate, indent), true
}
return "", false
}
// ConvertType should be called from custom converter functions.
func (c *Converter) ConvertType(t reflect.Type, validate string, indent int) string {
if t.Kind() == reflect.Ptr {
inner := t.Elem()
validate = strings.TrimPrefix(validate, "omitempty")
validate = strings.TrimPrefix(validate, ",")
return c.ConvertType(inner, validate, indent)
}
// Custom types should be handled before maps/slices, as we might have
// custom types that are maps/slices.
if custom, ok := c.handleCustomType(t, validate, indent); ok {
return custom
}
if t.Kind() == reflect.Slice || t.Kind() == reflect.Array {
return c.convertSliceAndArray(t, validate, indent)
}
if t.Kind() == reflect.Map {
return c.convertMap(t, validate, indent)
}
if t.Kind() == reflect.Struct {
name := typeName(t)
if name == "" {
// Handle fields with non-defined types - these are inline.
return c.convertStruct(t, indent)
} else if name == "Time" {
var validateStr string
if validate != "" {
// We compare with both the zero value from go and the zero value that zod coerces to
if validate == "required" {
validateStr = ".refine((val) => val.getTime() !== new Date('0001-01-01T00:00:00Z').getTime() && val.getTime() !== new Date(0).getTime(), 'Invalid date')"
}
}
// timestamps are to be coerced to date by zod. JSON.parse only serializes to string
return "z.coerce.date()" + validateStr
} else {
if c.stack[len(c.stack)-1].name == name {
c.stack[len(c.stack)-1].selfRef = true
return fmt.Sprintf("z.lazy(() => %s)", schemaName(c.prefix, name))
}
// throws panic if there is a cycle
detectCycle(name, c.stack)
c.addSchema(name, c.convertStructTopLevel(t))
return schemaName(c.prefix, name)
}
}
// boolean, number, string, any
zodType, ok := typeMapping[t.Kind()]
if !ok {
panic(fmt.Sprint("cannot handle: ", t.Kind()))
}
var validateStr string
if validate != "" {
switch zodType {
case "string":
validateStr = c.validateString(validate)
if strings.Contains(validateStr, ".enum(") {
return "z" + validateStr
}
case "number":
validateStr = c.validateNumber(validate)
}
}
return fmt.Sprintf("z.%s()%s", zodType, validateStr)
}
func (c *Converter) getType(t reflect.Type, indent int) string {
if t.Kind() == reflect.Ptr {
inner := t.Elem()
return c.getType(inner, indent)
}
// TODO: handle types for custom types
if t.Kind() == reflect.Slice || t.Kind() == reflect.Array {
return c.getTypeSliceAndArray(t, indent)
}
if t.Kind() == reflect.Map {
return c.getTypeMap(t, indent)
}
if t.Kind() == reflect.Struct {
name := typeName(t)
if t.Name() == "" {
// Handle fields with non-defined types - these are inline.
return c.getTypeStruct(t, indent)
} else if t.Name() == "Time" {
return "date"
} else {
return c.prefix + name
}
}
zodType, ok := typeMapping[t.Kind()]
if !ok {
panic(fmt.Sprint("cannot handle: ", t.Kind()))
}
return zodType
}
func (c *Converter) convertField(f reflect.StructField, indent int, optional, nullable, anonymous bool) (string, bool) {
name := fieldName(f)
// fields named `-` are not exported to JSON so don't export zod types
if name == "-" {
return "", false
}
// because nullability is processed before custom types, this makes sure
// the custom type has control over nullability.
fullName := getFullName(f.Type)
_, isCustom := c.custom[fullName]
optionalCall := ""
if optional {
optionalCall = ".optional()"
}
nullableCall := ""
if nullable && !isCustom {
nullableCall = ".nullable()"
}
t := c.ConvertType(f.Type, f.Tag.Get("validate"), indent)
if !anonymous {
return fmt.Sprintf(
"%s%s: %s%s%s,\n",
indentation(indent),
name,
t,
optionalCall,
nullableCall), false
} else {
return fmt.Sprintf(".merge(%s)", t), true
}
}
func (c *Converter) getTypeField(f reflect.StructField, indent int, optional, nullable bool) string {
name := fieldName(f)
// fields named `-` are not exported to JSON so don't export types
if name == "-" {
return ""
}
// because nullability is processed before custom types, this makes sure
// the custom type has control over nullability.
fullName := getFullName(f.Type)
_, isCustom := c.custom[fullName]
optionalCallPre := ""
optionalCallUndef := ""
if optional {
optionalCallPre = "?"
optionalCallUndef = " | undefined"
}
nullableCall := ""
if nullable && !isCustom {
nullableCall = " | null"
}
return fmt.Sprintf(
"%s%s%s: %s%s%s,\n",
indentation(indent),
name,
optionalCallPre,
c.getType(f.Type, indent),
nullableCall,
optionalCallUndef)
}
func (c *Converter) convertSliceAndArray(t reflect.Type, validate string, indent int) string {
if t.Kind() == reflect.Array {
return fmt.Sprintf(
"%s.array()%s",
c.ConvertType(t.Elem(), getValidateAfterDive(validate), indent), fmt.Sprintf(".length(%d)", t.Len()))
}
var validateStr strings.Builder
validateCurrent := getValidateCurrent(validate)
if validateCurrent != "" {
parts := strings.Split(validateCurrent, ",")
// eq and ne should be at the end since they output a refine function
sort.SliceStable(parts, func(i, j int) bool {
if strings.HasPrefix(parts[i], "ne") {
return false
}
if strings.HasPrefix(parts[j], "ne") {
return true
}
return i < j
})
for _, part := range parts {
part = strings.TrimSpace(part)
if part == "omitempty" {
} else if part == "dive" {
break
} else if part == "required" {
} else if strings.HasPrefix(part, "min=") {
validateStr.WriteString(fmt.Sprintf(".min(%s)", part[4:]))
} else if strings.HasPrefix(part, "max=") {
validateStr.WriteString(fmt.Sprintf(".max(%s)", part[4:]))
} else if strings.HasPrefix(part, "len=") {
validateStr.WriteString(fmt.Sprintf(".length(%s)", part[4:]))
} else if strings.HasPrefix(part, "eq=") {
validateStr.WriteString(fmt.Sprintf(".length(%s)", part[3:]))
} else if strings.HasPrefix(part, "ne=") {
validateStr.WriteString(fmt.Sprintf(".refine((val) => val.length !== %s)", part[3:]))
} else if strings.HasPrefix(part, "gt=") {
val, err := strconv.Atoi(part[3:])
if err != nil || val < 0 {
panic(fmt.Sprintf("invalid gt value: %s", part[3:]))
}
validateStr.WriteString(fmt.Sprintf(".min(%d)", val+1))
} else if strings.HasPrefix(part, "gte=") {
validateStr.WriteString(fmt.Sprintf(".min(%s)", part[4:]))
} else if strings.HasPrefix(part, "lt=") {
val, err := strconv.Atoi(part[3:])
if err != nil || val <= 0 {
panic(fmt.Sprintf("invalid lt value: %s", part[3:]))
}
validateStr.WriteString(fmt.Sprintf(".max(%d)", val-1))
} else if strings.HasPrefix(part, "lte=") {
validateStr.WriteString(fmt.Sprintf(".max(%s)", part[4:]))
} else {
panic(fmt.Sprintf("unknown validation: %s", part))
}
}
}
return fmt.Sprintf(
"%s.array()%s",
c.ConvertType(t.Elem(), getValidateAfterDive(validate), indent), validateStr.String())
}
func (c *Converter) getTypeSliceAndArray(t reflect.Type, indent int) string {
return fmt.Sprintf(
"%s[]",
c.getType(t.Elem(), indent))
}
func (c *Converter) convertKeyType(t reflect.Type, validate string) string {
if t.Name() == "Time" {
return "z.coerce.date()"
}
// boolean, number, string, any
zodType, ok := typeMapping[t.Kind()]
if !ok || (zodType != "string" && zodType != "number") {
panic(fmt.Sprint("cannot handle key type: ", t.Kind()))
}
var validateStr string
if validate != "" {
switch zodType {
case "string":
validateStr = c.validateString(validate)
if strings.Contains(validateStr, ".enum(") {
return "z" + validateStr
}
case "number":
validateStr = c.validateNumber(validate)
}
}
if zodType == "string" {
return fmt.Sprintf("z.%s()%s", zodType, validateStr)
}
// https://pkg.go.dev/encoding/json#Marshal
// Map values encode as JSON objects. The map's key type must either be a string, an integer type, or implement encoding.TextMarshaler.
return fmt.Sprintf("z.coerce.%s()%s", zodType, validateStr)
}
func (c *Converter) convertMap(t reflect.Type, validate string, indent int) string {
var validateStr strings.Builder
if validate != "" {
parts := strings.Split(validate, ",")
for _, part := range parts {
part = strings.TrimSpace(part)
if part == "omitempty" {
} else if part == "dive" {
break
} else if part == "required" {
validateStr.WriteString(".refine((val) => Object.keys(val).length > 0, 'Empty map')")
} else if strings.HasPrefix(part, "min=") {
validateStr.WriteString(fmt.Sprintf(".refine((val) => Object.keys(val).length >= %s, 'Map too small')", part[4:]))
} else if strings.HasPrefix(part, "max=") {
validateStr.WriteString(fmt.Sprintf(".refine((val) => Object.keys(val).length <= %s, 'Map too large')", part[4:]))
} else if strings.HasPrefix(part, "len=") {
validateStr.WriteString(fmt.Sprintf(".refine((val) => Object.keys(val).length === %s, 'Map wrong size')", part[4:]))
} else if strings.HasPrefix(part, "eq=") {
validateStr.WriteString(fmt.Sprintf(".refine((val) => Object.keys(val).length === %s, 'Map wrong size')", part[3:]))
} else if strings.HasPrefix(part, "ne=") {
validateStr.WriteString(fmt.Sprintf(".refine((val) => Object.keys(val).length !== %s, 'Map wrong size')", part[3:]))
} else if strings.HasPrefix(part, "gt=") {
validateStr.WriteString(fmt.Sprintf(".refine((val) => Object.keys(val).length > %s, 'Map too small')", part[3:]))
} else if strings.HasPrefix(part, "gte=") {
validateStr.WriteString(fmt.Sprintf(".refine((val) => Object.keys(val).length >= %s, 'Map too small')", part[4:]))
} else if strings.HasPrefix(part, "lt=") {
validateStr.WriteString(fmt.Sprintf(".refine((val) => Object.keys(val).length < %s, 'Map too large')", part[3:]))
} else if strings.HasPrefix(part, "lte=") {
validateStr.WriteString(fmt.Sprintf(".refine((val) => Object.keys(val).length <= %s, 'Map too large')", part[4:]))
} else {
panic(fmt.Sprintf("unknown validation: %s", part))
}
}
}
return fmt.Sprintf(`z.record(%s, %s)%s`,
c.convertKeyType(t.Key(), getValidateKeys(validate)),
c.ConvertType(t.Elem(), getValidateValues(validate), indent),
validateStr.String())
}
func (c *Converter) getTypeMap(t reflect.Type, indent int) string {
return fmt.Sprintf(`Record<%s, %s>`,
c.getType(t.Key(), indent),
c.getType(t.Elem(), indent))
}
// Select part of validate string after dive, if it exists.
func getValidateAfterDive(validate string) string {
var validateNext string
if validate != "" {
parts := strings.Split(validate, ",")
for i, part := range parts {
part = strings.TrimSpace(part)
if part == "dive" {
validateNext = strings.Join(parts[i+1:], ",")
break
}
}
}
return validateNext
}
// These are to be used together directly after the dive tag and tells the validator that anything between
// 'keys' and 'endkeys' applies to the keys of a map and not the values; think of it like the 'dive' tag,
// but for map keys instead of values. Multidimensional nesting is also supported, each level you wish to
// validate will require another 'keys' and 'endkeys' tag. These tags are only valid for maps.
//
// Usage: dive,keys,othertagvalidation(s),endkeys,valuevalidationtags
func getValidateKeys(validate string) string {
var validateKeys string
if strings.Contains(validate, "keys") {
removedSuffix := strings.SplitN(validate, ",endkeys", 2)[0]
parts := strings.SplitN(removedSuffix, "keys,", 2)
if len(parts) == 2 {
validateKeys = parts[1]
}
}
return validateKeys
}
func getValidateValues(validate string) string {
var validateValues string
if strings.Contains(validate, "dive,keys") {
removedPrefix := strings.SplitN(validate, ",endkeys", 2)[1]
if strings.Contains(removedPrefix, ",dive") {
validateValues = strings.SplitN(removedPrefix, ",dive", 2)[0]
} else {
validateValues = removedPrefix
}
validateValues = strings.TrimPrefix(validateValues, ",")
} else if strings.Contains(validate, "dive") {
removedPrefix := strings.SplitN(validate, "dive,", 2)[1]
if strings.Contains(removedPrefix, ",dive") {
validateValues = strings.SplitN(removedPrefix, ",dive", 2)[0]
} else {
validateValues = removedPrefix
}
}
return validateValues
}
func (c *Converter) SetIgnores(validations []string) {
c.ignores = validations
}
func (c *Converter) checkIsIgnored(part string) bool {
for _, ignore := range c.ignores {
if part == ignore {
return true
}
}
return false
}
// not implementing omitempty for numbers and strings
// could support unusual cases like `validate:"omitempty,min=3,max=5"`
func (c *Converter) validateNumber(validate string) string {
var validateStr strings.Builder
parts := strings.Split(validate, ",")
// eq and ne should be at the end since they output a refine function
sort.SliceStable(parts, func(i, j int) bool {
if strings.HasPrefix(parts[i], "eq") || strings.HasPrefix(parts[i], "len") ||
strings.HasPrefix(parts[i], "ne") || strings.HasPrefix(parts[i], "oneof") ||
strings.HasPrefix(parts[i], "required") {
return false
}
if strings.HasPrefix(parts[j], "eq") || strings.HasPrefix(parts[j], "len") ||
strings.HasPrefix(parts[j], "ne") || strings.HasPrefix(parts[j], "oneof") ||
strings.HasPrefix(parts[j], "required") {
return true
}
return i < j
})
for _, part := range parts {
part = strings.TrimSpace(part)
if c.checkIsIgnored(part) {
continue
}
if strings.ContainsRune(part, '=') {
idx := strings.Index(part, "=")
if idx == 0 || idx == len(part)-1 {
panic(fmt.Sprintf("invalid validation: %s", part))
}
valName := part[:idx]
valValue := part[idx+1:]
switch valName {
case "gt":
validateStr.WriteString(fmt.Sprintf(".gt(%s)", valValue))
case "gte", "min":
validateStr.WriteString(fmt.Sprintf(".gte(%s)", valValue))
case "lt":
validateStr.WriteString(fmt.Sprintf(".lt(%s)", valValue))
case "lte", "max":
validateStr.WriteString(fmt.Sprintf(".lte(%s)", valValue))
case "eq", "len":
validateStr.WriteString(fmt.Sprintf(".refine((val) => val === %s)", valValue))
case "ne":
validateStr.WriteString(fmt.Sprintf(".refine((val) => val !== %s)", valValue))
case "oneof":
vals := strings.Fields(valValue)
if len(vals) == 0 {
panic(fmt.Sprintf("invalid oneof validation: %s", part))
}
validateStr.WriteString(fmt.Sprintf(".refine((val) => [%s].includes(val))", strings.Join(vals, ", ")))
default:
panic(fmt.Sprintf("unknown validation: %s", part))
}
} else {
switch part {
case "omitempty":
case "required":
validateStr.WriteString(".refine((val) => val !== 0)")
default:
panic(fmt.Sprintf("unknown validation: %s", part))
}
}
}
return validateStr.String()
}
func (c *Converter) validateString(validate string) string {
var validateStr strings.Builder
parts := strings.Split(validate, ",")
// eq and ne should be at the end since they output a refine function
sort.SliceStable(parts, func(i, j int) bool {
if strings.HasPrefix(parts[i], "eq") || strings.HasPrefix(parts[i], "ne") {
return false
}
if strings.HasPrefix(parts[j], "eq") || strings.HasPrefix(parts[j], "ne") {
return true
}
return i < j
})
for _, part := range parts {
part = strings.TrimSpace(part)
if c.checkIsIgnored(part) {
continue
}
// We handle the parts which have = separately
if strings.ContainsRune(part, '=') {
idx := strings.Index(part, "=")
if idx == 0 || idx == len(part)-1 {
panic(fmt.Sprintf("invalid validation: %s", part))
}
valName := part[:idx]
valValue := part[idx+1:]
switch valName {
case "oneof":
vals := splitParamsRegex.FindAllString(part[6:], -1)
for i := 0; i < len(vals); i++ {
vals[i] = strings.Replace(vals[i], "'", "", -1)
}
if len(vals) == 0 {
panic("oneof= must be followed by a list of values")
}
// const FishEnum = z.enum(["Salmon", "Tuna", "Trout"]);
validateStr.WriteString(fmt.Sprintf(".enum([\"%s\"] as const)", strings.Join(vals, "\", \"")))
case "len":
validateStr.WriteString(fmt.Sprintf(".length(%s)", valValue))
case "min":
validateStr.WriteString(fmt.Sprintf(".min(%s)", valValue))
case "max":
validateStr.WriteString(fmt.Sprintf(".max(%s)", valValue))
case "gt":
val, err := strconv.Atoi(valValue)
if err != nil {
panic("gt= must be followed by a number")
}
validateStr.WriteString(fmt.Sprintf(".min(%d)", val+1))
case "gte":
validateStr.WriteString(fmt.Sprintf(".min(%s)", valValue))
case "lt":
val, err := strconv.Atoi(valValue)
if err != nil {
panic("lt= must be followed by a number")
}
validateStr.WriteString(fmt.Sprintf(".max(%d)", val-1))
case "lte":
validateStr.WriteString(fmt.Sprintf(".max(%s)", valValue))
case "contains":
validateStr.WriteString(fmt.Sprintf(".includes(\"%s\")", valValue))
case "endswith":
validateStr.WriteString(fmt.Sprintf(".endsWith(\"%s\")", valValue))
case "startswith":
validateStr.WriteString(fmt.Sprintf(".startsWith(\"%s\")", valValue))
case "eq":
validateStr.WriteString(fmt.Sprintf(".refine((val) => val === \"%s\")", valValue))
case "ne":
validateStr.WriteString(fmt.Sprintf(".refine((val) => val !== \"%s\")", valValue))
default:
panic(fmt.Sprintf("unknown validation: %s", part))
}
} else {
switch part {
case "omitempty":
case "required":
validateStr.WriteString(".min(1)")
case "email":
// email is more readable than copying the regex in regexes.go but could be incompatible
// Also there is an open issue https://github.com/go-playground/validator/issues/517
// https://github.com/puellanivis/pedantic-regexps/blob/master/email.go
// solution is there in the comments but not implemented yet
validateStr.WriteString(".email()")
case "url":
// url is more readable than copying the regex in regexes.go but could be incompatible
validateStr.WriteString(".url()")
case "ipv4":
validateStr.WriteString(".ip({ version: \"v4\" })")
case "ip4_addr":
validateStr.WriteString(".ip({ version: \"v4\" })")
case "ipv6":
validateStr.WriteString(".ip({ version: \"v6\" })")
case "ip6_addr":
validateStr.WriteString(".ip({ version: \"v6\" })")
case "ip":
validateStr.WriteString(".ip()")
case "ip_addr":
validateStr.WriteString(".ip()")
case "http_url":
// url is more readable than copying the regex in regexes.go but could be incompatible
validateStr.WriteString(".url()")
case "url_encoded":
validateStr.WriteString(fmt.Sprintf(".regex(/%s/)", uRLEncodedRegexString))
case "alpha":
validateStr.WriteString(fmt.Sprintf(".regex(/%s/)", alphaRegexString))
case "alphanum":
validateStr.WriteString(fmt.Sprintf(".regex(/%s/)", alphaNumericRegexString))
case "alphanumunicode":
validateStr.WriteString(fmt.Sprintf(".regex(/%s/)", alphaUnicodeNumericRegexString))
case "alphaunicode":
validateStr.WriteString(fmt.Sprintf(".regex(/%s/)", alphaUnicodeRegexString))
case "ascii":
validateStr.WriteString(fmt.Sprintf(".regex(/%s/)", aSCIIRegexString))
case "boolean":
validateStr.WriteString(".enum(['true', 'false'])")
case "lowercase":
validateStr.WriteString(".refine((val) => val === val.toLowerCase())")
case "number":
validateStr.WriteString(fmt.Sprintf(".regex(/%s/)", numberRegexString))
case "numeric":
validateStr.WriteString(fmt.Sprintf(".regex(/%s/)", numericRegexString))
case "uppercase":
validateStr.WriteString(".refine((val) => val === val.toUpperCase())")
case "base64":
validateStr.WriteString(fmt.Sprintf(".regex(/%s/)", base64RegexString))
case "mongodb":
validateStr.WriteString(fmt.Sprintf(".regex(/%s/)", mongodbRegexString))
case "datetime":
validateStr.WriteString(".datetime()")
case "hexadecimal":
validateStr.WriteString(fmt.Sprintf(".regex(/%s/)", hexadecimalRegexString))
case "json":
// TODO: Better error messages with this
// const literalSchema = z.union([z.string(), z.number(), z.boolean(), z.null()]);
//type Literal = z.infer<typeof literalSchema>;
//type Json = Literal | { [key: string]: Json } | Json[];
//const jsonSchema: z.ZodType<Json> = z.lazy(() =>
// z.union([literalSchema, z.array(jsonSchema), z.record(jsonSchema)])
//);
//
//jsonSchema.parse(data);
validateStr.WriteString(".refine((val) => { try { JSON.parse(val); return true } catch { return false } })")
case "jwt":
validateStr.WriteString(fmt.Sprintf(".regex(/%s/)", jWTRegexString))
case "latitude":
validateStr.WriteString(fmt.Sprintf(".regex(/%s/)", latitudeRegexString))
case "longitude":
validateStr.WriteString(fmt.Sprintf(".regex(/%s/)", longitudeRegexString))
case "uuid":
validateStr.WriteString(fmt.Sprintf(".regex(/%s/)", uUIDRegexString))
case "uuid3":
validateStr.WriteString(fmt.Sprintf(".regex(/%s/)", uUID3RegexString))
case "uuid3_rfc4122":
validateStr.WriteString(fmt.Sprintf(".regex(/%s/)", uUID3RFC4122RegexString))
case "uuid4":
validateStr.WriteString(fmt.Sprintf(".regex(/%s/)", uUID4RegexString))
case "uuid4_rfc4122":
validateStr.WriteString(fmt.Sprintf(".regex(/%s/)", uUID4RFC4122RegexString))
case "uuid5":
validateStr.WriteString(fmt.Sprintf(".regex(/%s/)", uUID5RegexString))
case "uuid5_rfc4122":
validateStr.WriteString(fmt.Sprintf(".regex(/%s/)", uUID5RFC4122RegexString))
case "uuid_rfc4122":
validateStr.WriteString(fmt.Sprintf(".regex(/%s/)", uUIDRFC4122RegexString))
case "md4":
validateStr.WriteString(fmt.Sprintf(".regex(/%s/)", md4RegexString))
case "md5":
validateStr.WriteString(fmt.Sprintf(".regex(/%s/)", md5RegexString))
case "sha256":
validateStr.WriteString(fmt.Sprintf(".regex(/%s/)", sha256RegexString))
case "sha384":
validateStr.WriteString(fmt.Sprintf(".regex(/%s/)", sha384RegexString))
case "sha512":
validateStr.WriteString(fmt.Sprintf(".regex(/%s/)", sha512RegexString))
default:
panic(fmt.Sprintf("unknown validation: %s", part))
}
}
}
return validateStr.String()
}
func isNullable(field reflect.StructField) bool {
validateCurrent := getValidateCurrent(field.Tag.Get("validate"))
// interfaces are currently exported with "any" type, which already includes "null"
if isInterface(field) || strings.Contains(validateCurrent, "required") {
return false
}
// If some comparison is present min=1 or max=2 or len=4 etc. then go-validator requires the value
// to be non-nil unless omitempty is also present