Skip to content

Commit 1bafe62

Browse files
Merge pull request #157 from stLmpp/feat/pipeline
feat: parse ZodPipeline
2 parents 547e9a8 + b3e6cb7 commit 1bafe62

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

package-lock.json

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/zod-openapi/src/lib/zod-openapi.spec.ts

+30
Original file line numberDiff line numberDiff line change
@@ -931,4 +931,34 @@ describe('zodOpenapi', () => {
931931
]
932932
});
933933
})
934+
935+
it('should work with ZodPipeline', () => {
936+
expect(
937+
generateSchema(
938+
z
939+
.string()
940+
.regex(/^\d+$/)
941+
.transform(Number)
942+
.pipe(z.number().min(0).max(10))
943+
)
944+
).toEqual({
945+
type: 'string',
946+
pattern: '^\\d+$',
947+
} satisfies SchemaObject);
948+
949+
expect(
950+
generateSchema(
951+
z
952+
.string()
953+
.regex(/^\d+$/)
954+
.transform(Number)
955+
.pipe(z.number().min(0).max(10)),
956+
true
957+
)
958+
).toEqual({
959+
type: 'number',
960+
minimum: 0,
961+
maximum: 10,
962+
} satisfies SchemaObject);
963+
});
934964
});

packages/zod-openapi/src/lib/zod-openapi.ts

+11
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,16 @@ function catchAllParser({
511511
);
512512
}
513513

514+
function parsePipeline({
515+
zodRef,
516+
useOutput,
517+
}: ParsingArgs<z.ZodPipeline<never, never>>): SchemaObject {
518+
if (useOutput) {
519+
return generateSchema(zodRef._def.out, useOutput);
520+
}
521+
return generateSchema(zodRef._def.in, useOutput);
522+
}
523+
514524
const workerMap = {
515525
ZodObject: parseObject,
516526
ZodRecord: parseRecord,
@@ -545,6 +555,7 @@ const workerMap = {
545555
ZodAny: catchAllParser,
546556
ZodUnknown: catchAllParser,
547557
ZodVoid: catchAllParser,
558+
ZodPipeline: parsePipeline,
548559
};
549560
type WorkerKeys = keyof typeof workerMap;
550561

0 commit comments

Comments
 (0)