Skip to content

Commit

Permalink
Merge pull request #240 from asteasolutions/238-nested-effects-for-re…
Browse files Browse the repository at this point in the history
…quest-params

allow nested effects on parameters
  • Loading branch information
AGalabov authored Jun 17, 2024
2 parents cf6e694 + 7eed72d commit 26ed121
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
20 changes: 12 additions & 8 deletions spec/routes/parameters.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ describe('parameters', () => {
]);
});

it('generates query parameter for route from object with refine', () => {
it('generates query parameter for route from object with effects', () => {
const { parameters } = generateDataForRoute({
request: {
query: z
.object({
filter: z.string(),
})
.refine(({ filter }) => filter.length > 3),
.refine(({ filter }) => filter.length > 3)
.transform(d => d),
},
});

Expand Down Expand Up @@ -142,14 +143,15 @@ describe('parameters', () => {
]);
});

it('generates path parameter for route from object with refine', () => {
it('generates path parameter for route from object with effects', () => {
const { parameters } = generateDataForRoute({
request: {
params: z
.object({
filter: z.string(),
})
.refine(({ filter }) => filter.length > 3),
.refine(({ filter }) => filter.length > 3)
.transform(d => d),
},
});

Expand Down Expand Up @@ -244,14 +246,15 @@ describe('parameters', () => {
]);
});

it('generates cookie parameter for route from object with refine', () => {
it('generates cookie parameter for route from object with effects', () => {
const { parameters } = generateDataForRoute({
request: {
cookies: z
.object({
filter: z.string(),
})
.refine(({ filter }) => filter.length > 3),
.refine(({ filter }) => filter.length > 3)
.transform(d => d),
},
});

Expand Down Expand Up @@ -367,14 +370,15 @@ describe('parameters', () => {
]);
});

it('generates header parameter for route from object with refine', () => {
it('generates header parameter for route from object with effects', () => {
const { parameters } = generateDataForRoute({
request: {
headers: z
.object({
filter: z.string(),
})
.refine(({ filter }) => filter.length > 3),
.refine(({ filter }) => filter.length > 3)
.transform(d => d),
},
});

Expand Down
6 changes: 4 additions & 2 deletions src/openapi-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,12 +545,14 @@ export class OpenAPIGenerator {
];
}

private cleanParameter(schema: RouteParameter) {
private cleanParameter(schema: RouteParameter): AnyZodObject | undefined {
if (!schema) {
return undefined;
}

return isZodType(schema, 'ZodEffects') ? schema._def.schema : schema;
return isZodType(schema, 'ZodEffects')
? this.cleanParameter(schema._def.schema)
: schema;
}

generatePath(route: RouteConfig): PathItemObject {
Expand Down
7 changes: 4 additions & 3 deletions src/openapi-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,11 @@ export interface ResponseConfig {
content?: ZodContentObject;
}

export type RouteParameter =
type ZodObjectWithEffect =
| AnyZodObject
| ZodEffects<AnyZodObject, unknown, unknown>
| undefined;
| ZodEffects<ZodObjectWithEffect, unknown, unknown>;

export type RouteParameter = ZodObjectWithEffect | undefined;

export type RouteConfig = Omit<OperationObject, 'responses'> & {
method: Method;
Expand Down

0 comments on commit 26ed121

Please sign in to comment.