@kubb/plugin-zod 4.10.1 → 4.11.0
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/dist/{components-BCKi8Hou.js → components-B9udp6Zi.js} +247 -200
- package/dist/components-B9udp6Zi.js.map +1 -0
- package/dist/{components-CwZBp5_M.cjs → components-DaK9uYjS.cjs} +246 -199
- package/dist/components-DaK9uYjS.cjs.map +1 -0
- package/dist/components.cjs +1 -1
- package/dist/components.js +1 -1
- package/dist/{generators-CCF0NvKr.cjs → generators-CDPXFoZT.cjs} +2 -2
- package/dist/{generators-CCF0NvKr.cjs.map → generators-CDPXFoZT.cjs.map} +1 -1
- package/dist/{generators-uL_Da-pi.js → generators-DKQPsBns.js} +2 -2
- package/dist/{generators-uL_Da-pi.js.map → generators-DKQPsBns.js.map} +1 -1
- package/dist/generators.cjs +1 -1
- package/dist/generators.js +1 -1
- package/dist/index.cjs +2 -2
- package/dist/index.js +1 -1
- package/package.json +6 -6
- package/src/parser.ts +339 -300
- package/dist/components-BCKi8Hou.js.map +0 -1
- package/dist/components-CwZBp5_M.cjs.map +0 -1
package/src/parser.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import transformers from '@kubb/core/transformers'
|
|
2
2
|
|
|
3
|
-
import type { Schema,
|
|
4
|
-
import { isKeyword, SchemaGenerator, type SchemaKeywordMapper,
|
|
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 |
|
|
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
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
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
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
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
|
-
.
|
|
521
|
-
|
|
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
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
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.
|
|
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
|
-
|
|
552
|
-
current.
|
|
553
|
-
|
|
554
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
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
|
-
|
|
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
|
-
|
|
595
|
-
.
|
|
596
|
-
return
|
|
597
|
-
}
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
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
|
-
|
|
607
|
-
|
|
608
|
-
|
|
646
|
+
return `get "${propertyName}"(){
|
|
647
|
+
return ${objectValue}
|
|
648
|
+
}`
|
|
649
|
+
}
|
|
609
650
|
|
|
610
|
-
|
|
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(
|
|
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(
|
|
662
|
+
return ${objectValue}${zodKeywordMapper.optional()}
|
|
624
663
|
}`
|
|
625
664
|
}
|
|
626
665
|
|
|
627
666
|
// null
|
|
628
667
|
if (isNullable) {
|
|
629
668
|
return `get "${propertyName}"(){
|
|
630
|
-
|
|
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 `
|
|
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 `
|
|
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 `
|
|
657
|
-
return ${objectValue}${zodKeywordMapper.nullable()}
|
|
658
|
-
}`
|
|
690
|
+
return `"${propertyName}": ${zodKeywordMapper.nullable(objectValue)}`
|
|
659
691
|
}
|
|
660
692
|
|
|
661
|
-
return `
|
|
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
|
-
: 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
|
-
|
|
712
|
-
|
|
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
|
-
|
|
718
|
-
|
|
719
|
-
|
|
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
|
-
|
|
724
|
-
|
|
725
|
-
|
|
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
|
-
|
|
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
|
-
maxSchema?.args,
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
minSchema
|
|
792
|
-
maxSchema
|
|
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
|
-
}
|
|
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
|
+
})
|