@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.
- package/dist/index.d.ts +35 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +36 -0
- package/dist/index.js.map +1 -0
- package/dist/middleware.d.ts +174 -0
- package/dist/middleware.d.ts.map +1 -0
- package/dist/middleware.js +274 -0
- package/dist/middleware.js.map +1 -0
- package/dist/schemas/common.d.ts +168 -0
- package/dist/schemas/common.d.ts.map +1 -0
- package/dist/schemas/common.js +211 -0
- package/dist/schemas/common.js.map +1 -0
- package/dist/schemas/pagination.d.ts +287 -0
- package/dist/schemas/pagination.d.ts.map +1 -0
- package/dist/schemas/pagination.js +202 -0
- package/dist/schemas/pagination.js.map +1 -0
- package/dist/types.d.ts +159 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +78 -0
- package/dist/types.js.map +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1,202 @@
|
|
|
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
|
+
// Pagination Constants
|
|
11
|
+
// ============================================================================
|
|
12
|
+
/**
|
|
13
|
+
* Default pagination configuration
|
|
14
|
+
*/
|
|
15
|
+
export const PAGINATION_DEFAULTS = {
|
|
16
|
+
/** Default page number */
|
|
17
|
+
page: 1,
|
|
18
|
+
/** Default items per page */
|
|
19
|
+
limit: 20,
|
|
20
|
+
/** Maximum allowed items per page */
|
|
21
|
+
maxLimit: 100,
|
|
22
|
+
};
|
|
23
|
+
// ============================================================================
|
|
24
|
+
// Pagination Input Schemas
|
|
25
|
+
// ============================================================================
|
|
26
|
+
/**
|
|
27
|
+
* Creates a pagination input schema with configurable defaults
|
|
28
|
+
*
|
|
29
|
+
* @param options - Pagination configuration options
|
|
30
|
+
* @returns Zod schema for pagination input
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* // Use defaults
|
|
35
|
+
* const PaginationSchema = createPaginationSchema();
|
|
36
|
+
*
|
|
37
|
+
* // Custom configuration
|
|
38
|
+
* const CustomPaginationSchema = createPaginationSchema({
|
|
39
|
+
* defaultLimit: 10,
|
|
40
|
+
* maxLimit: 50,
|
|
41
|
+
* });
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export function createPaginationSchema(options = {}) {
|
|
45
|
+
const { defaultPage = PAGINATION_DEFAULTS.page, defaultLimit = PAGINATION_DEFAULTS.limit, maxLimit = PAGINATION_DEFAULTS.maxLimit, } = options;
|
|
46
|
+
return z.object({
|
|
47
|
+
/** Current page number (1-indexed) */
|
|
48
|
+
page: z.coerce.number().int().positive().default(defaultPage),
|
|
49
|
+
/** Number of items per page */
|
|
50
|
+
limit: z.coerce.number().int().positive().max(maxLimit).default(defaultLimit),
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Default pagination input schema
|
|
55
|
+
*
|
|
56
|
+
* Accepts page (default 1) and limit (default 20, max 100)
|
|
57
|
+
*/
|
|
58
|
+
export const paginationInputSchema = createPaginationSchema();
|
|
59
|
+
/**
|
|
60
|
+
* Pagination with cursor-based navigation
|
|
61
|
+
*
|
|
62
|
+
* For more efficient pagination of large datasets
|
|
63
|
+
*/
|
|
64
|
+
export const cursorPaginationSchema = z.object({
|
|
65
|
+
/** Cursor for the current position */
|
|
66
|
+
cursor: z.string().optional(),
|
|
67
|
+
/** Number of items to fetch */
|
|
68
|
+
limit: z.coerce.number().int().positive().max(100).default(20),
|
|
69
|
+
/** Direction to fetch (forward/backward from cursor) */
|
|
70
|
+
direction: z.enum(['forward', 'backward']).default('forward'),
|
|
71
|
+
});
|
|
72
|
+
// ============================================================================
|
|
73
|
+
// Pagination Output Types
|
|
74
|
+
// ============================================================================
|
|
75
|
+
/**
|
|
76
|
+
* Creates a paginated response schema for a given item schema
|
|
77
|
+
*
|
|
78
|
+
* @param itemSchema - Zod schema for individual items
|
|
79
|
+
* @returns Zod schema for paginated response
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```typescript
|
|
83
|
+
* const UserSchema = z.object({ id: z.string(), name: z.string() });
|
|
84
|
+
* const PaginatedUsersSchema = createPaginatedResponseSchema(UserSchema);
|
|
85
|
+
*
|
|
86
|
+
* type PaginatedUsers = z.infer<typeof PaginatedUsersSchema>;
|
|
87
|
+
* // { data: User[]; meta: { page, limit, total, totalPages, hasMore } }
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
export function createPaginatedResponseSchema(itemSchema) {
|
|
91
|
+
return z.object({
|
|
92
|
+
/** Array of items for the current page */
|
|
93
|
+
data: z.array(itemSchema),
|
|
94
|
+
/** Pagination metadata */
|
|
95
|
+
meta: z.object({
|
|
96
|
+
/** Current page number */
|
|
97
|
+
page: z.number().int().positive(),
|
|
98
|
+
/** Items per page */
|
|
99
|
+
limit: z.number().int().positive(),
|
|
100
|
+
/** Total number of items across all pages */
|
|
101
|
+
total: z.number().int().nonnegative(),
|
|
102
|
+
/** Total number of pages */
|
|
103
|
+
totalPages: z.number().int().nonnegative(),
|
|
104
|
+
/** Whether there are more pages after this one */
|
|
105
|
+
hasMore: z.boolean(),
|
|
106
|
+
}),
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Creates a cursor-based paginated response schema
|
|
111
|
+
*
|
|
112
|
+
* @param itemSchema - Zod schema for individual items
|
|
113
|
+
* @returns Zod schema for cursor-paginated response
|
|
114
|
+
*/
|
|
115
|
+
export function createCursorPaginatedResponseSchema(itemSchema) {
|
|
116
|
+
return z.object({
|
|
117
|
+
/** Array of items */
|
|
118
|
+
data: z.array(itemSchema),
|
|
119
|
+
/** Cursor pagination metadata */
|
|
120
|
+
meta: z.object({
|
|
121
|
+
/** Cursor for the next page (null if no more) */
|
|
122
|
+
nextCursor: z.string().nullable(),
|
|
123
|
+
/** Cursor for the previous page (null if at start) */
|
|
124
|
+
prevCursor: z.string().nullable(),
|
|
125
|
+
/** Whether there are more items after this page */
|
|
126
|
+
hasMore: z.boolean(),
|
|
127
|
+
}),
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
// ============================================================================
|
|
131
|
+
// Pagination Utilities
|
|
132
|
+
// ============================================================================
|
|
133
|
+
/**
|
|
134
|
+
* Calculates pagination metadata from total count
|
|
135
|
+
*
|
|
136
|
+
* @param options - Pagination calculation options
|
|
137
|
+
* @returns Pagination metadata
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
* ```typescript
|
|
141
|
+
* const meta = calculatePaginationMeta({
|
|
142
|
+
* page: 2,
|
|
143
|
+
* limit: 20,
|
|
144
|
+
* total: 55,
|
|
145
|
+
* });
|
|
146
|
+
* // { page: 2, limit: 20, total: 55, totalPages: 3, hasMore: true }
|
|
147
|
+
* ```
|
|
148
|
+
*/
|
|
149
|
+
export function calculatePaginationMeta(options) {
|
|
150
|
+
const { page, limit, total } = options;
|
|
151
|
+
const totalPages = Math.ceil(total / limit);
|
|
152
|
+
return {
|
|
153
|
+
page,
|
|
154
|
+
limit,
|
|
155
|
+
total,
|
|
156
|
+
totalPages,
|
|
157
|
+
hasMore: page < totalPages,
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Calculates offset for database queries
|
|
162
|
+
*
|
|
163
|
+
* @param page - Current page number (1-indexed)
|
|
164
|
+
* @param limit - Items per page
|
|
165
|
+
* @returns Offset for database skip
|
|
166
|
+
*
|
|
167
|
+
* @example
|
|
168
|
+
* ```typescript
|
|
169
|
+
* const offset = calculateOffset(3, 20);
|
|
170
|
+
* // offset = 40 (skip first 40 items for page 3)
|
|
171
|
+
* ```
|
|
172
|
+
*/
|
|
173
|
+
export function calculateOffset(page, limit) {
|
|
174
|
+
return (page - 1) * limit;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Creates a paginated response from items and total count
|
|
178
|
+
*
|
|
179
|
+
* @param items - Array of items for current page
|
|
180
|
+
* @param pagination - Pagination input parameters
|
|
181
|
+
* @param total - Total count of items
|
|
182
|
+
* @returns Paginated response object
|
|
183
|
+
*
|
|
184
|
+
* @example
|
|
185
|
+
* ```typescript
|
|
186
|
+
* const users = await db.user.findMany({ skip: 20, take: 20 });
|
|
187
|
+
* const total = await db.user.count();
|
|
188
|
+
*
|
|
189
|
+
* return createPaginatedResponse(users, { page: 2, limit: 20 }, total);
|
|
190
|
+
* ```
|
|
191
|
+
*/
|
|
192
|
+
export function createPaginatedResponse(items, pagination, total) {
|
|
193
|
+
return {
|
|
194
|
+
data: items,
|
|
195
|
+
meta: calculatePaginationMeta({
|
|
196
|
+
page: pagination.page,
|
|
197
|
+
limit: pagination.limit,
|
|
198
|
+
total,
|
|
199
|
+
}),
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
//# sourceMappingURL=pagination.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pagination.js","sourceRoot":"","sources":["../../src/schemas/pagination.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,+EAA+E;AAC/E,uBAAuB;AACvB,+EAA+E;AAE/E;;GAEG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG;IACjC,0BAA0B;IAC1B,IAAI,EAAE,CAAC;IACP,6BAA6B;IAC7B,KAAK,EAAE,EAAE;IACT,qCAAqC;IACrC,QAAQ,EAAE,GAAG;CACL,CAAC;AAEX,+EAA+E;AAC/E,2BAA2B;AAC3B,+EAA+E;AAE/E;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,UAAU,sBAAsB,CACpC,UAA8E,EAAE;IAEhF,MAAM,EAC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,GAAG,IAAI,CAAC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC,CAAC;IAE5C,OAAO;QACL,IAAI;QACJ,KAAK;QACL,KAAK;QACL,UAAU;QACV,OAAO,EAAE,IAAI,GAAG,UAAU;KAC3B,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,eAAe,CAAC,IAAY,EAAE,KAAa;IACzD,OAAO,CAAC,IAAI,GAAG,CAAC,CAAC,GAAG,KAAK,CAAC;AAC5B,CAAC;AAED;;;;;;;;;;;;;;;GAeG;AACH,MAAM,UAAU,uBAAuB,CACrC,KAAU,EACV,UAA2B,EAC3B,KAAa;IAEb,OAAO;QACL,IAAI,EAAE,KAAK;QACX,IAAI,EAAE,uBAAuB,CAAC;YAC5B,IAAI,EAAE,UAAU,CAAC,IAAI;YACrB,KAAK,EAAE,UAAU,CAAC,KAAK;YACvB,KAAK;SACN,CAAC;KACH,CAAC;AACJ,CAAC"}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core type definitions for @veloxts/validation
|
|
3
|
+
*
|
|
4
|
+
* Provides type-safe Zod integration with automatic type inference
|
|
5
|
+
* for procedure inputs/outputs.
|
|
6
|
+
*
|
|
7
|
+
* @module types
|
|
8
|
+
*/
|
|
9
|
+
import type { ZodType, ZodTypeDef } from 'zod';
|
|
10
|
+
/**
|
|
11
|
+
* Type-safe validator interface that wraps Zod schemas
|
|
12
|
+
*
|
|
13
|
+
* This abstraction allows the framework to work with Zod while maintaining
|
|
14
|
+
* the flexibility to support other validation libraries in the future.
|
|
15
|
+
*
|
|
16
|
+
* @template TOutput - The validated output type
|
|
17
|
+
* @template TInput - The raw input type (defaults to TOutput)
|
|
18
|
+
*/
|
|
19
|
+
export interface Schema<TOutput = unknown, _TInput = TOutput> {
|
|
20
|
+
/**
|
|
21
|
+
* Parses input and returns validated data
|
|
22
|
+
* Throws ZodError on validation failure
|
|
23
|
+
*/
|
|
24
|
+
readonly parse: (input: unknown) => TOutput;
|
|
25
|
+
/**
|
|
26
|
+
* Safely parses input without throwing
|
|
27
|
+
* Returns discriminated union with success/error
|
|
28
|
+
*/
|
|
29
|
+
readonly safeParse: (input: unknown) => SafeParseResult<TOutput>;
|
|
30
|
+
/**
|
|
31
|
+
* Brand to identify Schema instances
|
|
32
|
+
* @internal
|
|
33
|
+
*/
|
|
34
|
+
readonly _schema: true;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Result type for safe parsing operations
|
|
38
|
+
*/
|
|
39
|
+
export type SafeParseResult<T> = SafeParseSuccess<T> | SafeParseError;
|
|
40
|
+
/**
|
|
41
|
+
* Successful parse result
|
|
42
|
+
*/
|
|
43
|
+
export interface SafeParseSuccess<T> {
|
|
44
|
+
readonly success: true;
|
|
45
|
+
readonly data: T;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Failed parse result with error details
|
|
49
|
+
*/
|
|
50
|
+
export interface SafeParseError {
|
|
51
|
+
readonly success: false;
|
|
52
|
+
readonly error: ValidationIssue[];
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Individual validation issue
|
|
56
|
+
*/
|
|
57
|
+
export interface ValidationIssue {
|
|
58
|
+
/** Dot-notation path to the invalid field */
|
|
59
|
+
readonly path: readonly (string | number)[];
|
|
60
|
+
/** Human-readable error message */
|
|
61
|
+
readonly message: string;
|
|
62
|
+
/** Zod error code */
|
|
63
|
+
readonly code: string;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Infers the output type from a Schema or ZodType
|
|
67
|
+
*
|
|
68
|
+
* Works with both wrapped Schema instances and raw Zod schemas,
|
|
69
|
+
* enabling seamless type inference in procedure chains.
|
|
70
|
+
*
|
|
71
|
+
* @template T - Schema or ZodType to infer from
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```typescript
|
|
75
|
+
* const UserSchema = z.object({ id: z.string(), name: z.string() });
|
|
76
|
+
* type User = InferOutput<typeof UserSchema>;
|
|
77
|
+
* // User = { id: string; name: string }
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
export type InferOutput<T> = T extends Schema<infer O, infer _I> ? O : T extends ZodType<infer O, ZodTypeDef, infer _I> ? O : never;
|
|
81
|
+
/**
|
|
82
|
+
* Infers the input type from a Schema or ZodType
|
|
83
|
+
*
|
|
84
|
+
* For schemas with transforms, this returns the pre-transform type.
|
|
85
|
+
*
|
|
86
|
+
* @template T - Schema or ZodType to infer from
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* ```typescript
|
|
90
|
+
* const DateSchema = z.string().transform((s) => new Date(s));
|
|
91
|
+
* type DateInput = InferInput<typeof DateSchema>;
|
|
92
|
+
* // DateInput = string (not Date)
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
export type InferInput<T> = T extends Schema<infer _O, infer I> ? I : T extends ZodType<infer _O, ZodTypeDef, infer I> ? I : never;
|
|
96
|
+
/**
|
|
97
|
+
* Type constraint for any Zod schema
|
|
98
|
+
*
|
|
99
|
+
* Used in generic constraints to accept any valid Zod schema.
|
|
100
|
+
*/
|
|
101
|
+
export type AnyZodSchema = ZodType<unknown, ZodTypeDef, unknown>;
|
|
102
|
+
/**
|
|
103
|
+
* Type constraint for any Schema wrapper
|
|
104
|
+
*/
|
|
105
|
+
export type AnySchema = Schema<unknown, unknown>;
|
|
106
|
+
/**
|
|
107
|
+
* Union type accepting both raw Zod schemas and wrapped Schema instances
|
|
108
|
+
*/
|
|
109
|
+
export type SchemaLike = AnyZodSchema | AnySchema;
|
|
110
|
+
/**
|
|
111
|
+
* Type guard to check if a value is a Schema instance
|
|
112
|
+
*/
|
|
113
|
+
export declare function isSchema(value: unknown): value is AnySchema;
|
|
114
|
+
/**
|
|
115
|
+
* Type guard to check if a value is a Zod schema
|
|
116
|
+
*/
|
|
117
|
+
export declare function isZodSchema(value: unknown): value is AnyZodSchema;
|
|
118
|
+
/**
|
|
119
|
+
* Wraps a Zod schema in the framework's Schema interface
|
|
120
|
+
*
|
|
121
|
+
* This provides a consistent API and transforms Zod errors into
|
|
122
|
+
* a framework-friendly format.
|
|
123
|
+
*
|
|
124
|
+
* @template TOutput - The validated output type
|
|
125
|
+
* @template TInput - The raw input type
|
|
126
|
+
* @param zodSchema - The Zod schema to wrap
|
|
127
|
+
* @returns Wrapped Schema instance
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```typescript
|
|
131
|
+
* import { z } from 'zod';
|
|
132
|
+
* import { wrapSchema } from '@veloxts/validation';
|
|
133
|
+
*
|
|
134
|
+
* const UserSchema = wrapSchema(z.object({
|
|
135
|
+
* id: z.string().uuid(),
|
|
136
|
+
* name: z.string().min(1),
|
|
137
|
+
* }));
|
|
138
|
+
* ```
|
|
139
|
+
*/
|
|
140
|
+
export declare function wrapSchema<TOutput, TInput = TOutput>(zodSchema: ZodType<TOutput, ZodTypeDef, TInput>): Schema<TOutput, TInput>;
|
|
141
|
+
/**
|
|
142
|
+
* Represents no input (undefined) for procedures without input schemas
|
|
143
|
+
*/
|
|
144
|
+
export type NoInput = undefined;
|
|
145
|
+
/**
|
|
146
|
+
* Represents unknown output for procedures without output schemas
|
|
147
|
+
*/
|
|
148
|
+
export type UnknownOutput = unknown;
|
|
149
|
+
/**
|
|
150
|
+
* Utility type to resolve the effective input type
|
|
151
|
+
* Returns NoInput if undefined, otherwise the inferred type
|
|
152
|
+
*/
|
|
153
|
+
export type ResolveInput<T> = T extends undefined ? NoInput : InferOutput<T>;
|
|
154
|
+
/**
|
|
155
|
+
* Utility type to resolve the effective output type
|
|
156
|
+
* Returns UnknownOutput if undefined, otherwise the inferred type
|
|
157
|
+
*/
|
|
158
|
+
export type ResolveOutput<T> = T extends undefined ? UnknownOutput : InferOutput<T>;
|
|
159
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","s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C,CAAC;AAE7E;;;GAGG;AACH,MAAM,MAAM,aAAa,CAAC,CAAC,IAAI,CAAC,SAAS,SAAS,GAAG,aAAa,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core type definitions for @veloxts/validation
|
|
3
|
+
*
|
|
4
|
+
* Provides type-safe Zod integration with automatic type inference
|
|
5
|
+
* for procedure inputs/outputs.
|
|
6
|
+
*
|
|
7
|
+
* @module types
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Type guard to check if a value is a Schema instance
|
|
11
|
+
*/
|
|
12
|
+
export function isSchema(value) {
|
|
13
|
+
return (typeof value === 'object' &&
|
|
14
|
+
value !== null &&
|
|
15
|
+
'_schema' in value &&
|
|
16
|
+
value._schema === true);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Type guard to check if a value is a Zod schema
|
|
20
|
+
*/
|
|
21
|
+
export function isZodSchema(value) {
|
|
22
|
+
return (typeof value === 'object' &&
|
|
23
|
+
value !== null &&
|
|
24
|
+
'parse' in value &&
|
|
25
|
+
'safeParse' in value &&
|
|
26
|
+
'_def' in value);
|
|
27
|
+
}
|
|
28
|
+
// ============================================================================
|
|
29
|
+
// Schema Wrapper Factory
|
|
30
|
+
// ============================================================================
|
|
31
|
+
/**
|
|
32
|
+
* Wraps a Zod schema in the framework's Schema interface
|
|
33
|
+
*
|
|
34
|
+
* This provides a consistent API and transforms Zod errors into
|
|
35
|
+
* a framework-friendly format.
|
|
36
|
+
*
|
|
37
|
+
* @template TOutput - The validated output type
|
|
38
|
+
* @template TInput - The raw input type
|
|
39
|
+
* @param zodSchema - The Zod schema to wrap
|
|
40
|
+
* @returns Wrapped Schema instance
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```typescript
|
|
44
|
+
* import { z } from 'zod';
|
|
45
|
+
* import { wrapSchema } from '@veloxts/validation';
|
|
46
|
+
*
|
|
47
|
+
* const UserSchema = wrapSchema(z.object({
|
|
48
|
+
* id: z.string().uuid(),
|
|
49
|
+
* name: z.string().min(1),
|
|
50
|
+
* }));
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export function wrapSchema(zodSchema) {
|
|
54
|
+
return {
|
|
55
|
+
_schema: true,
|
|
56
|
+
parse(input) {
|
|
57
|
+
return zodSchema.parse(input);
|
|
58
|
+
},
|
|
59
|
+
safeParse(input) {
|
|
60
|
+
const result = zodSchema.safeParse(input);
|
|
61
|
+
if (result.success) {
|
|
62
|
+
return {
|
|
63
|
+
success: true,
|
|
64
|
+
data: result.data,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
success: false,
|
|
69
|
+
error: result.error.issues.map((issue) => ({
|
|
70
|
+
path: issue.path,
|
|
71
|
+
message: issue.message,
|
|
72
|
+
code: issue.code,
|
|
73
|
+
})),
|
|
74
|
+
};
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AA2IH;;GAEG;AACH,MAAM,UAAU,QAAQ,CAAC,KAAc;IACrC,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,SAAS,IAAI,KAAK;QACjB,KAA8B,CAAC,OAAO,KAAK,IAAI,CACjD,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,WAAW,CAAC,KAAc;IACxC,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,OAAO,IAAI,KAAK;QAChB,WAAW,IAAI,KAAK;QACpB,MAAM,IAAI,KAAK,CAChB,CAAC;AACJ,CAAC;AAED,+EAA+E;AAC/E,yBAAyB;AACzB,+EAA+E;AAE/E;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,UAAU,UAAU,CACxB,SAA+C;IAE/C,OAAO;QACL,OAAO,EAAE,IAAa;QAEtB,KAAK,CAAC,KAAc;YAClB,OAAO,SAAS,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QAChC,CAAC;QAED,SAAS,CAAC,KAAc;YACtB,MAAM,MAAM,GAAG,SAAS,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;YAE1C,IAAI,MAAM,CAAC,OAAO,EAAE,CAAC;gBACnB,OAAO;oBACL,OAAO,EAAE,IAAI;oBACb,IAAI,EAAE,MAAM,CAAC,IAAI;iBAClB,CAAC;YACJ,CAAC;YAED,OAAO;gBACL,OAAO,EAAE,KAAK;gBACd,KAAK,EAAE,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;oBACzC,IAAI,EAAE,KAAK,CAAC,IAAI;oBAChB,OAAO,EAAE,KAAK,CAAC,OAAO;oBACtB,IAAI,EAAE,KAAK,CAAC,IAAI;iBACjB,CAAC,CAAC;aACJ,CAAC;QACJ,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@veloxts/validation",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Zod integration and validation middleware for VeloxTS framework",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
},
|
|
14
14
|
"dependencies": {
|
|
15
15
|
"zod": "3.24.4",
|
|
16
|
-
"@veloxts/core": "0.
|
|
16
|
+
"@veloxts/core": "0.3.0"
|
|
17
17
|
},
|
|
18
18
|
"devDependencies": {
|
|
19
19
|
"typescript": "5.9.3",
|