Skip to content

Commit d56837f

Browse files
committed
feat(request): don't make filter and page parameters optional by default
This allows to define defaults within the Zod schema for filter and page instead of having to resolve it within the middleware. BREAKING CHANGE: when filter or page is defined, it is now required by default. To get the old behavior, add `.optional()` to your schemas.
1 parent 93a7e62 commit d56837f

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

src/request.ts

+6-12
Original file line numberDiff line numberDiff line change
@@ -364,20 +364,18 @@ type ParseListQueryOptions<
364364
};
365365

366366
type OptionalSchema<T extends z.ZodType<unknown> | undefined> = T extends z.ZodType<unknown>
367-
? z.ZodOptional<T>
367+
? T
368368
: z.ZodUndefined;
369369

370-
type OptionalOutput<T extends z.ZodType<unknown> | undefined> = z.output<OptionalSchema<T>>;
371-
372370
type ParseListQueryResult<
373371
TContext,
374372
TSort extends string,
375373
TFilterSchema extends z.ZodType<unknown> | undefined,
376374
TPageSchema extends z.ZodType<unknown> | undefined,
377375
> = ParseBaseQueryResult<TContext> & {
378376
sort?: Sort<TSort>;
379-
filter?: OptionalOutput<TFilterSchema>;
380-
page?: OptionalOutput<TPageSchema>;
377+
filter: z.output<OptionalSchema<TFilterSchema>>;
378+
page: z.output<OptionalSchema<TPageSchema>>;
381379
};
382380

383381
const processSort = <TSort extends string>(
@@ -450,7 +448,7 @@ export const parseBaseQuery = <TContext = undefined>(
450448
};
451449

452450
type MergedListQuerySchema<
453-
TFilterSchema extends z.ZodType<unknown> | undefined,
451+
TFilterSchema extends z.ZodType<unknown> | undefined = undefined,
454452
TPageSchema extends z.ZodType<unknown> | undefined = undefined,
455453
> = ReturnType<
456454
typeof listQuerySchema.extend<{
@@ -466,12 +464,8 @@ const getListQuerySchema = <
466464
options?: ParseListQueryOptions<unknown, string, TFilterSchema, TPageSchema>,
467465
): MergedListQuerySchema<TFilterSchema, TPageSchema> => {
468466
return listQuerySchema.extend({
469-
filter: (options?.filterSchema
470-
? options.filterSchema.optional()
471-
: z.undefined()) as OptionalSchema<TFilterSchema>,
472-
page: (options?.pageSchema
473-
? options.pageSchema.optional()
474-
: z.undefined()) as OptionalSchema<TPageSchema>,
467+
filter: (options?.filterSchema ?? z.undefined()) as OptionalSchema<TFilterSchema>,
468+
page: (options?.pageSchema ?? z.undefined()) as OptionalSchema<TPageSchema>,
475469
});
476470
};
477471

0 commit comments

Comments
 (0)