Skip to content

Commit

Permalink
feat: parse ZodPipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
stLmpp committed Aug 8, 2023
1 parent 547e9a8 commit b3e6cb7
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions packages/zod-openapi/src/lib/zod-openapi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -931,4 +931,34 @@ describe('zodOpenapi', () => {
]
});
})

it('should work with ZodPipeline', () => {
expect(
generateSchema(
z
.string()
.regex(/^\d+$/)
.transform(Number)
.pipe(z.number().min(0).max(10))
)
).toEqual({
type: 'string',
pattern: '^\\d+$',
} satisfies SchemaObject);

expect(
generateSchema(
z
.string()
.regex(/^\d+$/)
.transform(Number)
.pipe(z.number().min(0).max(10)),
true
)
).toEqual({
type: 'number',
minimum: 0,
maximum: 10,
} satisfies SchemaObject);
});
});
11 changes: 11 additions & 0 deletions packages/zod-openapi/src/lib/zod-openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,16 @@ function catchAllParser({
);
}

function parsePipeline({
zodRef,
useOutput,
}: ParsingArgs<z.ZodPipeline<never, never>>): SchemaObject {
if (useOutput) {
return generateSchema(zodRef._def.out, useOutput);
}
return generateSchema(zodRef._def.in, useOutput);
}

const workerMap = {
ZodObject: parseObject,
ZodRecord: parseRecord,
Expand Down Expand Up @@ -545,6 +555,7 @@ const workerMap = {
ZodAny: catchAllParser,
ZodUnknown: catchAllParser,
ZodVoid: catchAllParser,
ZodPipeline: parsePipeline,
};
type WorkerKeys = keyof typeof workerMap;

Expand Down

0 comments on commit b3e6cb7

Please sign in to comment.