Skip to content

Commit 39ae76b

Browse files
authored
Merge pull request #70 from samchungy/fix-route-params-query
Fix Route types to support strict objects
2 parents 90ecc97 + 57994cd commit 39ae76b

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

spec/routes.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,28 @@ const routeTests = ({
302302
]);
303303
});
304304

305+
it('supports strict zod objects', () => {
306+
const routeParameters = generateParamsForRoute({
307+
request: {
308+
query: z.strictObject({
309+
test: z.string().optional().default('test'),
310+
}),
311+
},
312+
});
313+
314+
expect(routeParameters).toEqual([
315+
{
316+
in: 'query',
317+
name: 'test',
318+
required: false,
319+
schema: {
320+
type: 'string',
321+
default: 'test',
322+
},
323+
},
324+
]);
325+
});
326+
305327
describe('errors', () => {
306328
it('throws an error in case of names mismatch', () => {
307329
expect(() =>

src/lib/zod-is-type.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type ZodTypes = {
1212
ZodNull: z.ZodNull;
1313
ZodNullable: z.ZodNullable<any>;
1414
ZodNumber: z.ZodNumber;
15-
ZodObject: z.ZodObject<any>;
15+
ZodObject: z.AnyZodObject;
1616
ZodOptional: z.ZodOptional<any>;
1717
ZodRecord: z.ZodRecord;
1818
ZodSchema: z.ZodSchema;

src/openapi-generator.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
DiscriminatorObject,
1313
} from 'openapi3-ts';
1414
import type {
15+
AnyZodObject,
1516
ZodObject,
1617
ZodRawShape,
1718
ZodSchema,
@@ -588,7 +589,7 @@ export class OpenAPIGenerator {
588589
}
589590

590591
private mapDiscriminator(
591-
zodObjects: ZodObject<any>[],
592+
zodObjects: AnyZodObject[],
592593
discriminator: string
593594
): DiscriminatorObject | undefined {
594595
// All schemas must be registered to use a discriminator

src/openapi-registry.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
SchemaObject,
1818
SecuritySchemeObject,
1919
} from 'openapi3-ts';
20-
import type { ZodObject, ZodSchema, ZodType } from 'zod';
20+
import type { AnyZodObject, ZodSchema, ZodType } from 'zod';
2121

2222
type Method = 'get' | 'post' | 'put' | 'delete' | 'patch';
2323

@@ -50,8 +50,8 @@ export interface RouteConfig extends OperationObject {
5050
path: string;
5151
request?: {
5252
body?: ZodRequestBody;
53-
params?: ZodObject<any>;
54-
query?: ZodObject<any>;
53+
params?: AnyZodObject;
54+
query?: AnyZodObject;
5555
headers?: ZodType<unknown>[];
5656
};
5757
responses: {

0 commit comments

Comments
 (0)