@kubb/plugin-zod 0.0.0-canary-20251103140549 → 0.0.0-canary-20251103161135
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-DlFtXaJq.js → components-BtBB23cQ.js} +81 -71
- package/dist/components-BtBB23cQ.js.map +1 -0
- package/dist/{components-BShpQYfg.cjs → components-DIUzzW4k.cjs} +81 -71
- package/dist/components-DIUzzW4k.cjs.map +1 -0
- package/dist/components.cjs +1 -1
- package/dist/components.d.cts +2 -2
- package/dist/components.d.ts +2 -2
- package/dist/components.js +1 -1
- package/dist/{generators-BtR5ImYH.js → generators-9upbJt-4.js} +4 -4
- package/dist/generators-9upbJt-4.js.map +1 -0
- package/dist/{generators-CTwKUJk6.cjs → generators-T3e_E7Uy.cjs} +4 -4
- package/dist/generators-T3e_E7Uy.cjs.map +1 -0
- package/dist/generators.cjs +2 -2
- package/dist/generators.js +2 -2
- package/dist/index.cjs +2 -2
- package/dist/index.js +2 -2
- package/package.json +6 -6
- package/src/components/Zod.tsx +6 -12
- package/src/generators/__snapshots__/createPet.ts +2 -2
- package/src/generators/__snapshots__/createPetWithUnknownTypeUnknown.ts +2 -2
- package/src/generators/__snapshots__/createPetWithUnknownTypeUnknown_wrapOutput.ts +7 -9
- package/src/generators/__snapshots__/createPetWithUnknownTypeUnknown_wrapOutput_entire_.ts +7 -10
- package/src/generators/__snapshots__/createPet_wrapOutput.ts +7 -9
- package/src/generators/__snapshots__/discriminator.ts +20 -28
- package/src/generators/__snapshots__/getPets.ts +3 -3
- package/src/generators/__snapshots__/getPets_wrapOutput.ts +8 -10
- package/src/generators/__snapshots__/petArray.ts +1 -5
- package/src/generators/__snapshots__/queryAllDefaulted.ts +1 -1
- package/src/generators/__snapshots__/queryAllDefaulted_wrapOutput.ts +2 -2
- package/src/generators/__snapshots__/showPetById.ts +3 -3
- package/src/generators/__snapshots__/showPetById_wrapOutput.ts +8 -10
- package/src/generators/zodGenerator.tsx +2 -2
- package/src/parser.ts +37 -36
- package/dist/components-BShpQYfg.cjs.map +0 -1
- package/dist/components-DlFtXaJq.js.map +0 -1
- package/dist/generators-BtR5ImYH.js.map +0 -1
- package/dist/generators-CTwKUJk6.cjs.map +0 -1
package/src/components/Zod.tsx
CHANGED
|
@@ -11,7 +11,7 @@ type Props = {
|
|
|
11
11
|
typeName?: string
|
|
12
12
|
inferTypeName?: string
|
|
13
13
|
tree: Array<Schema>
|
|
14
|
-
|
|
14
|
+
schema: SchemaObject
|
|
15
15
|
description?: string
|
|
16
16
|
coercion: PluginZod['resolvedOptions']['coercion']
|
|
17
17
|
mapper: PluginZod['resolvedOptions']['mapper']
|
|
@@ -25,7 +25,7 @@ export function Zod({
|
|
|
25
25
|
name,
|
|
26
26
|
typeName,
|
|
27
27
|
tree,
|
|
28
|
-
|
|
28
|
+
schema,
|
|
29
29
|
inferTypeName,
|
|
30
30
|
mapper,
|
|
31
31
|
coercion,
|
|
@@ -49,10 +49,7 @@ export function Zod({
|
|
|
49
49
|
.map((schema, index) => {
|
|
50
50
|
const siblings = schemas.filter((_, i) => i !== index)
|
|
51
51
|
|
|
52
|
-
return parserZod.parse(
|
|
53
|
-
{ parent: undefined, current: schema, siblings },
|
|
54
|
-
{ name, keysToOmit, typeName, description, mapper, coercion, wrapOutput, rawSchema, version },
|
|
55
|
-
)
|
|
52
|
+
return parserZod.parse({ schema, parent: undefined, current: schema, siblings, name }, { mapper, coercion, wrapOutput, version })
|
|
56
53
|
})
|
|
57
54
|
.filter(Boolean)
|
|
58
55
|
.join('')
|
|
@@ -71,28 +68,25 @@ export function Zod({
|
|
|
71
68
|
} else {
|
|
72
69
|
suffix = '.unwrap()'
|
|
73
70
|
}
|
|
74
|
-
} else {
|
|
75
|
-
if (firstSchema && isKeyword(firstSchema, schemaKeywords.ref) && version === '3') {
|
|
76
|
-
suffix = '.schema'
|
|
77
|
-
}
|
|
78
71
|
}
|
|
79
72
|
|
|
80
73
|
const emptyValue = parserZod.parse(
|
|
81
74
|
{
|
|
75
|
+
schema,
|
|
82
76
|
parent: undefined,
|
|
83
77
|
current: {
|
|
84
78
|
keyword: schemaKeywords[emptySchemaType],
|
|
85
79
|
},
|
|
86
80
|
siblings: [],
|
|
87
81
|
},
|
|
88
|
-
{
|
|
82
|
+
{ mapper, coercion, wrapOutput, version },
|
|
89
83
|
)
|
|
90
84
|
|
|
91
85
|
const baseSchemaOutput =
|
|
92
86
|
[output, keysToOmit?.length ? `${suffix}.omit({ ${keysToOmit.map((key) => `'${key}': true`).join(',')} })` : undefined].filter(Boolean).join('') ||
|
|
93
87
|
emptyValue ||
|
|
94
88
|
''
|
|
95
|
-
const wrappedSchemaOutput = wrapOutput ? wrapOutput({ output: baseSchemaOutput, schema
|
|
89
|
+
const wrappedSchemaOutput = wrapOutput ? wrapOutput({ output: baseSchemaOutput, schema }) || baseSchemaOutput : baseSchemaOutput
|
|
96
90
|
const finalOutput = typeName ? `${wrappedSchemaOutput} as unknown as ${version === '4' ? 'z.ZodType' : 'ToZod'}<${typeName}>` : wrappedSchemaOutput
|
|
97
91
|
|
|
98
92
|
return (
|
|
@@ -12,11 +12,11 @@ export const createPets201 = z.unknown()
|
|
|
12
12
|
/**
|
|
13
13
|
* @description unexpected error
|
|
14
14
|
*/
|
|
15
|
-
export const createPetsError =
|
|
15
|
+
export const createPetsError = error
|
|
16
16
|
|
|
17
17
|
export const createPetsMutationRequest = z.object({
|
|
18
18
|
name: z.string(),
|
|
19
19
|
tag: z.string(),
|
|
20
20
|
})
|
|
21
21
|
|
|
22
|
-
export const createPetsMutationResponse =
|
|
22
|
+
export const createPetsMutationResponse = createPets201
|
|
@@ -12,11 +12,11 @@ export const createPets201 = z.unknown()
|
|
|
12
12
|
/**
|
|
13
13
|
* @description unexpected error
|
|
14
14
|
*/
|
|
15
|
-
export const createPetsError =
|
|
15
|
+
export const createPetsError = error
|
|
16
16
|
|
|
17
17
|
export const createPetsMutationRequest = z.object({
|
|
18
18
|
name: z.string(),
|
|
19
19
|
tag: z.string(),
|
|
20
20
|
})
|
|
21
21
|
|
|
22
|
-
export const createPetsMutationResponse =
|
|
22
|
+
export const createPetsMutationResponse = createPets201
|
|
@@ -12,18 +12,16 @@ export const createPets201 = z.unknown()
|
|
|
12
12
|
/**
|
|
13
13
|
* @description unexpected error
|
|
14
14
|
*/
|
|
15
|
-
export const createPetsError =
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
],
|
|
22
|
-
})
|
|
15
|
+
export const createPetsError = error.openapi({
|
|
16
|
+
examples: [
|
|
17
|
+
{ sample: { summary: 'A sample error', value: { code: 1, message: 'A sample error message' } } },
|
|
18
|
+
{ other_example: { summary: 'Another sample error', value: { code: 2, message: 'A totally specific message' } } },
|
|
19
|
+
],
|
|
20
|
+
})
|
|
23
21
|
|
|
24
22
|
export const createPetsMutationRequest = z.object({
|
|
25
23
|
name: z.string().openapi({ example: 'Baxter' }),
|
|
26
24
|
tag: z.string(),
|
|
27
25
|
})
|
|
28
26
|
|
|
29
|
-
export const createPetsMutationResponse =
|
|
27
|
+
export const createPetsMutationResponse = createPets201
|
|
@@ -12,19 +12,16 @@ export const createPets201 = z.unknown()
|
|
|
12
12
|
/**
|
|
13
13
|
* @description unexpected error
|
|
14
14
|
*/
|
|
15
|
-
export const createPetsError = extendApi(
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
],
|
|
22
|
-
},
|
|
23
|
-
)
|
|
15
|
+
export const createPetsError = extendApi(error, {
|
|
16
|
+
examples: [
|
|
17
|
+
{ sample: { summary: 'A sample error', value: { code: 1, message: 'A sample error message' } } },
|
|
18
|
+
{ other_example: { summary: 'Another sample error', value: { code: 2, message: 'A totally specific message' } } },
|
|
19
|
+
],
|
|
20
|
+
})
|
|
24
21
|
|
|
25
22
|
export const createPetsMutationRequest = z.object({
|
|
26
23
|
name: extendApi(z.string(), { example: 'Baxter' }),
|
|
27
24
|
tag: z.string(),
|
|
28
25
|
})
|
|
29
26
|
|
|
30
|
-
export const createPetsMutationResponse =
|
|
27
|
+
export const createPetsMutationResponse = createPets201
|
|
@@ -12,18 +12,16 @@ export const createPets201 = z.unknown()
|
|
|
12
12
|
/**
|
|
13
13
|
* @description unexpected error
|
|
14
14
|
*/
|
|
15
|
-
export const createPetsError =
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
],
|
|
22
|
-
})
|
|
15
|
+
export const createPetsError = error.openapi({
|
|
16
|
+
examples: [
|
|
17
|
+
{ sample: { summary: 'A sample error', value: { code: 1, message: 'A sample error message' } } },
|
|
18
|
+
{ other_example: { summary: 'Another sample error', value: { code: 2, message: 'A totally specific message' } } },
|
|
19
|
+
],
|
|
20
|
+
})
|
|
23
21
|
|
|
24
22
|
export const createPetsMutationRequest = z.object({
|
|
25
23
|
name: z.string().openapi({ example: 'Baxter' }),
|
|
26
24
|
tag: z.string(),
|
|
27
25
|
})
|
|
28
26
|
|
|
29
|
-
export const createPetsMutationResponse =
|
|
27
|
+
export const createPetsMutationResponse = createPets201
|
|
@@ -5,32 +5,24 @@
|
|
|
5
5
|
import { z } from 'zod'
|
|
6
6
|
|
|
7
7
|
export const advanced = z.union([
|
|
8
|
-
|
|
9
|
-
.
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
),
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
.
|
|
25
|
-
z.
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
),
|
|
29
|
-
z
|
|
30
|
-
.lazy(() => sliderValueSpecificationDto)
|
|
31
|
-
.and(
|
|
32
|
-
z.object({
|
|
33
|
-
type: z.literal('slider'),
|
|
34
|
-
}),
|
|
35
|
-
),
|
|
8
|
+
enumerationValueSpecificationDto.and(
|
|
9
|
+
z.object({
|
|
10
|
+
type: z.literal('enum'),
|
|
11
|
+
}),
|
|
12
|
+
),
|
|
13
|
+
rangeValueSpecificationDto.and(
|
|
14
|
+
z.object({
|
|
15
|
+
type: z.literal('range'),
|
|
16
|
+
}),
|
|
17
|
+
),
|
|
18
|
+
regexValueSpecificationDto.and(
|
|
19
|
+
z.object({
|
|
20
|
+
type: z.literal('regex'),
|
|
21
|
+
}),
|
|
22
|
+
),
|
|
23
|
+
sliderValueSpecificationDto.and(
|
|
24
|
+
z.object({
|
|
25
|
+
type: z.literal('slider'),
|
|
26
|
+
}),
|
|
27
|
+
),
|
|
36
28
|
])
|
|
@@ -12,11 +12,11 @@ export const listPetsQueryParams = z.object({
|
|
|
12
12
|
/**
|
|
13
13
|
* @description A paged array of pets
|
|
14
14
|
*/
|
|
15
|
-
export const listPets200 =
|
|
15
|
+
export const listPets200 = pets
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
18
|
* @description unexpected error
|
|
19
19
|
*/
|
|
20
|
-
export const listPetsError =
|
|
20
|
+
export const listPetsError = error
|
|
21
21
|
|
|
22
|
-
export const listPetsQueryResponse =
|
|
22
|
+
export const listPetsQueryResponse = listPets200
|
|
@@ -12,18 +12,16 @@ export const listPetsQueryParams = z.object({
|
|
|
12
12
|
/**
|
|
13
13
|
* @description A paged array of pets
|
|
14
14
|
*/
|
|
15
|
-
export const listPets200 =
|
|
15
|
+
export const listPets200 = pets
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
18
|
* @description unexpected error
|
|
19
19
|
*/
|
|
20
|
-
export const listPetsError =
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
],
|
|
27
|
-
})
|
|
20
|
+
export const listPetsError = error.openapi({
|
|
21
|
+
examples: [
|
|
22
|
+
{ sample: { summary: 'A sample error', value: { code: 1, message: 'A sample error message' } } },
|
|
23
|
+
{ other_example: { summary: 'Another sample error', value: { code: 2, message: 'A totally specific message' } } },
|
|
24
|
+
],
|
|
25
|
+
})
|
|
28
26
|
|
|
29
|
-
export const listPetsQueryResponse =
|
|
27
|
+
export const listPetsQueryResponse = listPets200
|
|
@@ -7,8 +7,4 @@ import { z } from 'zod'
|
|
|
7
7
|
/**
|
|
8
8
|
* @description List of Pet object
|
|
9
9
|
*/
|
|
10
|
-
export const petArray = z
|
|
11
|
-
.array(z.lazy(() => pet))
|
|
12
|
-
.min(1)
|
|
13
|
-
.max(3)
|
|
14
|
-
.describe('List of Pet object')
|
|
10
|
+
export const petArray = z.array(pet).min(1).max(3).describe('List of Pet object')
|
|
@@ -13,7 +13,7 @@ export const getThingsQueryParams = z.object({
|
|
|
13
13
|
* @description A list of things
|
|
14
14
|
*/
|
|
15
15
|
export const getThings200 = z.object({
|
|
16
|
-
items: z.array(
|
|
16
|
+
items: z.array(thing),
|
|
17
17
|
})
|
|
18
18
|
|
|
19
|
-
export const getThingsQueryResponse =
|
|
19
|
+
export const getThingsQueryResponse = getThings200
|
|
@@ -12,11 +12,11 @@ export const showPetByIdPathParams = z.object({
|
|
|
12
12
|
/**
|
|
13
13
|
* @description Expected response to a valid request
|
|
14
14
|
*/
|
|
15
|
-
export const showPetById200 =
|
|
15
|
+
export const showPetById200 = pet
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
18
|
* @description unexpected error
|
|
19
19
|
*/
|
|
20
|
-
export const showPetByIdError =
|
|
20
|
+
export const showPetByIdError = error
|
|
21
21
|
|
|
22
|
-
export const showPetByIdQueryResponse =
|
|
22
|
+
export const showPetByIdQueryResponse = showPetById200
|
|
@@ -12,18 +12,16 @@ export const showPetByIdPathParams = z.object({
|
|
|
12
12
|
/**
|
|
13
13
|
* @description Expected response to a valid request
|
|
14
14
|
*/
|
|
15
|
-
export const showPetById200 =
|
|
15
|
+
export const showPetById200 = pet
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
18
|
* @description unexpected error
|
|
19
19
|
*/
|
|
20
|
-
export const showPetByIdError =
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
],
|
|
27
|
-
})
|
|
20
|
+
export const showPetByIdError = error.openapi({
|
|
21
|
+
examples: [
|
|
22
|
+
{ sample: { summary: 'A sample error', value: { code: 1, message: 'A sample error message' } } },
|
|
23
|
+
{ other_example: { summary: 'Another sample error', value: { code: 2, message: 'A totally specific message' } } },
|
|
24
|
+
],
|
|
25
|
+
})
|
|
28
26
|
|
|
29
|
-
export const showPetByIdQueryResponse =
|
|
27
|
+
export const showPetByIdQueryResponse = showPetById200
|
|
@@ -81,7 +81,7 @@ export const zodGenerator = createReactGenerator<PluginZod>({
|
|
|
81
81
|
inferTypeName={inferred ? zod.inferTypeName : undefined}
|
|
82
82
|
description={description}
|
|
83
83
|
tree={tree}
|
|
84
|
-
|
|
84
|
+
schema={schemaObject}
|
|
85
85
|
mapper={mapper}
|
|
86
86
|
coercion={coercion}
|
|
87
87
|
keysToOmit={keysToOmit}
|
|
@@ -150,7 +150,7 @@ export const zodGenerator = createReactGenerator<PluginZod>({
|
|
|
150
150
|
inferTypeName={inferred ? zod.inferTypeName : undefined}
|
|
151
151
|
description={schema.value.description}
|
|
152
152
|
tree={schema.tree}
|
|
153
|
-
|
|
153
|
+
schema={schema.value}
|
|
154
154
|
mapper={mapper}
|
|
155
155
|
coercion={coercion}
|
|
156
156
|
wrapOutput={wrapOutput}
|
package/src/parser.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import transformers from '@kubb/core/transformers'
|
|
2
|
-
import type { SchemaObject } from '@kubb/oas'
|
|
3
2
|
|
|
4
3
|
import type { Schema, SchemaKeywordBase, SchemaMapper } from '@kubb/plugin-oas'
|
|
5
4
|
import { isKeyword, SchemaGenerator, type SchemaKeywordMapper, type SchemaTree, schemaKeywords } from '@kubb/plugin-oas'
|
|
@@ -191,12 +190,12 @@ const zodKeywordMapper = {
|
|
|
191
190
|
phone: undefined,
|
|
192
191
|
readOnly: undefined,
|
|
193
192
|
writeOnly: undefined,
|
|
194
|
-
ref: (value?: string,
|
|
193
|
+
ref: (value?: string, lazy = true) => {
|
|
195
194
|
if (!value) {
|
|
196
195
|
return undefined
|
|
197
196
|
}
|
|
198
197
|
|
|
199
|
-
return
|
|
198
|
+
return lazy ? `z.lazy(() => ${value})` : value
|
|
200
199
|
},
|
|
201
200
|
blob: () => 'z.instanceof(File)',
|
|
202
201
|
deprecated: undefined,
|
|
@@ -258,18 +257,14 @@ const shouldCoerce = (coercion: ParserOptions['coercion'] | undefined, type: 'da
|
|
|
258
257
|
}
|
|
259
258
|
|
|
260
259
|
type ParserOptions = {
|
|
261
|
-
|
|
262
|
-
typeName?: string
|
|
263
|
-
description?: string
|
|
264
|
-
keysToOmit?: string[]
|
|
260
|
+
lazy?: boolean
|
|
265
261
|
mapper?: Record<string, string>
|
|
266
262
|
coercion?: boolean | { dates?: boolean; strings?: boolean; numbers?: boolean }
|
|
267
263
|
wrapOutput?: (opts: { output: string; schema: any }) => string | undefined
|
|
268
|
-
rawSchema: SchemaObject
|
|
269
264
|
version: '3' | '4'
|
|
270
265
|
}
|
|
271
266
|
|
|
272
|
-
export function parse({ parent, current, name, siblings }: SchemaTree, options: ParserOptions): string | undefined {
|
|
267
|
+
export function parse({ schema, parent, current, name, siblings }: SchemaTree, options: ParserOptions): string | undefined {
|
|
273
268
|
const value = zodKeywordMapper[current.keyword as keyof typeof zodKeywordMapper]
|
|
274
269
|
|
|
275
270
|
// Early exit: if siblings contain both matches and ref → skip matches entirely
|
|
@@ -287,7 +282,7 @@ export function parse({ parent, current, name, siblings }: SchemaTree, options:
|
|
|
287
282
|
if (isKeyword(current, schemaKeywords.union)) {
|
|
288
283
|
// zod union type needs at least 2 items
|
|
289
284
|
if (Array.isArray(current.args) && current.args.length === 1) {
|
|
290
|
-
return parse({ parent, name
|
|
285
|
+
return parse({ schema, parent, name, current: current.args[0] as Schema, siblings }, options)
|
|
291
286
|
}
|
|
292
287
|
if (Array.isArray(current.args) && !current.args.length) {
|
|
293
288
|
return ''
|
|
@@ -295,7 +290,7 @@ export function parse({ parent, current, name, siblings }: SchemaTree, options:
|
|
|
295
290
|
|
|
296
291
|
return zodKeywordMapper.union(
|
|
297
292
|
sort(current.args)
|
|
298
|
-
.map((
|
|
293
|
+
.map((it, _index, siblings) => parse({ schema, parent: current, name, current: it, siblings }, options))
|
|
299
294
|
.filter(Boolean),
|
|
300
295
|
)
|
|
301
296
|
}
|
|
@@ -305,7 +300,7 @@ export function parse({ parent, current, name, siblings }: SchemaTree, options:
|
|
|
305
300
|
.filter((schema: Schema) => {
|
|
306
301
|
return ![schemaKeywords.optional, schemaKeywords.describe].includes(schema.keyword as typeof schemaKeywords.describe)
|
|
307
302
|
})
|
|
308
|
-
.map((
|
|
303
|
+
.map((it: Schema, _index, siblings) => parse({ schema, parent: current, name, current: it, siblings }, options))
|
|
309
304
|
.filter(Boolean)
|
|
310
305
|
|
|
311
306
|
return `${items.slice(0, 1)}${zodKeywordMapper.and(items.slice(1))}`
|
|
@@ -314,7 +309,9 @@ export function parse({ parent, current, name, siblings }: SchemaTree, options:
|
|
|
314
309
|
if (isKeyword(current, schemaKeywords.array)) {
|
|
315
310
|
return zodKeywordMapper.array(
|
|
316
311
|
sort(current.args.items)
|
|
317
|
-
.map((
|
|
312
|
+
.map((it, _index, siblings) => {
|
|
313
|
+
return parse({ schema, parent: current, name, current: it, siblings }, options)
|
|
314
|
+
})
|
|
318
315
|
.filter(Boolean),
|
|
319
316
|
current.args.min,
|
|
320
317
|
current.args.max,
|
|
@@ -329,7 +326,7 @@ export function parse({ parent, current, name, siblings }: SchemaTree, options:
|
|
|
329
326
|
keyword: schemaKeywords.const,
|
|
330
327
|
args: current.args.items[0],
|
|
331
328
|
}
|
|
332
|
-
return parse({ parent: current, name
|
|
329
|
+
return parse({ schema, parent: current, name, current: child, siblings: [child] }, options)
|
|
333
330
|
}
|
|
334
331
|
|
|
335
332
|
return zodKeywordMapper.union(
|
|
@@ -338,8 +335,8 @@ export function parse({ parent, current, name, siblings }: SchemaTree, options:
|
|
|
338
335
|
keyword: schemaKeywords.const,
|
|
339
336
|
args: schema,
|
|
340
337
|
}))
|
|
341
|
-
.map((
|
|
342
|
-
return parse({ parent: current, name
|
|
338
|
+
.map((it, _index, siblings) => {
|
|
339
|
+
return parse({ schema, parent: current, name, current: it, siblings }, options)
|
|
343
340
|
})
|
|
344
341
|
.filter(Boolean),
|
|
345
342
|
)
|
|
@@ -360,7 +357,7 @@ export function parse({ parent, current, name, siblings }: SchemaTree, options:
|
|
|
360
357
|
}
|
|
361
358
|
|
|
362
359
|
if (isKeyword(current, schemaKeywords.ref)) {
|
|
363
|
-
return zodKeywordMapper.ref(current.args?.name, options.
|
|
360
|
+
return zodKeywordMapper.ref(current.args?.name, options.lazy ?? name === current.args.name)
|
|
364
361
|
}
|
|
365
362
|
|
|
366
363
|
if (isKeyword(current, schemaKeywords.object)) {
|
|
@@ -370,80 +367,84 @@ export function parse({ parent, current, name, siblings }: SchemaTree, options:
|
|
|
370
367
|
})
|
|
371
368
|
|
|
372
369
|
const properties = propertyEntries
|
|
373
|
-
.map(([
|
|
370
|
+
.map(([propertyName, schemas]) => {
|
|
374
371
|
const nameSchema = schemas.find((it) => it.keyword === schemaKeywords.name) as SchemaKeywordMapper['name']
|
|
375
372
|
const isNullable = schemas.some((it) => isKeyword(it, schemaKeywords.nullable))
|
|
376
373
|
const isNullish = schemas.some((it) => isKeyword(it, schemaKeywords.nullish))
|
|
377
374
|
const isOptional = schemas.some((it) => isKeyword(it, schemaKeywords.optional))
|
|
375
|
+
const hasRef = !!SchemaGenerator.find(schemas, schemaKeywords.ref)
|
|
378
376
|
|
|
379
|
-
const mappedName = nameSchema?.args ||
|
|
377
|
+
const mappedName = nameSchema?.args || propertyName
|
|
380
378
|
|
|
381
379
|
// custom mapper(pluginOptions)
|
|
382
380
|
if (options.mapper?.[mappedName]) {
|
|
383
|
-
return `"${
|
|
381
|
+
return `"${propertyName}": ${options.mapper?.[mappedName]}`
|
|
384
382
|
}
|
|
385
383
|
|
|
386
384
|
const baseSchemaOutput = sort(schemas)
|
|
387
385
|
.filter((schema) => {
|
|
388
386
|
return !isKeyword(schema, schemaKeywords.optional) && !isKeyword(schema, schemaKeywords.nullable) && !isKeyword(schema, schemaKeywords.nullish)
|
|
389
387
|
})
|
|
390
|
-
.map((
|
|
388
|
+
.map((it) => {
|
|
389
|
+
const lazy = !(options.version === '4' && hasRef)
|
|
390
|
+
return parse({ schema, parent: current, name, current: it, siblings: schemas }, { ...options, lazy })
|
|
391
|
+
})
|
|
391
392
|
.filter(Boolean)
|
|
392
393
|
.join('')
|
|
393
394
|
|
|
394
395
|
const objectValue = options.wrapOutput
|
|
395
|
-
? options.wrapOutput({ output: baseSchemaOutput, schema:
|
|
396
|
+
? options.wrapOutput({ output: baseSchemaOutput, schema: schema?.properties?.[propertyName] }) || baseSchemaOutput
|
|
396
397
|
: baseSchemaOutput
|
|
397
398
|
|
|
398
|
-
if (options.version === '4' &&
|
|
399
|
+
if (options.version === '4' && hasRef) {
|
|
399
400
|
// both optional and nullable
|
|
400
401
|
if (isNullish) {
|
|
401
|
-
return `get "${
|
|
402
|
-
return ${zodKeywordMapper.nullish(
|
|
402
|
+
return `get "${propertyName}"(){
|
|
403
|
+
return ${objectValue}${zodKeywordMapper.nullish()}
|
|
403
404
|
}`
|
|
404
405
|
}
|
|
405
406
|
|
|
406
407
|
// undefined
|
|
407
408
|
if (isOptional) {
|
|
408
|
-
return `get "${
|
|
409
|
-
return ${zodKeywordMapper.optional(
|
|
409
|
+
return `get "${propertyName}"(){
|
|
410
|
+
return ${objectValue}${zodKeywordMapper.optional()}
|
|
410
411
|
}`
|
|
411
412
|
}
|
|
412
413
|
|
|
413
414
|
// null
|
|
414
415
|
if (isNullable) {
|
|
415
|
-
return `get "${
|
|
416
|
-
|
|
416
|
+
return `get "${propertyName}"(){
|
|
417
|
+
return ${objectValue}${zodKeywordMapper.nullable()}
|
|
417
418
|
}`
|
|
418
419
|
}
|
|
419
420
|
|
|
420
|
-
return `get "${
|
|
421
|
+
return `get "${propertyName}"(){
|
|
421
422
|
return ${objectValue}
|
|
422
423
|
}`
|
|
423
424
|
}
|
|
424
425
|
|
|
425
426
|
// both optional and nullable
|
|
426
427
|
if (isNullish) {
|
|
427
|
-
return `"${
|
|
428
|
+
return `"${propertyName}": ${objectValue}${zodKeywordMapper.nullish()}`
|
|
428
429
|
}
|
|
429
430
|
|
|
430
431
|
// undefined
|
|
431
432
|
if (isOptional) {
|
|
432
|
-
return `"${
|
|
433
|
+
return `"${propertyName}": ${zodKeywordMapper.optional(objectValue)}`
|
|
433
434
|
}
|
|
434
435
|
|
|
435
436
|
// null
|
|
436
437
|
if (isNullable) {
|
|
437
|
-
return `"${
|
|
438
|
+
return `"${propertyName}": ${zodKeywordMapper.nullable(objectValue)}`
|
|
438
439
|
}
|
|
439
440
|
|
|
440
|
-
return `"${
|
|
441
|
+
return `"${propertyName}": ${objectValue}`
|
|
441
442
|
})
|
|
442
443
|
.join(',\n')
|
|
443
444
|
|
|
444
445
|
const additionalProperties = current.args?.additionalProperties?.length
|
|
445
446
|
? current.args.additionalProperties
|
|
446
|
-
.map((
|
|
447
|
+
.map((it, _index, siblings) => parse({ schema, parent: current, name, current: it, siblings }, options))
|
|
447
448
|
.filter(Boolean)
|
|
448
449
|
.join('')
|
|
449
450
|
: undefined
|
|
@@ -458,7 +459,7 @@ export function parse({ parent, current, name, siblings }: SchemaTree, options:
|
|
|
458
459
|
|
|
459
460
|
if (isKeyword(current, schemaKeywords.tuple)) {
|
|
460
461
|
return zodKeywordMapper.tuple(
|
|
461
|
-
current.args.items.map((
|
|
462
|
+
current.args.items.map((it, _index, siblings) => parse({ schema, parent: current, name, current: it, siblings }, options)).filter(Boolean),
|
|
462
463
|
)
|
|
463
464
|
}
|
|
464
465
|
|