All files / src/schemas pagination.schemas.ts

100% Statements 4/4
100% Branches 0/0
100% Functions 0/0
100% Lines 4/4

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87                  4x                                 4x                                           4x                                           4x                                
/**
 * 分页参数 Zod Schema 定义
 */
 
import { z } from 'zod';
 
/**
 * 默认分页配置 Schema
 */
export const PaginationDefaultsSchema = z.object({
  page: z.literal(1),
  pageSize: z.literal(20),
  maxPageSize: z.literal(100),
  minPageSize: z.literal(1),
});
 
/**
 * 默认分页参数类型
 */
export type PaginationDefaults = z.infer<typeof PaginationDefaultsSchema>;
 
/**
 * 分页参数 Schema(page 风格,推荐使用)
 *
 * 适用于用户界面分页场景
 */
export const PaginationParamsSchema = z.object({
  /**
   * 页码(从 1 开始)
   */
  page: z.number().int().positive().optional(),
 
  /**
   * 每页数量
   */
  pageSize: z.number().int().positive().optional(),
});
 
/**
 * 分页参数类型(page 风格)
 */
export type PaginationParams = z.infer<typeof PaginationParamsSchema>;
 
/**
 * Offset 分页参数 Schema
 *
 * 适用于 API 调用、数据库查询等场景
 */
export const OffsetPaginationParamsSchema = z.object({
  /**
   * 限制数量(正整数)
   */
  limit: z.number().int().positive().optional(),
 
  /**
   * 偏移量(非负整数)
   */
  offset: z.number().int().nonnegative().optional(),
});
 
/**
 * Offset 分页参数类型
 */
export type OffsetPaginationParams = z.infer<typeof OffsetPaginationParamsSchema>;
 
/**
 * 游标分页参数 Schema
 *
 * 适用于无限滚动、实时数据流场景
 */
export const CursorPaginationParamsSchema = z.object({
  /**
   * 游标
   */
  cursor: z.string().optional(),
 
  /**
   * 限制数量
   */
  limit: z.number().int().positive().optional(),
});
 
/**
 * 游标分页参数类型
 */
export type CursorPaginationParams = z.infer<typeof CursorPaginationParamsSchema>;