zod-paginate 1.5.0 → 1.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -271,12 +271,12 @@ export type PaginationResponseSchemaShape<TType extends PaginationType> = TType
271
271
  * return paginate({ dataSchema: MySchema, … });
272
272
  * }
273
273
  */
274
- export interface PaginateResult<TSchema extends DataSchema, TSelectable extends AllowedPath<TSchema> = AllowedPath<TSchema>, TType extends PaginationType = PaginationType> {
274
+ export interface PaginateResult<TSchema extends DataSchema, TType extends PaginationType = PaginationType> {
275
275
  queryParamsSchema: {
276
276
  (): z.ZodType<PaginationQueryParams<TSchema, TType>>;
277
277
  <TExtraShape extends z.ZodRawShape>(extraShape: TExtraShape): z.ZodType<PaginationQueryParams<TSchema, TType> & z.infer<z.ZodObject<TExtraShape>>>;
278
278
  };
279
- validatorSchema: (parsed?: PaginationPayload<TSchema>) => z.ZodType;
279
+ validatorSchema: (parsed?: PaginationPayload<TSchema>) => z.ZodType<PaginationResponse<TSchema, AllowedPath<TSchema>, TType>>;
280
280
  responseSchema: z.ZodObject<PaginationResponseSchemaShape<TType>>;
281
281
  }
282
282
  /**
@@ -298,7 +298,7 @@ export declare function paginate<TSchema extends DataSchema, const TSelectable e
298
298
  filterable?: Partial<{
299
299
  [P in NoInfer<TSelectable[number]>]: FilterableFieldConfig<FieldTypeFromValue<PathValue<InferData<TSchema>, P>>>;
300
300
  }>;
301
- }): PaginateResult<TSchema, TSelectable[number], 'LIMIT_OFFSET'>;
301
+ }): PaginateResult<TSchema, 'LIMIT_OFFSET'>;
302
302
  export declare function paginate<TSchema extends DataSchema, const TSelectable extends readonly AllowedPath<TSchema>[]>(config: Omit<CommonQueryConfigFromSchema<TSchema, TSelectable[number]>, 'selectable' | 'defaultSelect' | 'sortable' | 'defaultSortBy' | 'filterable'> & CursorPaginationConfig<InferData<TSchema>> & {
303
303
  selectable?: EnsureDiscriminatorInSelectable<TSchema, TSelectable>;
304
304
  defaultSelect: readonly NoInfer<TSelectable[number]>[] | '*';
@@ -310,7 +310,7 @@ export declare function paginate<TSchema extends DataSchema, const TSelectable e
310
310
  filterable?: Partial<{
311
311
  [P in NoInfer<TSelectable[number]>]: FilterableFieldConfig<FieldTypeFromValue<PathValue<InferData<TSchema>, P>>>;
312
312
  }>;
313
- }): PaginateResult<TSchema, TSelectable[number], 'CURSOR'>;
313
+ }): PaginateResult<TSchema, 'CURSOR'>;
314
314
  export declare function paginate<TSchema extends DataSchema, const TSelectable extends readonly AllowedPath<TSchema>[]>(config: Omit<CommonQueryConfigFromSchema<TSchema, TSelectable[number]>, 'selectable' | 'defaultSelect' | 'sortable' | 'defaultSortBy' | 'filterable'> & {
315
315
  selectable?: EnsureDiscriminatorInSelectable<TSchema, TSelectable>;
316
316
  defaultSelect: readonly NoInfer<TSelectable[number]>[] | '*';
@@ -322,5 +322,5 @@ export declare function paginate<TSchema extends DataSchema, const TSelectable e
322
322
  filterable?: Partial<{
323
323
  [P in NoInfer<TSelectable[number]>]: FilterableFieldConfig<FieldTypeFromValue<PathValue<InferData<TSchema>, P>>>;
324
324
  }>;
325
- } & (LimitOffsetPaginationConfig | CursorPaginationConfig<InferData<TSchema>>)): PaginateResult<TSchema, TSelectable[number]>;
325
+ } & (LimitOffsetPaginationConfig | CursorPaginationConfig<InferData<TSchema>>)): PaginateResult<TSchema>;
326
326
  export {};
package/dist/paginate.js CHANGED
@@ -501,45 +501,47 @@ function isFiniteNumber(v) {
501
501
  function validateConditionType(expected, cond, field) {
502
502
  if (expected === 'any')
503
503
  return null;
504
- if (cond.op === '$null')
505
- return null;
506
- if (cond.op === '$eq') {
507
- if (expected === 'number' && !isFiniteNumber(cond.value))
508
- return `Field "${field}" expects a number for "$eq"`;
509
- if (expected === 'date' && !isISODateString(cond.value))
510
- return `Field "${field}" expects an ISO date for "$eq"`;
511
- if (expected === 'string' && typeof cond.value !== 'string')
512
- return `Field "${field}" expects a string for "$eq"`;
513
- return null;
514
- }
515
- if (cond.op === '$ilike' || cond.op === '$sw') {
516
- if (expected !== 'string')
517
- return `Field "${field}" does not support "${cond.op}" (configured as ${expected})`;
518
- return null;
519
- }
520
- if (cond.op === '$in' || cond.op === '$contains')
521
- return null;
522
- if (cond.op === '$gt' || cond.op === '$gte' || cond.op === '$lt' || cond.op === '$lte') {
523
- if (expected === 'string')
524
- return `Field "${field}" does not support "${cond.op}" (configured as string)`;
525
- if (expected === 'number' && !isFiniteNumber(cond.value))
526
- return `Field "${field}" expects number for "${cond.op}"`;
527
- if (expected === 'date' && !isISODateString(cond.value))
528
- return `Field "${field}" expects ISO date for "${cond.op}"`;
529
- return null;
530
- }
531
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
532
- if (cond.op === '$btw') {
533
- const [a, b] = cond.value;
534
- if (expected === 'string')
535
- return `Field "${field}" does not support "$btw" (configured as string)`;
536
- if (expected === 'number' && (!isFiniteNumber(a) || !isFiniteNumber(b)))
537
- return `Field "${field}" expects numbers for "$btw"`;
538
- if (expected === 'date' && (!isISODateString(a) || !isISODateString(b)))
539
- return `Field "${field}" expects ISO dates for "$btw"`;
540
- return null;
504
+ switch (cond.op) {
505
+ case '$null':
506
+ return null;
507
+ case '$eq':
508
+ if (expected === 'number' && !isFiniteNumber(cond.value))
509
+ return `Field "${field}" expects a number for "$eq"`;
510
+ if (expected === 'date' && !isISODateString(cond.value))
511
+ return `Field "${field}" expects an ISO date for "$eq"`;
512
+ if (expected === 'string' && typeof cond.value !== 'string')
513
+ return `Field "${field}" expects a string for "$eq"`;
514
+ return null;
515
+ case '$ilike':
516
+ case '$sw':
517
+ if (expected !== 'string')
518
+ return `Field "${field}" does not support "${cond.op}" (configured as ${expected})`;
519
+ return null;
520
+ case '$in':
521
+ case '$contains':
522
+ return null;
523
+ case '$gt':
524
+ case '$gte':
525
+ case '$lt':
526
+ case '$lte':
527
+ if (expected === 'string')
528
+ return `Field "${field}" does not support "${cond.op}" (configured as string)`;
529
+ if (expected === 'number' && !isFiniteNumber(cond.value))
530
+ return `Field "${field}" expects number for "${cond.op}"`;
531
+ if (expected === 'date' && !isISODateString(cond.value))
532
+ return `Field "${field}" expects ISO date for "${cond.op}"`;
533
+ return null;
534
+ case '$btw': {
535
+ const [a, b] = cond.value;
536
+ if (expected === 'string')
537
+ return `Field "${field}" does not support "$btw" (configured as string)`;
538
+ if (expected === 'number' && (!isFiniteNumber(a) || !isFiniteNumber(b)))
539
+ return `Field "${field}" expects numbers for "$btw"`;
540
+ if (expected === 'date' && (!isISODateString(a) || !isISODateString(b)))
541
+ return `Field "${field}" expects ISO dates for "$btw"`;
542
+ return null;
543
+ }
541
544
  }
542
- return null;
543
545
  }
544
546
  function computeSortBy(sortByRaw, config) {
545
547
  if (sortByRaw) {
@@ -906,7 +908,9 @@ function paginate(config) {
906
908
  // Check nested discriminated unions
907
909
  if (!hasWildcard) {
908
910
  for (const nested of nestedDiscriminators) {
909
- const hasAnyFieldUnderPrefix = selectForValidation.some((f) => f === nested.prefix || f.startsWith(`${nested.prefix}.`));
911
+ const hasAnyFieldUnderPrefix = nested.prefix === ''
912
+ ? selectForValidation.length > 0
913
+ : selectForValidation.some((f) => f === nested.prefix || f.startsWith(`${nested.prefix}.`));
910
914
  if (hasAnyFieldUnderPrefix &&
911
915
  !selectForValidation.includes(nested.discriminatorPath)) {
912
916
  ctx.addIssue({
@@ -1055,7 +1059,7 @@ function paginate(config) {
1055
1059
  const LIMIT_OFFSET_META_DESC = 'Pagination metadata for limit/offset mode';
1056
1060
  const CURSOR_META_DESC = 'Pagination metadata for cursor mode';
1057
1061
  const CURSOR_VALUE_DESC = 'Cursor value pointing to the last item returned';
1058
- const validatorSchema = (parsed) => {
1062
+ function validatorSchema(parsed) {
1059
1063
  const effectiveSelect = parsed?.select ?? (0, select_1.computeSelect)(undefined, effectiveConfig) ?? undefined;
1060
1064
  const dataItemSchema = effectiveSelect && effectiveSelect.length > 0
1061
1065
  ? (0, select_1.projectDataSchemaPreservingUnion)(config.dataSchema, effectiveSelect.map(String))
@@ -1087,7 +1091,7 @@ function paginate(config) {
1087
1091
  })
1088
1092
  .meta({ description: CURSOR_META_DESC }),
1089
1093
  });
1090
- };
1094
+ }
1091
1095
  const partialDataItemSchema = selectableStrings.length > 0
1092
1096
  ? (0, select_1.projectDataSchemaPreservingUnion)(config.dataSchema, selectableStrings, { partial: true })
1093
1097
  : (0, select_1.resolveToZodObject)(config.dataSchema);