Skip to content

Commit fc2dc8f

Browse files
authored
fix(zod): enhance schema conversion logic for zod4 preprocess (#1318)
Fixes #1317 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Tests** * Added test coverage for preprocess functionality with string schemas. * **Bug Fixes** * Improved handling of pipe operations in schema conversion to more accurately process transformations with input strategy. <sub>✏️ Tip: You can customize this high-level summary in your review settings.</sub> <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent a8f52f0 commit fc2dc8f

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

packages/zod/src/zod4/converter.processed.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,10 @@ testSchemaConverter([
1818
input: [true, { type: 'string' }],
1919
output: [false, {}],
2020
},
21+
{
22+
name: 'preprocess(x => x, z.string())',
23+
schema: z.preprocess(x => x, z.string()),
24+
input: [true, { type: 'string' }],
25+
output: [true, { type: 'string' }],
26+
},
2127
])

packages/zod/src/zod4/converter.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,15 @@ export class ZodToJsonSchemaConverter implements ConditionalSchemaConverter {
519519

520520
case 'pipe': {
521521
const pipe = schema as $ZodPipe
522-
return this.#convert(options.strategy === 'input' ? pipe._zod.def.in : pipe._zod.def.out, options, lazyDepth, structureDepth)
522+
return this.#convert(
523+
// prefer out schema when in schema is preprocess/transform
524+
options.strategy === 'input' && pipe._zod.def.in._zod.def.type !== 'transform'
525+
? pipe._zod.def.in
526+
: pipe._zod.def.out,
527+
options,
528+
lazyDepth,
529+
structureDepth,
530+
)
523531
}
524532

525533
case 'readonly': {

0 commit comments

Comments
 (0)