Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/zod/src/converter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ const combinationCases: SchemaTestCase[] = [
},
{
schema: z.union([z.string(), z.undefined()]),
input: [false, { type: 'string' }],
input: [false, { anyOf: [{ type: 'string' }] }],
},
{
schema: z.intersection(z.string(), z.number()),
Expand Down
8 changes: 2 additions & 6 deletions packages/zod/src/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ export class ZodToJsonSchemaConverter implements ConditionalSchemaConverter {
let required = true

for (const item of schema_._def.options) {
const [itemRequired, itemJson] = this.convert(item, options, lazyDepth, false, false, structureDepth)
const [itemRequired, itemJson] = this.convert(item, options, lazyDepth, false, false, structureDepth + 1)

if (!itemRequired) {
required = false
Expand All @@ -527,10 +527,6 @@ export class ZodToJsonSchemaConverter implements ConditionalSchemaConverter {
}
}

if (anyOf.length === 1) {
return [required, anyOf[0]!]
}

return [required, { anyOf }]
}

Expand All @@ -541,7 +537,7 @@ export class ZodToJsonSchemaConverter implements ConditionalSchemaConverter {
let required: boolean = false

for (const item of [schema_._def.left, schema_._def.right]) {
const [itemRequired, itemJson] = this.convert(item, options, lazyDepth, false, false, structureDepth)
const [itemRequired, itemJson] = this.convert(item, options, lazyDepth, false, false, structureDepth + 1)

allOf.push(itemJson)

Expand Down
2 changes: 1 addition & 1 deletion packages/zod/src/zod4/converter.combination.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ testSchemaConverter([
{
name: 'union([z.string(), z.undefined()])',
schema: z.union([z.string(), z.undefined()]),
input: [false, { type: 'string' }],
input: [false, { anyOf: [{ type: 'string' }] }],
},
{
name: 'intersection(z.string(), z.number())',
Expand Down
6 changes: 3 additions & 3 deletions packages/zod/src/zod4/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ export class ZodToJsonSchemaConverter implements ConditionalSchemaConverter {
let required = true

for (const item of union._zod.def.options) {
const [itemRequired, itemJson] = this.#convert(item, options, lazyDepth, structureDepth)
const [itemRequired, itemJson] = this.#convert(item, options, lazyDepth, structureDepth + 1)

if (!itemRequired) {
required = false
Expand All @@ -343,7 +343,7 @@ export class ZodToJsonSchemaConverter implements ConditionalSchemaConverter {
}
}

return [required, anyOf.length === 1 ? anyOf[0]! : { anyOf }]
return [required, { anyOf }]
}

case 'intersection': {
Expand All @@ -353,7 +353,7 @@ export class ZodToJsonSchemaConverter implements ConditionalSchemaConverter {
let required = false

for (const item of [intersection._zod.def.left, intersection._zod.def.right]) {
const [itemRequired, itemJson] = this.#convert(item, options, lazyDepth, structureDepth)
const [itemRequired, itemJson] = this.#convert(item, options, lazyDepth, structureDepth + 1)

json.allOf.push(itemJson)

Expand Down
Loading