@kubb/plugin-zod 4.10.1 → 4.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/parser.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import transformers from '@kubb/core/transformers'
2
2
 
3
- import type { Schema, SchemaKeywordBase, SchemaMapper } from '@kubb/plugin-oas'
4
- import { isKeyword, SchemaGenerator, type SchemaKeywordMapper, type SchemaTree, schemaKeywords } from '@kubb/plugin-oas'
3
+ import type { Schema, SchemaMapper } from '@kubb/plugin-oas'
4
+ import { createParser, findSchemaKeyword, isKeyword, SchemaGenerator, type SchemaKeywordMapper, schemaKeywords } from '@kubb/plugin-oas'
5
5
 
6
6
  //TODO add zodKeywordMapper as function that returns 3 versions: v3, v4 and v4 mini, this can also be used to have the custom mapping(see object type)
7
7
  // also include shouldCoerce
@@ -224,14 +224,24 @@ const zodKeywordMapper = {
224
224
  .filter(Boolean)
225
225
  .join('')
226
226
  },
227
- default: (value?: string | number | true | object, innerSchema?: string, mini?: boolean) => {
227
+ default: (value?: string | number | boolean | object, innerSchema?: string, mini?: boolean) => {
228
228
  if (mini && innerSchema) {
229
229
  const defaultValue = typeof value === 'object' ? '{}' : (value ?? '')
230
230
  return `z._default(${innerSchema}, ${defaultValue})`
231
231
  }
232
+
232
233
  if (typeof value === 'object') {
233
234
  return '.default({})'
234
235
  }
236
+
237
+ if (value === undefined) {
238
+ return '.default()'
239
+ }
240
+
241
+ if (typeof value === 'string' && !value) {
242
+ return `.default('')`
243
+ }
244
+
235
245
  return `.default(${value ?? ''})`
236
246
  },
237
247
  and: (items: string[] = [], mini?: boolean) => {
@@ -469,165 +479,194 @@ type ParserOptions = {
469
479
  mini?: boolean
470
480
  }
471
481
 
472
- export function parse({ schema, parent, current, name, siblings }: SchemaTree, options: ParserOptions): string | undefined {
473
- const value = zodKeywordMapper[current.keyword as keyof typeof zodKeywordMapper]
474
-
475
- // Early exit: if siblings contain both matches and ref → skip matches entirely
476
- const hasMatches = siblings.some((it) => isKeyword(it, schemaKeywords.matches))
477
- const hasRef = siblings.some((it) => isKeyword(it, schemaKeywords.ref))
478
-
479
- if (hasMatches && hasRef && isKeyword(current, schemaKeywords.matches)) {
480
- return undefined // strip matches
481
- }
482
-
483
- if (!value) {
484
- return undefined
485
- }
486
-
487
- if (isKeyword(current, schemaKeywords.union)) {
488
- // zod union type needs at least 2 items
489
- if (Array.isArray(current.args) && current.args.length === 1) {
490
- return parse({ schema, parent, name, current: current.args[0] as Schema, siblings }, options)
491
- }
492
- if (Array.isArray(current.args) && !current.args.length) {
493
- return ''
494
- }
495
-
496
- return zodKeywordMapper.union(
497
- sort(current.args)
498
- .map((it, _index, siblings) => parse({ schema, parent: current, name, current: it, siblings }, options))
499
- .filter(Boolean),
500
- )
501
- }
502
-
503
- if (isKeyword(current, schemaKeywords.and)) {
504
- const items = sort(current.args)
505
- .filter((schema: Schema) => {
506
- return ![schemaKeywords.optional, schemaKeywords.describe].includes(schema.keyword as typeof schemaKeywords.describe)
507
- })
508
- .map((it: Schema, _index, siblings) => parse({ schema, parent: current, name, current: it, siblings }, options))
509
- .filter(Boolean)
510
-
511
- return `${items.slice(0, 1)}${zodKeywordMapper.and(items.slice(1), options.mini)}`
512
- }
482
+ // Create the parser using createParser
483
+ export const parse = createParser<string, ParserOptions>({
484
+ mapper: zodKeywordMapper,
485
+ handlers: {
486
+ union(tree, options) {
487
+ const { current, schema, parent, name, siblings } = tree
488
+ if (!isKeyword(current, schemaKeywords.union)) return undefined
489
+
490
+ // zod union type needs at least 2 items
491
+ if (Array.isArray(current.args) && current.args.length === 1) {
492
+ return this.parse({ schema, parent, name, current: current.args[0] as Schema, siblings }, options)
493
+ }
494
+ if (Array.isArray(current.args) && !current.args.length) {
495
+ return ''
496
+ }
513
497
 
514
- if (isKeyword(current, schemaKeywords.array)) {
515
- return zodKeywordMapper.array(
516
- sort(current.args.items)
517
- .map((it, _index, siblings) => {
518
- return parse({ schema, parent: current, name, current: it, siblings }, options)
498
+ return zodKeywordMapper.union(
499
+ sort(current.args)
500
+ .map((it, _index, siblings) => this.parse({ schema, parent: current, name, current: it, siblings }, options))
501
+ .filter(Boolean),
502
+ )
503
+ },
504
+ and(tree, options) {
505
+ const { current, schema, name } = tree
506
+ if (!isKeyword(current, schemaKeywords.and)) return undefined
507
+
508
+ const items = sort(current.args)
509
+ .filter((schema: Schema) => {
510
+ return ![schemaKeywords.optional, schemaKeywords.describe].includes(schema.keyword as typeof schemaKeywords.describe)
519
511
  })
520
- .filter(Boolean),
521
- current.args.min,
522
- current.args.max,
523
- current.args.unique,
524
- options.mini,
525
- )
526
- }
512
+ .map((it: Schema, _index, siblings) => this.parse({ schema, parent: current, name, current: it, siblings }, options))
513
+ .filter(Boolean)
527
514
 
528
- if (isKeyword(current, schemaKeywords.enum)) {
529
- if (current.args.asConst) {
530
- if (current.args.items.length === 1) {
531
- const child = {
532
- keyword: schemaKeywords.const,
533
- args: current.args.items[0],
534
- }
535
- return parse({ schema, parent: current, name, current: child, siblings: [child] }, options)
536
- }
515
+ return `${items.slice(0, 1)}${zodKeywordMapper.and(items.slice(1), options.mini)}`
516
+ },
517
+ array(tree, options) {
518
+ const { current, schema, name } = tree
519
+ if (!isKeyword(current, schemaKeywords.array)) return undefined
537
520
 
538
- return zodKeywordMapper.union(
539
- current.args.items
540
- .map((schema) => ({
541
- keyword: schemaKeywords.const,
542
- args: schema,
543
- }))
521
+ return zodKeywordMapper.array(
522
+ sort(current.args.items)
544
523
  .map((it, _index, siblings) => {
545
- return parse({ schema, parent: current, name, current: it, siblings }, options)
524
+ return this.parse({ schema, parent: current, name, current: it, siblings }, options)
546
525
  })
547
526
  .filter(Boolean),
527
+ current.args.min,
528
+ current.args.max,
529
+ current.args.unique,
530
+ options.mini,
548
531
  )
549
- }
550
-
551
- return zodKeywordMapper.enum(
552
- current.args.items.map((schema) => {
553
- if (schema.format === 'boolean') {
554
- return transformers.stringify(schema.value)
532
+ },
533
+ enum(tree, options) {
534
+ const { current, schema, name } = tree
535
+ if (!isKeyword(current, schemaKeywords.enum)) return undefined
536
+
537
+ if (current.args.asConst) {
538
+ if (current.args.items.length === 1) {
539
+ const child = {
540
+ keyword: schemaKeywords.const,
541
+ args: current.args.items[0],
542
+ }
543
+ return this.parse({ schema, parent: current, name, current: child, siblings: [child] }, options)
555
544
  }
556
545
 
557
- if (schema.format === 'number') {
546
+ return zodKeywordMapper.union(
547
+ current.args.items
548
+ .map((schema) => ({
549
+ keyword: schemaKeywords.const,
550
+ args: schema,
551
+ }))
552
+ .map((it, _index, siblings) => {
553
+ return this.parse({ schema, parent: current, name, current: it, siblings }, options)
554
+ })
555
+ .filter(Boolean),
556
+ )
557
+ }
558
+
559
+ return zodKeywordMapper.enum(
560
+ current.args.items.map((schema) => {
561
+ if (schema.format === 'boolean') {
562
+ return transformers.stringify(schema.value)
563
+ }
564
+
565
+ if (schema.format === 'number') {
566
+ return transformers.stringify(schema.value)
567
+ }
558
568
  return transformers.stringify(schema.value)
559
- }
560
- return transformers.stringify(schema.value)
561
- }),
562
- )
563
- }
569
+ }),
570
+ )
571
+ },
572
+ ref(tree, options) {
573
+ const { current } = tree
574
+ if (!isKeyword(current, schemaKeywords.ref)) return undefined
575
+
576
+ // Skip z.lazy wrapper if skipLazyForRefs is true (e.g., inside v4 getters)
577
+ if (options.skipLazyForRefs) {
578
+ return current.args?.name
579
+ }
580
+ return zodKeywordMapper.ref(current.args?.name)
581
+ },
582
+ object(tree, options) {
583
+ const { current, schema, name } = tree
584
+ if (!isKeyword(current, schemaKeywords.object)) return undefined
585
+
586
+ const propertyEntries = Object.entries(current.args?.properties || {}).filter((item) => {
587
+ const schema = item[1]
588
+ return schema && typeof schema.map === 'function'
589
+ })
564
590
 
565
- if (isKeyword(current, schemaKeywords.ref)) {
566
- // Skip z.lazy wrapper if skipLazyForRefs is true (e.g., inside v4 getters)
567
- if (options.skipLazyForRefs) {
568
- return current.args?.name
569
- }
570
- return zodKeywordMapper.ref(current.args?.name)
571
- }
591
+ const properties = propertyEntries
592
+ .map(([propertyName, schemas]) => {
593
+ const nameSchema = schemas.find((it) => it.keyword === schemaKeywords.name) as SchemaKeywordMapper['name']
594
+ const isNullable = schemas.some((it) => isKeyword(it, schemaKeywords.nullable))
595
+ const isNullish = schemas.some((it) => isKeyword(it, schemaKeywords.nullish))
596
+ const isOptional = schemas.some((it) => isKeyword(it, schemaKeywords.optional))
597
+ const hasRef = !!SchemaGenerator.find(schemas, schemaKeywords.ref)
572
598
 
573
- if (isKeyword(current, schemaKeywords.object)) {
574
- const propertyEntries = Object.entries(current.args?.properties || {}).filter((item) => {
575
- const schema = item[1]
576
- return schema && typeof schema.map === 'function'
577
- })
578
-
579
- const properties = propertyEntries
580
- .map(([propertyName, schemas]) => {
581
- const nameSchema = schemas.find((it) => it.keyword === schemaKeywords.name) as SchemaKeywordMapper['name']
582
- const isNullable = schemas.some((it) => isKeyword(it, schemaKeywords.nullable))
583
- const isNullish = schemas.some((it) => isKeyword(it, schemaKeywords.nullish))
584
- const isOptional = schemas.some((it) => isKeyword(it, schemaKeywords.optional))
585
- const hasRef = !!SchemaGenerator.find(schemas, schemaKeywords.ref)
586
-
587
- const mappedName = nameSchema?.args || propertyName
588
-
589
- // custom mapper(pluginOptions)
590
- if (options.mapper?.[mappedName]) {
591
- return `"${propertyName}": ${options.mapper?.[mappedName]}`
592
- }
599
+ const mappedName = nameSchema?.args || propertyName
593
600
 
594
- const baseSchemaOutput = sort(schemas)
595
- .filter((schema) => {
596
- return !isKeyword(schema, schemaKeywords.optional) && !isKeyword(schema, schemaKeywords.nullable) && !isKeyword(schema, schemaKeywords.nullish)
597
- })
598
- .map((it) => {
599
- // For v4 with refs, skip z.lazy wrapper since the getter provides lazy evaluation
600
- const skipLazyForRefs = options.version === '4' && hasRef
601
- return parse({ schema, parent: current, name, current: it, siblings: schemas }, { ...options, skipLazyForRefs })
602
- })
603
- .filter(Boolean)
604
- .join('')
601
+ // custom mapper(pluginOptions)
602
+ if (options.mapper?.[mappedName]) {
603
+ return `"${propertyName}": ${options.mapper?.[mappedName]}`
604
+ }
605
+
606
+ const baseSchemaOutput = sort(schemas)
607
+ .filter((schema) => {
608
+ return !isKeyword(schema, schemaKeywords.optional) && !isKeyword(schema, schemaKeywords.nullable) && !isKeyword(schema, schemaKeywords.nullish)
609
+ })
610
+ .map((it) => {
611
+ // For v4 with refs, skip z.lazy wrapper since the getter provides lazy evaluation
612
+ const skipLazyForRefs = options.version === '4' && hasRef
613
+ return this.parse({ schema, parent: current, name, current: it, siblings: schemas }, { ...options, skipLazyForRefs })
614
+ })
615
+ .filter(Boolean)
616
+ .join('')
617
+
618
+ const objectValue = options.wrapOutput
619
+ ? options.wrapOutput({ output: baseSchemaOutput, schema: schema?.properties?.[propertyName] }) || baseSchemaOutput
620
+ : baseSchemaOutput
621
+
622
+ if (options.version === '4' && hasRef) {
623
+ // In mini mode, use functional wrappers instead of chainable methods
624
+ if (options.mini) {
625
+ // both optional and nullable
626
+ if (isNullish) {
627
+ return `get "${propertyName}"(){
628
+ return ${zodKeywordMapper.nullish(objectValue)}
629
+ }`
630
+ }
631
+
632
+ // undefined
633
+ if (isOptional) {
634
+ return `get "${propertyName}"(){
635
+ return ${zodKeywordMapper.optional(objectValue)}
636
+ }`
637
+ }
638
+
639
+ // null
640
+ if (isNullable) {
641
+ return `get "${propertyName}"(){
642
+ return ${zodKeywordMapper.nullable(objectValue)}
643
+ }`
644
+ }
605
645
 
606
- const objectValue = options.wrapOutput
607
- ? options.wrapOutput({ output: baseSchemaOutput, schema: schema?.properties?.[propertyName] }) || baseSchemaOutput
608
- : baseSchemaOutput
646
+ return `get "${propertyName}"(){
647
+ return ${objectValue}
648
+ }`
649
+ }
609
650
 
610
- if (options.version === '4' && hasRef) {
611
- // In mini mode, use functional wrappers instead of chainable methods
612
- if (options.mini) {
651
+ // Non-mini mode uses chainable methods
613
652
  // both optional and nullable
614
653
  if (isNullish) {
615
654
  return `get "${propertyName}"(){
616
- return ${zodKeywordMapper.nullish(objectValue)}
655
+ return ${objectValue}${zodKeywordMapper.nullish()}
617
656
  }`
618
657
  }
619
658
 
620
659
  // undefined
621
660
  if (isOptional) {
622
661
  return `get "${propertyName}"(){
623
- return ${zodKeywordMapper.optional(objectValue)}
662
+ return ${objectValue}${zodKeywordMapper.optional()}
624
663
  }`
625
664
  }
626
665
 
627
666
  // null
628
667
  if (isNullable) {
629
668
  return `get "${propertyName}"(){
630
- return ${zodKeywordMapper.nullable(objectValue)}
669
+ return ${objectValue}${zodKeywordMapper.nullable()}
631
670
  }`
632
671
  }
633
672
 
@@ -636,188 +675,188 @@ export function parse({ schema, parent, current, name, siblings }: SchemaTree, o
636
675
  }`
637
676
  }
638
677
 
639
- // Non-mini mode uses chainable methods
640
678
  // both optional and nullable
641
679
  if (isNullish) {
642
- return `get "${propertyName}"(){
643
- return ${objectValue}${zodKeywordMapper.nullish()}
644
- }`
680
+ return `"${propertyName}": ${objectValue}${zodKeywordMapper.nullish()}`
645
681
  }
646
682
 
647
683
  // undefined
648
684
  if (isOptional) {
649
- return `get "${propertyName}"(){
650
- return ${objectValue}${zodKeywordMapper.optional()}
651
- }`
685
+ return `"${propertyName}": ${zodKeywordMapper.optional(objectValue)}`
652
686
  }
653
687
 
654
688
  // null
655
689
  if (isNullable) {
656
- return `get "${propertyName}"(){
657
- return ${objectValue}${zodKeywordMapper.nullable()}
658
- }`
690
+ return `"${propertyName}": ${zodKeywordMapper.nullable(objectValue)}`
659
691
  }
660
692
 
661
- return `get "${propertyName}"(){
662
- return ${objectValue}
663
- }`
664
- }
665
-
666
- // both optional and nullable
667
- if (isNullish) {
668
- return `"${propertyName}": ${objectValue}${zodKeywordMapper.nullish()}`
669
- }
670
-
671
- // undefined
672
- if (isOptional) {
673
- return `"${propertyName}": ${zodKeywordMapper.optional(objectValue)}`
674
- }
675
-
676
- // null
677
- if (isNullable) {
678
- return `"${propertyName}": ${zodKeywordMapper.nullable(objectValue)}`
679
- }
680
-
681
- return `"${propertyName}": ${objectValue}`
682
- })
683
- .join(',\n')
684
-
685
- const additionalProperties = current.args?.additionalProperties?.length
686
- ? current.args.additionalProperties
687
- .map((it, _index, siblings) => parse({ schema, parent: current, name, current: it, siblings }, options))
688
- .filter(Boolean)
689
- .join('')
690
- : undefined
691
-
692
- const text = [
693
- zodKeywordMapper.object(properties, current.args?.strict, options.version),
694
- additionalProperties ? zodKeywordMapper.catchall(additionalProperties, options.mini) : undefined,
695
- ].filter(Boolean)
696
-
697
- return text.join('')
698
- }
699
-
700
- if (isKeyword(current, schemaKeywords.tuple)) {
701
- return zodKeywordMapper.tuple(
702
- current.args.items.map((it, _index, siblings) => parse({ schema, parent: current, name, current: it, siblings }, options)).filter(Boolean),
703
- )
704
- }
705
-
706
- if (isKeyword(current, schemaKeywords.const)) {
707
- if (current.args.format === 'number' && current.args.value !== undefined) {
708
- return zodKeywordMapper.const(Number(current.args.value))
709
- }
693
+ return `"${propertyName}": ${objectValue}`
694
+ })
695
+ .join(',\n')
696
+
697
+ const additionalProperties = current.args?.additionalProperties?.length
698
+ ? current.args.additionalProperties
699
+ .map((it, _index, siblings) => this.parse({ schema, parent: current, name, current: it, siblings }, options))
700
+ .filter(Boolean)
701
+ .join('')
702
+ : undefined
703
+
704
+ const text = [
705
+ zodKeywordMapper.object(properties, current.args?.strict, options.version),
706
+ additionalProperties ? zodKeywordMapper.catchall(additionalProperties, options.mini) : undefined,
707
+ ].filter(Boolean)
708
+
709
+ return text.join('')
710
+ },
711
+ tuple(tree, options) {
712
+ const { current, schema, name } = tree
713
+ if (!isKeyword(current, schemaKeywords.tuple)) return undefined
714
+
715
+ return zodKeywordMapper.tuple(
716
+ current.args.items.map((it, _index, siblings) => this.parse({ schema, parent: current, name, current: it, siblings }, options)).filter(Boolean),
717
+ )
718
+ },
719
+ const(tree, _options) {
720
+ const { current } = tree
721
+ if (!isKeyword(current, schemaKeywords.const)) return undefined
710
722
 
711
- if (current.args.format === 'boolean' && current.args.value !== undefined) {
712
- return zodKeywordMapper.const(current.args.value)
713
- }
714
- return zodKeywordMapper.const(transformers.stringify(current.args.value))
715
- }
723
+ if (current.args.format === 'number' && current.args.value !== undefined) {
724
+ return zodKeywordMapper.const(Number(current.args.value))
725
+ }
716
726
 
717
- if (isKeyword(current, schemaKeywords.matches)) {
718
- if (current.args) {
719
- return zodKeywordMapper.matches(transformers.toRegExpString(current.args, null), shouldCoerce(options.coercion, 'strings'), options.mini)
720
- }
721
- }
727
+ if (current.args.format === 'boolean' && current.args.value !== undefined) {
728
+ return zodKeywordMapper.const(typeof current.args.value === 'boolean' ? current.args.value : undefined)
729
+ }
730
+ return zodKeywordMapper.const(transformers.stringify(current.args.value))
731
+ },
732
+ matches(tree, options) {
733
+ const { current, siblings } = tree
734
+ if (!isKeyword(current, schemaKeywords.matches)) return undefined
735
+
736
+ // Early exit: if siblings contain both matches and ref → skip matches entirely
737
+ const hasRef = siblings.some((it) => isKeyword(it, schemaKeywords.ref))
738
+ if (hasRef) {
739
+ return undefined // strip matches
740
+ }
722
741
 
723
- if (isKeyword(current, schemaKeywords.default)) {
724
- // In mini mode, default is handled by wrapWithMiniModifiers
725
- if (options.mini) {
742
+ if (current.args) {
743
+ return zodKeywordMapper.matches(transformers.toRegExpString(current.args, null), shouldCoerce(options.coercion, 'strings'), options.mini)
744
+ }
726
745
  return undefined
727
- }
728
- if (current.args) {
729
- return zodKeywordMapper.default(current.args)
730
- }
731
- }
732
-
733
- if (isKeyword(current, schemaKeywords.describe)) {
734
- if (current.args) {
735
- return zodKeywordMapper.describe(transformers.stringify(current.args.toString()), undefined, options.mini)
736
- }
737
- }
738
-
739
- if (isKeyword(current, schemaKeywords.string)) {
740
- const minSchema = SchemaGenerator.find(siblings, schemaKeywords.min)
741
- const maxSchema = SchemaGenerator.find(siblings, schemaKeywords.max)
742
-
743
- return zodKeywordMapper.string(shouldCoerce(options.coercion, 'strings'), minSchema?.args, maxSchema?.args, options.mini)
744
- }
745
-
746
- if (isKeyword(current, schemaKeywords.uuid)) {
747
- const minSchema = SchemaGenerator.find(siblings, schemaKeywords.min)
748
- const maxSchema = SchemaGenerator.find(siblings, schemaKeywords.max)
749
-
750
- return zodKeywordMapper.uuid(shouldCoerce(options.coercion, 'strings'), options.version, minSchema?.args, maxSchema?.args, options.mini)
751
- }
752
-
753
- if (isKeyword(current, schemaKeywords.email)) {
754
- const minSchema = SchemaGenerator.find(siblings, schemaKeywords.min)
755
- const maxSchema = SchemaGenerator.find(siblings, schemaKeywords.max)
756
-
757
- return zodKeywordMapper.email(shouldCoerce(options.coercion, 'strings'), options.version, minSchema?.args, maxSchema?.args, options.mini)
758
- }
759
-
760
- if (isKeyword(current, schemaKeywords.url)) {
761
- const minSchema = SchemaGenerator.find(siblings, schemaKeywords.min)
762
- const maxSchema = SchemaGenerator.find(siblings, schemaKeywords.max)
763
-
764
- return zodKeywordMapper.url(shouldCoerce(options.coercion, 'strings'), options.version, minSchema?.args, maxSchema?.args, options.mini)
765
- }
766
-
767
- if (isKeyword(current, schemaKeywords.number)) {
768
- const minSchema = SchemaGenerator.find(siblings, schemaKeywords.min)
769
- const maxSchema = SchemaGenerator.find(siblings, schemaKeywords.max)
770
-
771
- const exclusiveMinimumSchema = SchemaGenerator.find(siblings, schemaKeywords.exclusiveMinimum)
772
- const exclusiveMaximumSchema = SchemaGenerator.find(siblings, schemaKeywords.exclusiveMaximum)
773
- return zodKeywordMapper.number(
774
- shouldCoerce(options.coercion, 'numbers'),
775
- minSchema?.args,
776
- maxSchema?.args,
777
- exclusiveMinimumSchema?.args,
778
- exclusiveMaximumSchema?.args,
779
- options.mini,
780
- )
781
- }
782
-
783
- if (isKeyword(current, schemaKeywords.integer)) {
784
- const minSchema = SchemaGenerator.find(siblings, schemaKeywords.min)
785
- const maxSchema = SchemaGenerator.find(siblings, schemaKeywords.max)
786
-
787
- const exclusiveMinimumSchema = SchemaGenerator.find(siblings, schemaKeywords.exclusiveMinimum)
788
- const exclusiveMaximumSchema = SchemaGenerator.find(siblings, schemaKeywords.exclusiveMaximum)
789
- return zodKeywordMapper.integer(
790
- shouldCoerce(options.coercion, 'numbers'),
791
- minSchema?.args,
792
- maxSchema?.args,
793
- options.version,
794
- exclusiveMinimumSchema?.args,
795
- exclusiveMaximumSchema?.args,
796
- options.mini,
797
- )
798
- }
799
-
800
- if (isKeyword(current, schemaKeywords.datetime)) {
801
- return zodKeywordMapper.datetime(current.args.offset, current.args.local, options.version, options.mini)
802
- }
803
-
804
- if (isKeyword(current, schemaKeywords.date)) {
805
- return zodKeywordMapper.date(current.args.type, shouldCoerce(options.coercion, 'dates'), options.version)
806
- }
807
-
808
- if (isKeyword(current, schemaKeywords.time)) {
809
- return zodKeywordMapper.time(current.args.type, shouldCoerce(options.coercion, 'dates'), options.version)
810
- }
811
-
812
- if (current.keyword in zodKeywordMapper && 'args' in current) {
813
- const value = zodKeywordMapper[current.keyword as keyof typeof zodKeywordMapper] as (typeof zodKeywordMapper)['const']
814
-
815
- return value((current as SchemaKeywordBase<unknown>).args as any)
816
- }
817
-
818
- if (current.keyword in zodKeywordMapper) {
819
- return value()
820
- }
821
-
822
- return undefined
823
- }
746
+ },
747
+ default(tree, options) {
748
+ const { current } = tree
749
+ if (!isKeyword(current, schemaKeywords.default)) return undefined
750
+
751
+ // In mini mode, default is handled by wrapWithMiniModifiers
752
+ if (options.mini) {
753
+ return undefined
754
+ }
755
+ if (current.args !== undefined) {
756
+ return zodKeywordMapper.default(current.args)
757
+ }
758
+ // When args is undefined, call the mapper without arguments
759
+ return zodKeywordMapper.default()
760
+ },
761
+ describe(tree, options) {
762
+ const { current } = tree
763
+ if (!isKeyword(current, schemaKeywords.describe)) return undefined
764
+
765
+ if (current.args) {
766
+ return zodKeywordMapper.describe(transformers.stringify(current.args.toString()), undefined, options.mini)
767
+ }
768
+ return undefined
769
+ },
770
+ string(tree, options) {
771
+ const { current, siblings } = tree
772
+ if (!isKeyword(current, schemaKeywords.string)) return undefined
773
+
774
+ const minSchema = findSchemaKeyword(siblings, 'min')
775
+ const maxSchema = findSchemaKeyword(siblings, 'max')
776
+
777
+ return zodKeywordMapper.string(shouldCoerce(options.coercion, 'strings'), minSchema?.args, maxSchema?.args, options.mini)
778
+ },
779
+ uuid(tree, options) {
780
+ const { current, siblings } = tree
781
+ if (!isKeyword(current, schemaKeywords.uuid)) return undefined
782
+
783
+ const minSchema = findSchemaKeyword(siblings, 'min')
784
+ const maxSchema = findSchemaKeyword(siblings, 'max')
785
+
786
+ return zodKeywordMapper.uuid(shouldCoerce(options.coercion, 'strings'), options.version, minSchema?.args, maxSchema?.args, options.mini)
787
+ },
788
+ email(tree, options) {
789
+ const { current, siblings } = tree
790
+ if (!isKeyword(current, schemaKeywords.email)) return undefined
791
+
792
+ const minSchema = findSchemaKeyword(siblings, 'min')
793
+ const maxSchema = findSchemaKeyword(siblings, 'max')
794
+
795
+ return zodKeywordMapper.email(shouldCoerce(options.coercion, 'strings'), options.version, minSchema?.args, maxSchema?.args, options.mini)
796
+ },
797
+ url(tree, options) {
798
+ const { current, siblings } = tree
799
+ if (!isKeyword(current, schemaKeywords.url)) return undefined
800
+
801
+ const minSchema = findSchemaKeyword(siblings, 'min')
802
+ const maxSchema = findSchemaKeyword(siblings, 'max')
803
+
804
+ return zodKeywordMapper.url(shouldCoerce(options.coercion, 'strings'), options.version, minSchema?.args, maxSchema?.args, options.mini)
805
+ },
806
+ number(tree, options) {
807
+ const { current, siblings } = tree
808
+ if (!isKeyword(current, schemaKeywords.number)) return undefined
809
+
810
+ const minSchema = findSchemaKeyword(siblings, 'min')
811
+ const maxSchema = findSchemaKeyword(siblings, 'max')
812
+ const exclusiveMinimumSchema = findSchemaKeyword(siblings, 'exclusiveMinimum')
813
+ const exclusiveMaximumSchema = findSchemaKeyword(siblings, 'exclusiveMaximum')
814
+
815
+ return zodKeywordMapper.number(
816
+ shouldCoerce(options.coercion, 'numbers'),
817
+ minSchema?.args,
818
+ maxSchema?.args,
819
+ exclusiveMinimumSchema?.args,
820
+ exclusiveMaximumSchema?.args,
821
+ options.mini,
822
+ )
823
+ },
824
+ integer(tree, options) {
825
+ const { current, siblings } = tree
826
+ if (!isKeyword(current, schemaKeywords.integer)) return undefined
827
+
828
+ const minSchema = findSchemaKeyword(siblings, 'min')
829
+ const maxSchema = findSchemaKeyword(siblings, 'max')
830
+ const exclusiveMinimumSchema = findSchemaKeyword(siblings, 'exclusiveMinimum')
831
+ const exclusiveMaximumSchema = findSchemaKeyword(siblings, 'exclusiveMaximum')
832
+
833
+ return zodKeywordMapper.integer(
834
+ shouldCoerce(options.coercion, 'numbers'),
835
+ minSchema?.args,
836
+ maxSchema?.args,
837
+ options.version,
838
+ exclusiveMinimumSchema?.args,
839
+ exclusiveMaximumSchema?.args,
840
+ options.mini,
841
+ )
842
+ },
843
+ datetime(tree, options) {
844
+ const { current } = tree
845
+ if (!isKeyword(current, schemaKeywords.datetime)) return undefined
846
+
847
+ return zodKeywordMapper.datetime(current.args.offset, current.args.local, options.version, options.mini)
848
+ },
849
+ date(tree, options) {
850
+ const { current } = tree
851
+ if (!isKeyword(current, schemaKeywords.date)) return undefined
852
+
853
+ return zodKeywordMapper.date(current.args.type, shouldCoerce(options.coercion, 'dates'), options.version)
854
+ },
855
+ time(tree, options) {
856
+ const { current } = tree
857
+ if (!isKeyword(current, schemaKeywords.time)) return undefined
858
+
859
+ return zodKeywordMapper.time(current.args.type, shouldCoerce(options.coercion, 'dates'), options.version)
860
+ },
861
+ },
862
+ })