@veloxts/validation 0.2.0 → 0.3.0

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.
@@ -0,0 +1,168 @@
1
+ /**
2
+ * Common schema utilities and pre-built schemas
3
+ *
4
+ * Provides commonly used validation patterns with proper type inference.
5
+ *
6
+ * @module schemas/common
7
+ */
8
+ import { z } from 'zod';
9
+ /**
10
+ * UUID v4 string schema
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * const id = uuidSchema.parse('123e4567-e89b-12d3-a456-426614174000');
15
+ * // id: string (validated UUID)
16
+ * ```
17
+ */
18
+ export declare const uuidSchema: z.ZodString;
19
+ /**
20
+ * Email address schema
21
+ */
22
+ export declare const emailSchema: z.ZodString;
23
+ /**
24
+ * Non-empty string schema
25
+ */
26
+ export declare const nonEmptyStringSchema: z.ZodString;
27
+ /**
28
+ * URL string schema
29
+ */
30
+ export declare const urlSchema: z.ZodString;
31
+ /**
32
+ * ISO 8601 datetime string schema
33
+ */
34
+ export declare const datetimeSchema: z.ZodString;
35
+ /**
36
+ * Generic ID parameter schema (UUID)
37
+ * Commonly used for route parameters like `/users/:id`
38
+ */
39
+ export declare const idParamSchema: z.ZodObject<{
40
+ id: z.ZodString;
41
+ }, "strip", z.ZodTypeAny, {
42
+ id: string;
43
+ }, {
44
+ id: string;
45
+ }>;
46
+ /**
47
+ * Type for id parameter objects
48
+ */
49
+ export type IdParam = z.infer<typeof idParamSchema>;
50
+ /**
51
+ * Creates an ID schema that accepts either UUID or integer string
52
+ *
53
+ * @param type - Type of ID to accept ('uuid' | 'integer' | 'string')
54
+ * @returns Zod schema for the specified ID type
55
+ */
56
+ export declare function createIdSchema<T extends 'uuid' | 'integer' | 'string'>(type: T): T extends 'uuid' ? typeof uuidSchema : T extends 'integer' ? z.ZodPipeline<z.ZodEffects<z.ZodString, number, string>, z.ZodNumber> : z.ZodString;
57
+ /**
58
+ * Timestamp fields commonly added to database records
59
+ */
60
+ export declare const timestampFieldsSchema: z.ZodObject<{
61
+ createdAt: z.ZodDate;
62
+ updatedAt: z.ZodDate;
63
+ }, "strip", z.ZodTypeAny, {
64
+ createdAt: Date;
65
+ updatedAt: Date;
66
+ }, {
67
+ createdAt: Date;
68
+ updatedAt: Date;
69
+ }>;
70
+ /**
71
+ * Type for timestamp fields
72
+ */
73
+ export type TimestampFields = z.infer<typeof timestampFieldsSchema>;
74
+ /**
75
+ * Base entity schema with ID and timestamps
76
+ */
77
+ export declare const baseEntitySchema: z.ZodObject<{
78
+ id: z.ZodString;
79
+ createdAt: z.ZodDate;
80
+ updatedAt: z.ZodDate;
81
+ }, "strip", z.ZodTypeAny, {
82
+ id: string;
83
+ createdAt: Date;
84
+ updatedAt: Date;
85
+ }, {
86
+ id: string;
87
+ createdAt: Date;
88
+ updatedAt: Date;
89
+ }>;
90
+ /**
91
+ * Type for base entity
92
+ */
93
+ export type BaseEntity = z.infer<typeof baseEntitySchema>;
94
+ /**
95
+ * Makes all fields in a schema optional
96
+ *
97
+ * @param schema - Zod object schema
98
+ * @returns Schema with all fields optional
99
+ *
100
+ * @example
101
+ * ```typescript
102
+ * const UpdateUserSchema = makePartial(UserSchema);
103
+ * // All fields are now optional
104
+ * ```
105
+ */
106
+ export declare function makePartial<T extends z.ZodRawShape>(schema: z.ZodObject<T>): z.ZodObject<{
107
+ [K in keyof T]: z.ZodOptional<T[K]>;
108
+ }>;
109
+ /**
110
+ * Makes specific fields required in a partial schema
111
+ *
112
+ * @param schema - Zod object schema
113
+ * @param keys - Array of keys to make required
114
+ * @returns Schema with specified fields required, others optional
115
+ *
116
+ * @example
117
+ * ```typescript
118
+ * const UpdateUserSchema = partialExcept(UserSchema, ['id'] as const);
119
+ * // id is required, all other fields are optional
120
+ * ```
121
+ */
122
+ export declare function partialExcept<T extends z.ZodRawShape, K extends keyof T & string>(schema: z.ZodObject<T>, keys: readonly K[]): z.ZodObject<{
123
+ [P in K]: T[P];
124
+ } & {
125
+ [P in Exclude<keyof T, K>]: z.ZodOptional<T[P]>;
126
+ }>;
127
+ /**
128
+ * Omits specified fields from a schema
129
+ *
130
+ * @param schema - Zod object schema
131
+ * @param keys - Array of keys to omit
132
+ * @returns Schema without the specified fields
133
+ *
134
+ * @example
135
+ * ```typescript
136
+ * const UserWithoutPassword = omitFields(UserSchema, ['password'] as const);
137
+ * ```
138
+ */
139
+ export declare function omitFields<T extends z.ZodRawShape, K extends keyof T & string>(schema: z.ZodObject<T>, keys: readonly K[]): z.ZodObject<Omit<T, K>>;
140
+ /**
141
+ * Picks specified fields from a schema
142
+ *
143
+ * @param schema - Zod object schema
144
+ * @param keys - Array of keys to pick
145
+ * @returns Schema with only the specified fields
146
+ *
147
+ * @example
148
+ * ```typescript
149
+ * const UserIdAndName = pickFields(UserSchema, ['id', 'name'] as const);
150
+ * ```
151
+ */
152
+ export declare function pickFields<T extends z.ZodRawShape, K extends keyof T & string>(schema: z.ZodObject<T>, keys: readonly K[]): z.ZodObject<Pick<T, K>>;
153
+ /**
154
+ * String that coerces to boolean
155
+ * Accepts 'true', '1', 'yes' as true; 'false', '0', 'no' as false
156
+ */
157
+ export declare const booleanStringSchema: z.ZodPipeline<z.ZodEffects<z.ZodString, boolean | undefined, string>, z.ZodBoolean>;
158
+ /**
159
+ * String that coerces to number
160
+ * Useful for query parameters
161
+ */
162
+ export declare const numberStringSchema: z.ZodPipeline<z.ZodEffects<z.ZodString, number, string>, z.ZodNumber>;
163
+ /**
164
+ * String that coerces to integer
165
+ * Useful for pagination parameters
166
+ */
167
+ export declare const integerStringSchema: z.ZodPipeline<z.ZodEffects<z.ZodString, number, string>, z.ZodNumber>;
168
+ //# sourceMappingURL=common.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"common.d.ts","sourceRoot":"","sources":["../../src/schemas/common.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAMxB;;;;;;;;GAQG;AACH,eAAO,MAAM,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AAmB,uEAGP,CAAC"}
@@ -0,0 +1,211 @@
1
+ /**
2
+ * Common schema utilities and pre-built schemas
3
+ *
4
+ * Provides commonly used validation patterns with proper type inference.
5
+ *
6
+ * @module schemas/common
7
+ */
8
+ import { z } from 'zod';
9
+ // ============================================================================
10
+ // Common String Schemas
11
+ // ============================================================================
12
+ /**
13
+ * UUID v4 string schema
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * const id = uuidSchema.parse('123e4567-e89b-12d3-a456-426614174000');
18
+ * // id: string (validated UUID)
19
+ * ```
20
+ */
21
+ export const uuidSchema = z.string().uuid();
22
+ /**
23
+ * Email address schema
24
+ */
25
+ export const emailSchema = z.string().email();
26
+ /**
27
+ * Non-empty string schema
28
+ */
29
+ export const nonEmptyStringSchema = z.string().min(1);
30
+ /**
31
+ * URL string schema
32
+ */
33
+ export const urlSchema = z.string().url();
34
+ /**
35
+ * ISO 8601 datetime string schema
36
+ */
37
+ export const datetimeSchema = z.string().datetime();
38
+ // ============================================================================
39
+ // Common ID Schemas
40
+ // ============================================================================
41
+ /**
42
+ * Generic ID parameter schema (UUID)
43
+ * Commonly used for route parameters like `/users/:id`
44
+ */
45
+ export const idParamSchema = z.object({
46
+ id: uuidSchema,
47
+ });
48
+ /**
49
+ * Creates an ID schema that accepts either UUID or integer string
50
+ *
51
+ * @param type - Type of ID to accept ('uuid' | 'integer' | 'string')
52
+ * @returns Zod schema for the specified ID type
53
+ */
54
+ export function createIdSchema(type) {
55
+ switch (type) {
56
+ case 'uuid':
57
+ return uuidSchema;
58
+ case 'integer':
59
+ return z
60
+ .string()
61
+ .transform((val) => parseInt(val, 10))
62
+ .pipe(z.number().int().positive());
63
+ default:
64
+ return z.string().min(1);
65
+ }
66
+ }
67
+ // ============================================================================
68
+ // Common Object Schemas
69
+ // ============================================================================
70
+ /**
71
+ * Timestamp fields commonly added to database records
72
+ */
73
+ export const timestampFieldsSchema = z.object({
74
+ createdAt: z.coerce.date(),
75
+ updatedAt: z.coerce.date(),
76
+ });
77
+ /**
78
+ * Base entity schema with ID and timestamps
79
+ */
80
+ export const baseEntitySchema = z.object({
81
+ id: uuidSchema,
82
+ createdAt: z.coerce.date(),
83
+ updatedAt: z.coerce.date(),
84
+ });
85
+ // ============================================================================
86
+ // Schema Composition Utilities
87
+ // ============================================================================
88
+ /**
89
+ * Makes all fields in a schema optional
90
+ *
91
+ * @param schema - Zod object schema
92
+ * @returns Schema with all fields optional
93
+ *
94
+ * @example
95
+ * ```typescript
96
+ * const UpdateUserSchema = makePartial(UserSchema);
97
+ * // All fields are now optional
98
+ * ```
99
+ */
100
+ export function makePartial(schema) {
101
+ return schema.partial();
102
+ }
103
+ /**
104
+ * Makes specific fields required in a partial schema
105
+ *
106
+ * @param schema - Zod object schema
107
+ * @param keys - Array of keys to make required
108
+ * @returns Schema with specified fields required, others optional
109
+ *
110
+ * @example
111
+ * ```typescript
112
+ * const UpdateUserSchema = partialExcept(UserSchema, ['id'] as const);
113
+ * // id is required, all other fields are optional
114
+ * ```
115
+ */
116
+ export function partialExcept(schema, keys) {
117
+ const shape = schema.shape;
118
+ const requiredKeys = new Set(keys);
119
+ const newShape = {};
120
+ for (const key in shape) {
121
+ if (requiredKeys.has(key)) {
122
+ newShape[key] = shape[key];
123
+ }
124
+ else {
125
+ newShape[key] = shape[key].optional();
126
+ }
127
+ }
128
+ return z.object(newShape);
129
+ }
130
+ /**
131
+ * Omits specified fields from a schema
132
+ *
133
+ * @param schema - Zod object schema
134
+ * @param keys - Array of keys to omit
135
+ * @returns Schema without the specified fields
136
+ *
137
+ * @example
138
+ * ```typescript
139
+ * const UserWithoutPassword = omitFields(UserSchema, ['password'] as const);
140
+ * ```
141
+ */
142
+ export function omitFields(schema, keys) {
143
+ // Build the omit mask manually to avoid Zod's strict typing
144
+ const shape = schema.shape;
145
+ const keysToOmit = new Set(keys);
146
+ const newShape = {};
147
+ for (const key in shape) {
148
+ if (!keysToOmit.has(key)) {
149
+ newShape[key] = shape[key];
150
+ }
151
+ }
152
+ return z.object(newShape);
153
+ }
154
+ /**
155
+ * Picks specified fields from a schema
156
+ *
157
+ * @param schema - Zod object schema
158
+ * @param keys - Array of keys to pick
159
+ * @returns Schema with only the specified fields
160
+ *
161
+ * @example
162
+ * ```typescript
163
+ * const UserIdAndName = pickFields(UserSchema, ['id', 'name'] as const);
164
+ * ```
165
+ */
166
+ export function pickFields(schema, keys) {
167
+ // Build the pick mask manually to avoid Zod's strict typing
168
+ const shape = schema.shape;
169
+ const newShape = {};
170
+ for (const key of keys) {
171
+ if (key in shape) {
172
+ newShape[key] = shape[key];
173
+ }
174
+ }
175
+ return z.object(newShape);
176
+ }
177
+ // ============================================================================
178
+ // Coercion Utilities
179
+ // ============================================================================
180
+ /**
181
+ * String that coerces to boolean
182
+ * Accepts 'true', '1', 'yes' as true; 'false', '0', 'no' as false
183
+ */
184
+ export const booleanStringSchema = z
185
+ .string()
186
+ .transform((val) => {
187
+ const lower = val.toLowerCase();
188
+ if (['true', '1', 'yes'].includes(lower))
189
+ return true;
190
+ if (['false', '0', 'no'].includes(lower))
191
+ return false;
192
+ return undefined;
193
+ })
194
+ .pipe(z.boolean());
195
+ /**
196
+ * String that coerces to number
197
+ * Useful for query parameters
198
+ */
199
+ export const numberStringSchema = z
200
+ .string()
201
+ .transform((val) => Number(val))
202
+ .pipe(z.number());
203
+ /**
204
+ * String that coerces to integer
205
+ * Useful for pagination parameters
206
+ */
207
+ export const integerStringSchema = z
208
+ .string()
209
+ .transform((val) => parseInt(val, 10))
210
+ .pipe(z.number().int());
211
+ //# sourceMappingURL=common.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"common.js","sourceRoot":"","sources":["../../src/schemas/common.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,+EAA+E;AAC/E,wBAAwB;AACxB,+EAA+E;AAE/E;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,C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AAO,SAAS,CAAC;AACnB,CAAC,CAAC;KACD,IAAI,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC;AAErB;;;GAGG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC;KAChC,MAAM,EAAE;KACR,SAAS,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;KAC/B,IAAI,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC;AAEpB;;;GAGG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC;KACjC,MAAM,EAAE;KACR,SAAS,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,QAAQ,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;KACrC,IAAI,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,CAAC,CAAC"}
@@ -0,0 +1,287 @@
1
+ /**
2
+ * Pagination schema utilities
3
+ *
4
+ * Provides standardized pagination input/output schemas for list endpoints.
5
+ *
6
+ * @module schemas/pagination
7
+ */
8
+ import { z } from 'zod';
9
+ /**
10
+ * Default pagination configuration
11
+ */
12
+ export declare const PAGINATION_DEFAULTS: {
13
+ /** Default page number */
14
+ readonly page: 1;
15
+ /** Default items per page */
16
+ readonly limit: 20;
17
+ /** Maximum allowed items per page */
18
+ readonly maxLimit: 100;
19
+ };
20
+ /**
21
+ * Creates a pagination input schema with configurable defaults
22
+ *
23
+ * @param options - Pagination configuration options
24
+ * @returns Zod schema for pagination input
25
+ *
26
+ * @example
27
+ * ```typescript
28
+ * // Use defaults
29
+ * const PaginationSchema = createPaginationSchema();
30
+ *
31
+ * // Custom configuration
32
+ * const CustomPaginationSchema = createPaginationSchema({
33
+ * defaultLimit: 10,
34
+ * maxLimit: 50,
35
+ * });
36
+ * ```
37
+ */
38
+ export declare function createPaginationSchema(options?: {
39
+ defaultPage?: number;
40
+ defaultLimit?: number;
41
+ maxLimit?: number;
42
+ }): z.ZodObject<{
43
+ /** Current page number (1-indexed) */
44
+ page: z.ZodDefault<z.ZodNumber>;
45
+ /** Number of items per page */
46
+ limit: z.ZodDefault<z.ZodNumber>;
47
+ }, "strip", z.ZodTypeAny, {
48
+ page: number;
49
+ limit: number;
50
+ }, {
51
+ page?: number | undefined;
52
+ limit?: number | undefined;
53
+ }>;
54
+ /**
55
+ * Default pagination input schema
56
+ *
57
+ * Accepts page (default 1) and limit (default 20, max 100)
58
+ */
59
+ export declare const paginationInputSchema: z.ZodObject<{
60
+ /** Current page number (1-indexed) */
61
+ page: z.ZodDefault<z.ZodNumber>;
62
+ /** Number of items per page */
63
+ limit: z.ZodDefault<z.ZodNumber>;
64
+ }, "strip", z.ZodTypeAny, {
65
+ page: number;
66
+ limit: number;
67
+ }, {
68
+ page?: number | undefined;
69
+ limit?: number | undefined;
70
+ }>;
71
+ /**
72
+ * Type for pagination input
73
+ */
74
+ export type PaginationInput = z.infer<typeof paginationInputSchema>;
75
+ /**
76
+ * Pagination with cursor-based navigation
77
+ *
78
+ * For more efficient pagination of large datasets
79
+ */
80
+ export declare const cursorPaginationSchema: z.ZodObject<{
81
+ /** Cursor for the current position */
82
+ cursor: z.ZodOptional<z.ZodString>;
83
+ /** Number of items to fetch */
84
+ limit: z.ZodDefault<z.ZodNumber>;
85
+ /** Direction to fetch (forward/backward from cursor) */
86
+ direction: z.ZodDefault<z.ZodEnum<["forward", "backward"]>>;
87
+ }, "strip", z.ZodTypeAny, {
88
+ limit: number;
89
+ direction: "forward" | "backward";
90
+ cursor?: string | undefined;
91
+ }, {
92
+ limit?: number | undefined;
93
+ cursor?: string | undefined;
94
+ direction?: "forward" | "backward" | undefined;
95
+ }>;
96
+ /**
97
+ * Type for cursor-based pagination input
98
+ */
99
+ export type CursorPaginationInput = z.infer<typeof cursorPaginationSchema>;
100
+ /**
101
+ * Creates a paginated response schema for a given item schema
102
+ *
103
+ * @param itemSchema - Zod schema for individual items
104
+ * @returns Zod schema for paginated response
105
+ *
106
+ * @example
107
+ * ```typescript
108
+ * const UserSchema = z.object({ id: z.string(), name: z.string() });
109
+ * const PaginatedUsersSchema = createPaginatedResponseSchema(UserSchema);
110
+ *
111
+ * type PaginatedUsers = z.infer<typeof PaginatedUsersSchema>;
112
+ * // { data: User[]; meta: { page, limit, total, totalPages, hasMore } }
113
+ * ```
114
+ */
115
+ export declare function createPaginatedResponseSchema<T extends z.ZodTypeAny>(itemSchema: T): z.ZodObject<{
116
+ /** Array of items for the current page */
117
+ data: z.ZodArray<T, "many">;
118
+ /** Pagination metadata */
119
+ meta: z.ZodObject<{
120
+ /** Current page number */
121
+ page: z.ZodNumber;
122
+ /** Items per page */
123
+ limit: z.ZodNumber;
124
+ /** Total number of items across all pages */
125
+ total: z.ZodNumber;
126
+ /** Total number of pages */
127
+ totalPages: z.ZodNumber;
128
+ /** Whether there are more pages after this one */
129
+ hasMore: z.ZodBoolean;
130
+ }, "strip", z.ZodTypeAny, {
131
+ page: number;
132
+ limit: number;
133
+ total: number;
134
+ totalPages: number;
135
+ hasMore: boolean;
136
+ }, {
137
+ page: number;
138
+ limit: number;
139
+ total: number;
140
+ totalPages: number;
141
+ hasMore: boolean;
142
+ }>;
143
+ }, "strip", z.ZodTypeAny, {
144
+ data: T["_output"][];
145
+ meta: {
146
+ page: number;
147
+ limit: number;
148
+ total: number;
149
+ totalPages: number;
150
+ hasMore: boolean;
151
+ };
152
+ }, {
153
+ data: T["_input"][];
154
+ meta: {
155
+ page: number;
156
+ limit: number;
157
+ total: number;
158
+ totalPages: number;
159
+ hasMore: boolean;
160
+ };
161
+ }>;
162
+ /**
163
+ * Type helper to infer paginated response type from item schema
164
+ */
165
+ export type PaginatedResponse<T> = {
166
+ data: T[];
167
+ meta: PaginationMeta;
168
+ };
169
+ /**
170
+ * Pagination metadata type
171
+ */
172
+ export interface PaginationMeta {
173
+ page: number;
174
+ limit: number;
175
+ total: number;
176
+ totalPages: number;
177
+ hasMore: boolean;
178
+ }
179
+ /**
180
+ * Creates a cursor-based paginated response schema
181
+ *
182
+ * @param itemSchema - Zod schema for individual items
183
+ * @returns Zod schema for cursor-paginated response
184
+ */
185
+ export declare function createCursorPaginatedResponseSchema<T extends z.ZodTypeAny>(itemSchema: T): z.ZodObject<{
186
+ /** Array of items */
187
+ data: z.ZodArray<T, "many">;
188
+ /** Cursor pagination metadata */
189
+ meta: z.ZodObject<{
190
+ /** Cursor for the next page (null if no more) */
191
+ nextCursor: z.ZodNullable<z.ZodString>;
192
+ /** Cursor for the previous page (null if at start) */
193
+ prevCursor: z.ZodNullable<z.ZodString>;
194
+ /** Whether there are more items after this page */
195
+ hasMore: z.ZodBoolean;
196
+ }, "strip", z.ZodTypeAny, {
197
+ hasMore: boolean;
198
+ nextCursor: string | null;
199
+ prevCursor: string | null;
200
+ }, {
201
+ hasMore: boolean;
202
+ nextCursor: string | null;
203
+ prevCursor: string | null;
204
+ }>;
205
+ }, "strip", z.ZodTypeAny, {
206
+ data: T["_output"][];
207
+ meta: {
208
+ hasMore: boolean;
209
+ nextCursor: string | null;
210
+ prevCursor: string | null;
211
+ };
212
+ }, {
213
+ data: T["_input"][];
214
+ meta: {
215
+ hasMore: boolean;
216
+ nextCursor: string | null;
217
+ prevCursor: string | null;
218
+ };
219
+ }>;
220
+ /**
221
+ * Type for cursor-paginated response
222
+ */
223
+ export type CursorPaginatedResponse<T> = {
224
+ data: T[];
225
+ meta: CursorPaginationMeta;
226
+ };
227
+ /**
228
+ * Cursor pagination metadata type
229
+ */
230
+ export interface CursorPaginationMeta {
231
+ nextCursor: string | null;
232
+ prevCursor: string | null;
233
+ hasMore: boolean;
234
+ }
235
+ /**
236
+ * Calculates pagination metadata from total count
237
+ *
238
+ * @param options - Pagination calculation options
239
+ * @returns Pagination metadata
240
+ *
241
+ * @example
242
+ * ```typescript
243
+ * const meta = calculatePaginationMeta({
244
+ * page: 2,
245
+ * limit: 20,
246
+ * total: 55,
247
+ * });
248
+ * // { page: 2, limit: 20, total: 55, totalPages: 3, hasMore: true }
249
+ * ```
250
+ */
251
+ export declare function calculatePaginationMeta(options: {
252
+ page: number;
253
+ limit: number;
254
+ total: number;
255
+ }): PaginationMeta;
256
+ /**
257
+ * Calculates offset for database queries
258
+ *
259
+ * @param page - Current page number (1-indexed)
260
+ * @param limit - Items per page
261
+ * @returns Offset for database skip
262
+ *
263
+ * @example
264
+ * ```typescript
265
+ * const offset = calculateOffset(3, 20);
266
+ * // offset = 40 (skip first 40 items for page 3)
267
+ * ```
268
+ */
269
+ export declare function calculateOffset(page: number, limit: number): number;
270
+ /**
271
+ * Creates a paginated response from items and total count
272
+ *
273
+ * @param items - Array of items for current page
274
+ * @param pagination - Pagination input parameters
275
+ * @param total - Total count of items
276
+ * @returns Paginated response object
277
+ *
278
+ * @example
279
+ * ```typescript
280
+ * const users = await db.user.findMany({ skip: 20, take: 20 });
281
+ * const total = await db.user.count();
282
+ *
283
+ * return createPaginatedResponse(users, { page: 2, limit: 20 }, total);
284
+ * ```
285
+ */
286
+ export declare function createPaginatedResponse<T>(items: T[], pagination: PaginationInput, total: number): PaginatedResponse<T>;
287
+ //# sourceMappingURL=pagination.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pagination.d.ts","sourceRoot":"","sources":["../../src/schemas/pagination.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAMxB;;GAEG;AACH,eAAO,MAAM,mBAAmB;IAC9B,0BAA0B;;IAE1B,6BAA6B;;IAE7B,qCAAqC;;CAE7B,CAAC;AAMX;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,sBAAsB,CACpC,OAAO,GAAE;IAAE,WAAW,CAAC,EAAE,MAAM,CAAC;IAAC,YAAY,CAAC,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;CAAO;IAS9E,sCAAsC;;IAEtC,+BAA+B;;;;;;;;GAGlC;AAED;;;;GAIG;AACH,eAAO,MAAM,qBAAqB;IAZ9B,sC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