Skip to content

Commit

Permalink
[zod-openapi] Merge additional OpenAPIdef with ZodPipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
pepakriz committed Mar 4, 2024
1 parent 1646808 commit acb5608
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
42 changes: 42 additions & 0 deletions packages/zod-openapi/src/lib/zod-openapi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,48 @@ describe('zodOpenapi', () => {
} satisfies SchemaObject);
});

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

expect(
generateSchema(
extendApi(
z
.string()
.regex(/^\d+$/)
.transform(Number)
.pipe(z.number().min(0).max(10)),
{
description: 'Foo description',
}
),
true
)
).toEqual({
type: 'number',
minimum: 0,
maximum: 10,
description: 'Foo description',
} satisfies SchemaObject);
});


it('should work with ZodTransform and correctly set nullable and optional', () => {
type Type = string;
Expand Down
9 changes: 5 additions & 4 deletions packages/zod-openapi/src/lib/zod-openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,13 +527,14 @@ function catchAllParser({
}

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

function parseReadonly({
Expand Down

0 comments on commit acb5608

Please sign in to comment.