zodvex 0.2.4 → 0.3.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.
package/dist/index.d.mts DELETED
@@ -1,489 +0,0 @@
1
- import { Customization } from 'convex-helpers/server/customFunctions';
2
- export { customCtx } from 'convex-helpers/server/customFunctions';
3
- import * as convex_server from 'convex/server';
4
- import { RegisteredQuery, RegisteredMutation, RegisteredAction, DefaultFunctionArgs, FunctionVisibility, ArgsArrayToObject, GenericDataModel, QueryBuilder, GenericQueryCtx } from 'convex/server';
5
- import * as convex_values from 'convex/values';
6
- import { VAny, VOptional, VUnion, VId, GenericId, VString, VFloat64, VInt64, VBoolean, VNull, VArray, VObject, VLiteral, VRecord, PropertyValidators } from 'convex/values';
7
- import { z } from 'zod';
8
- import * as convex_helpers from 'convex-helpers';
9
-
10
- type IsZid<T> = T extends {
11
- _tableName: infer _TableName extends string;
12
- } ? true : false;
13
- type ExtractTableName<T> = T extends {
14
- _tableName: infer TableName;
15
- } ? TableName : never;
16
- type EnumToLiteralsTuple<T extends readonly [string, ...string[]]> = T['length'] extends 1 ? [VLiteral<T[0], 'required'>] : T['length'] extends 2 ? [VLiteral<T[0], 'required'>, VLiteral<T[1], 'required'>] : [
17
- VLiteral<T[0], 'required'>,
18
- VLiteral<T[1], 'required'>,
19
- ...{
20
- [K in keyof T]: K extends '0' | '1' ? never : K extends keyof T ? VLiteral<T[K], 'required'> : never;
21
- }[keyof T & number][]
22
- ];
23
- type ZodValidator = Record<string, z.ZodTypeAny>;
24
- type ConvexValidatorFromZodRequired<Z extends z.ZodTypeAny> = Z extends z.ZodOptional<infer T extends z.ZodTypeAny> ? VUnion<z.infer<T> | null, any[], 'required'> : ConvexValidatorFromZodBase<Z>;
25
- type ConvexValidatorFromZodBase<Z extends z.ZodTypeAny> = IsZid<Z> extends true ? ExtractTableName<Z> extends infer TableName extends string ? VId<GenericId<TableName>, 'required'> : VAny<'required'> : Z extends z.ZodString ? VString<z.infer<Z>, 'required'> : Z extends z.ZodNumber ? VFloat64<z.infer<Z>, 'required'> : Z extends z.ZodDate ? VFloat64<number, 'required'> : Z extends z.ZodBigInt ? VInt64<z.infer<Z>, 'required'> : Z extends z.ZodBoolean ? VBoolean<z.infer<Z>, 'required'> : Z extends z.ZodNull ? VNull<null, 'required'> : Z extends z.ZodArray<infer T extends z.ZodTypeAny> ? VArray<z.infer<Z>, ConvexValidatorFromZodRequired<T>, 'required'> : Z extends z.ZodObject<infer T> ? VObject<z.infer<Z>, ConvexValidatorFromZodFieldsAuto<T>, 'required', string> : Z extends z.ZodUnion<infer T> ? T extends readonly [z.ZodTypeAny, z.ZodTypeAny, ...z.ZodTypeAny[]] ? VUnion<z.infer<Z>, any[], 'required'> : never : Z extends z.ZodLiteral<infer T> ? VLiteral<T, 'required'> : Z extends z.ZodEnum<infer T> ? T extends readonly [string, ...string[]] ? T['length'] extends 1 ? VLiteral<T[0], 'required'> : T['length'] extends 2 ? VUnion<T[number], [
26
- VLiteral<T[0], 'required'>,
27
- VLiteral<T[1], 'required'>
28
- ], 'required', never> : VUnion<T[number], EnumToLiteralsTuple<T>, 'required', never> : T extends Record<string, string | number> ? VUnion<T[keyof T], Array<VLiteral<T[keyof T], 'required'>>, 'required', never> : VUnion<string, any[], 'required', any> : Z extends z.ZodRecord<z.ZodString, infer V extends z.ZodTypeAny> ? VRecord<Record<string, z.infer<V>>, VString<string, 'required'>, ConvexValidatorFromZodRequired<V>, 'required', string> : Z extends z.ZodNullable<infer Inner extends z.ZodTypeAny> ? Inner extends z.ZodOptional<infer InnerInner extends z.ZodTypeAny> ? VOptional<VUnion<z.infer<InnerInner> | null, [
29
- ConvexValidatorFromZodBase<InnerInner>,
30
- VNull<null, 'required'>
31
- ], 'required'>> : VUnion<z.infer<Inner> | null, [
32
- ConvexValidatorFromZodBase<Inner>,
33
- VNull<null, 'required'>
34
- ], 'required'> : Z extends z.ZodAny ? VAny<'required'> : Z extends z.ZodUnknown ? VAny<'required'> : VAny<'required'>;
35
- type ConvexValidatorFromZod<Z extends z.ZodTypeAny, Constraint extends 'required' | 'optional' = 'required'> = Z extends z.ZodAny ? VAny<'required'> : Z extends z.ZodUnknown ? VAny<'required'> : Z extends z.ZodDefault<infer T extends z.ZodTypeAny> ? ConvexValidatorFromZod<T, Constraint> : Z extends z.ZodOptional<infer T extends z.ZodTypeAny> ? T extends z.ZodNullable<infer Inner extends z.ZodTypeAny> ? VOptional<VUnion<z.infer<Inner> | null, any[], 'required'>> : Constraint extends 'required' ? VUnion<z.infer<T>, any[], 'required'> : VOptional<ConvexValidatorFromZod<T, 'required'>> : Z extends z.ZodNullable<infer T extends z.ZodTypeAny> ? VUnion<z.infer<T> | null, any[], Constraint> : IsZid<Z> extends true ? ExtractTableName<Z> extends infer TableName extends string ? VId<GenericId<TableName>, Constraint> : VAny<'required'> : Z extends z.ZodString ? VString<z.infer<Z>, Constraint> : Z extends z.ZodNumber ? VFloat64<z.infer<Z>, Constraint> : Z extends z.ZodDate ? VFloat64<number, Constraint> : Z extends z.ZodBigInt ? VInt64<z.infer<Z>, Constraint> : Z extends z.ZodBoolean ? VBoolean<z.infer<Z>, Constraint> : Z extends z.ZodNull ? VNull<null, Constraint> : Z extends z.ZodArray<infer T extends z.ZodTypeAny> ? VArray<z.infer<Z>, ConvexValidatorFromZodRequired<T>, Constraint> : Z extends z.ZodObject<infer T> ? VObject<z.infer<Z>, ConvexValidatorFromZodFields<T, 'required'>, Constraint, string> : Z extends z.ZodUnion<infer T> ? T extends readonly [z.ZodTypeAny, z.ZodTypeAny, ...z.ZodTypeAny[]] ? VUnion<z.infer<Z>, any[], Constraint> : never : Z extends z.ZodLiteral<infer T> ? VLiteral<T, Constraint> : Z extends z.ZodEnum<infer T> ? T extends readonly [string, ...string[]] ? T['length'] extends 1 ? VLiteral<T[0], Constraint> : T['length'] extends 2 ? VUnion<T[number], [
36
- VLiteral<T[0], 'required'>,
37
- VLiteral<T[1], 'required'>
38
- ], Constraint, never> : VUnion<T[number], EnumToLiteralsTuple<T>, Constraint, never> : T extends Record<string, string | number> ? VUnion<T[keyof T], Array<VLiteral<T[keyof T], 'required'>>, Constraint, never> : VUnion<string, any[], Constraint, any> : Z extends z.ZodRecord<z.ZodString, infer V extends z.ZodTypeAny> ? VRecord<Record<string, z.infer<V>>, VString<string, 'required'>, ConvexValidatorFromZodRequired<V>, Constraint, string> : VAny<'required'>;
39
- type ConvexValidatorFromZodFields<T extends {
40
- [key: string]: any;
41
- }, Constraint extends 'required' | 'optional' = 'required'> = {
42
- [K in keyof T]: T[K] extends z.ZodTypeAny ? ConvexValidatorFromZod<T[K], Constraint> : VAny<'required'>;
43
- };
44
- type ConvexValidatorFromZodFieldsAuto<T extends {
45
- [key: string]: any;
46
- }> = {
47
- [K in keyof T]: T[K] extends z.ZodOptional<any> ? ConvexValidatorFromZod<T[K], 'optional'> : T[K] extends z.ZodDefault<any> ? ConvexValidatorFromZod<T[K], 'optional'> : T[K] extends z.ZodNullable<any> ? ConvexValidatorFromZod<T[K], 'required'> : T[K] extends z.ZodEnum<any> ? ConvexValidatorFromZod<T[K], 'required'> : T[K] extends z.ZodTypeAny ? ConvexValidatorFromZod<T[K], 'required'> : VAny<'required'>;
48
- };
49
-
50
- declare function zodToConvex<Z extends z.ZodTypeAny | ZodValidator>(zod: Z): Z extends z.ZodTypeAny ? ConvexValidatorFromZod<Z, 'required'> : Z extends ZodValidator ? ConvexValidatorFromZodFieldsAuto<Z> : never;
51
- declare function zodToConvexFields<Z extends z.ZodRawShape>(zod: Z): ConvexValidatorFromZodFieldsAuto<Z>;
52
-
53
- /**
54
- * IDs + registry for Convex + Zod v4
55
- */
56
-
57
- declare const registryHelpers: {
58
- getMetadata: (type: z.ZodTypeAny) => any;
59
- setMetadata: (type: z.ZodTypeAny, meta: any) => WeakMap<z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>, any>;
60
- };
61
- /**
62
- * Create a Zod validator for a Convex Id
63
- *
64
- * Uses the string → transform → brand pattern for proper type narrowing with ctx.db.get()
65
- * This aligns with Zod v4 best practices and matches convex-helpers implementation
66
- */
67
- declare function zid<TableName extends string>(tableName: TableName): z.ZodType<GenericId<TableName>> & {
68
- _tableName: TableName;
69
- };
70
- type Zid<TableName extends string> = ReturnType<typeof zid<TableName>>;
71
-
72
- declare function makeUnion(members: any[]): any;
73
- declare function getObjectShape(obj: any): Record<string, any>;
74
-
75
- type InferArgs<A> = A extends z.ZodObject<infer S> ? z.infer<z.ZodObject<S>> : A extends Record<string, z.ZodTypeAny> ? {
76
- [K in keyof A]: z.infer<A[K]>;
77
- } : A extends z.ZodTypeAny ? z.infer<A> : Record<string, never>;
78
- type InferReturns<R> = R extends z.ZodUnion<any> ? any : R extends z.ZodCustom<any> ? any : R extends z.ZodType<any, any, any> ? z.output<R> : R extends undefined ? any : R;
79
- type InferHandlerReturns<R> = R extends z.ZodUnion<any> ? any : R extends z.ZodCustom<any> ? any : R extends z.ZodType<any, any, any> ? z.input<R> : any;
80
- /**
81
- * Extract the visibility type from a Convex builder function
82
- */
83
- type ExtractVisibility<Builder extends (...args: any) => any> = ReturnType<Builder> extends RegisteredQuery<infer V, any, any> ? V : ReturnType<Builder> extends RegisteredMutation<infer V, any, any> ? V : ReturnType<Builder> extends RegisteredAction<infer V, any, any> ? V : 'public';
84
- /**
85
- * @deprecated Use GenericQueryCtx, GenericMutationCtx, or GenericActionCtx directly instead
86
- */
87
- type ExtractCtx<Builder> = Builder extends {
88
- (fn: {
89
- handler: (ctx: infer Ctx, ...args: any[]) => any;
90
- }): any;
91
- } ? Ctx : never;
92
- /**
93
- * @deprecated Return types are now specified explicitly using RegisteredQuery, RegisteredMutation, or RegisteredAction
94
- */
95
- type PreserveReturnType<Builder extends (...args: any) => any, ArgsType, ReturnsType> = ReturnType<Builder> extends RegisteredQuery<infer V, any, any> ? RegisteredQuery<V, ArgsType extends DefaultFunctionArgs ? ArgsType : DefaultFunctionArgs, Promise<ReturnsType>> : ReturnType<Builder> extends RegisteredMutation<infer V, any, any> ? RegisteredMutation<V, ArgsType extends DefaultFunctionArgs ? ArgsType : DefaultFunctionArgs, Promise<ReturnsType>> : ReturnType<Builder> extends RegisteredAction<infer V, any, any> ? RegisteredAction<V, ArgsType extends DefaultFunctionArgs ? ArgsType : DefaultFunctionArgs, Promise<ReturnsType>> : ReturnType<Builder>;
96
- type ZodToConvexArgs<A> = A extends z.ZodObject<any> ? z.infer<A> : A extends Record<string, z.ZodTypeAny> ? {
97
- [K in keyof A]: z.infer<A[K]>;
98
- } : A extends z.ZodTypeAny ? {
99
- value: z.infer<A>;
100
- } : Record<string, never>;
101
-
102
- type OneArgArray<ArgsObject extends DefaultFunctionArgs = DefaultFunctionArgs> = [ArgsObject];
103
- type NullToUndefinedOrNull<T> = T extends null ? T | undefined | void : T;
104
- type Returns<T> = Promise<NullToUndefinedOrNull<T>> | NullToUndefinedOrNull<T>;
105
- type ReturnValueInput<ReturnsValidator extends z.ZodTypeAny | ZodValidator | void> = [
106
- ReturnsValidator
107
- ] extends [z.ZodTypeAny] ? Returns<z.input<ReturnsValidator>> : [ReturnsValidator] extends [ZodValidator] ? Returns<z.input<z.ZodObject<ReturnsValidator>>> : any;
108
- type ReturnValueOutput<ReturnsValidator extends z.ZodTypeAny | ZodValidator | void> = [
109
- ReturnsValidator
110
- ] extends [z.ZodTypeAny] ? Returns<z.output<ReturnsValidator>> : [ReturnsValidator] extends [ZodValidator] ? Returns<z.output<z.ZodObject<ReturnsValidator>>> : any;
111
- type ArgsInput<ArgsValidator extends ZodValidator | z.ZodObject<any> | void> = [
112
- ArgsValidator
113
- ] extends [z.ZodObject<any>] ? [z.input<ArgsValidator>] : [ArgsValidator] extends [ZodValidator] ? [z.input<z.ZodObject<ArgsValidator>>] : OneArgArray;
114
- type ArgsOutput<ArgsValidator extends ZodValidator | z.ZodObject<any> | void> = [
115
- ArgsValidator
116
- ] extends [z.ZodObject<any>] ? [z.output<ArgsValidator>] : [ArgsValidator] extends [ZodValidator] ? [z.output<z.ZodObject<ArgsValidator>>] : OneArgArray;
117
- type Overwrite<T, U> = Omit<T, keyof U> & U;
118
- type Expand<ObjectType extends Record<any, any>> = ObjectType extends Record<any, any> ? {
119
- [Key in keyof ObjectType]: ObjectType[Key];
120
- } : never;
121
- type ArgsForHandlerType<OneOrZeroArgs extends [] | [Record<string, any>], CustomMadeArgs extends Record<string, any>> = CustomMadeArgs extends Record<string, never> ? OneOrZeroArgs : OneOrZeroArgs extends [infer A] ? [Expand<A & CustomMadeArgs>] : [CustomMadeArgs];
122
- type Registration<FuncType extends 'query' | 'mutation' | 'action', Visibility extends FunctionVisibility, Args extends DefaultFunctionArgs, Output> = FuncType extends 'query' ? convex_server.RegisteredQuery<Visibility, Args, Output> : FuncType extends 'mutation' ? convex_server.RegisteredMutation<Visibility, Args, Output> : convex_server.RegisteredAction<Visibility, Args, Output>;
123
- /**
124
- * A builder that customizes a Convex function, whether or not it validates
125
- * arguments. If the customization requires arguments, however, the resulting
126
- * builder will require argument validation too.
127
- *
128
- * This is our own Zod-aware CustomBuilder type that properly handles Zod validators.
129
- */
130
- type CustomBuilder<FuncType extends 'query' | 'mutation' | 'action', CustomArgsValidator extends PropertyValidators, CustomCtx extends Record<string, any>, CustomMadeArgs extends Record<string, any>, InputCtx, Visibility extends FunctionVisibility, ExtraArgs extends Record<string, any>> = {
131
- <ArgsValidator extends ZodValidator | z.ZodObject<any> | void, ReturnsZodValidator extends z.ZodTypeAny | ZodValidator | void = void, ReturnValue extends ReturnValueInput<ReturnsZodValidator> = any>(func: ({
132
- /**
133
- * Specify the arguments to the function as a Zod validator.
134
- */
135
- args?: ArgsValidator;
136
- handler: (ctx: Overwrite<InputCtx, CustomCtx>, ...args: ArgsForHandlerType<ArgsOutput<ArgsValidator>, CustomMadeArgs>) => ReturnValue;
137
- /**
138
- * Validates the value returned by the function.
139
- * Note: you can't pass an object directly without wrapping it
140
- * in `z.object()`.
141
- */
142
- returns?: ReturnsZodValidator;
143
- /**
144
- * If true, the function will not be validated by Convex,
145
- * in case you're seeing performance issues with validating twice.
146
- */
147
- skipConvexValidation?: boolean;
148
- } & {
149
- [key in keyof ExtraArgs as key extends 'args' | 'handler' | 'skipConvexValidation' | 'returns' ? never : key]: ExtraArgs[key];
150
- }) | {
151
- (ctx: Overwrite<InputCtx, CustomCtx>, ...args: ArgsForHandlerType<ArgsOutput<ArgsValidator>, CustomMadeArgs>): ReturnValue;
152
- }): Registration<FuncType, Visibility, ArgsArrayToObject<CustomArgsValidator extends Record<string, never> ? ArgsInput<ArgsValidator> : ArgsInput<ArgsValidator> extends [infer A] ? [Expand<A & convex_values.ObjectType<CustomArgsValidator>>] : [convex_values.ObjectType<CustomArgsValidator>]>, ReturnsZodValidator extends void ? ReturnValue : ReturnValueOutput<ReturnsZodValidator>>;
153
- };
154
- declare function customFnBuilder<Ctx extends Record<string, any>, Builder extends (fn: any) => any, CustomArgsValidator extends PropertyValidators, CustomCtx extends Record<string, any>, CustomMadeArgs extends Record<string, any>, ExtraArgs extends Record<string, any> = Record<string, any>>(builder: Builder, customization: Customization<Ctx, CustomArgsValidator, CustomCtx, CustomMadeArgs, ExtraArgs>): (fn: any) => any;
155
- declare function zCustomQuery<CustomArgsValidator extends PropertyValidators, CustomCtx extends Record<string, any>, CustomMadeArgs extends Record<string, any>, Visibility extends FunctionVisibility, DataModel extends GenericDataModel, ExtraArgs extends Record<string, any> = Record<string, any>>(query: QueryBuilder<DataModel, Visibility>, customization: Customization<any, CustomArgsValidator, CustomCtx, CustomMadeArgs, ExtraArgs>): CustomBuilder<'query', CustomArgsValidator, CustomCtx, CustomMadeArgs, GenericQueryCtx<DataModel>, Visibility, ExtraArgs>;
156
- declare function zCustomQuery<CustomArgsValidator extends PropertyValidators, CustomCtx extends Record<string, any>, CustomMadeArgs extends Record<string, any>, Visibility extends FunctionVisibility, ExtraArgs extends Record<string, any> = Record<string, any>>(query: QueryBuilder<any, Visibility>, customization: Customization<any, CustomArgsValidator, CustomCtx, CustomMadeArgs, ExtraArgs>): CustomBuilder<'query', CustomArgsValidator, CustomCtx, CustomMadeArgs, any, Visibility, ExtraArgs>;
157
- declare function zCustomMutation<CustomArgsValidator extends PropertyValidators, CustomCtx extends Record<string, any>, CustomMadeArgs extends Record<string, any>, Builder extends (fn: any) => any, Visibility extends FunctionVisibility = 'public', ExtraArgs extends Record<string, any> = Record<string, any>>(mutation: Builder, customization: Customization<any, CustomArgsValidator, CustomCtx, CustomMadeArgs, ExtraArgs>): CustomBuilder<'mutation', CustomArgsValidator, CustomCtx, CustomMadeArgs, ExtractCtx<Builder>, Visibility, ExtraArgs>;
158
- declare function zCustomAction<CustomArgsValidator extends PropertyValidators, CustomCtx extends Record<string, any>, CustomMadeArgs extends Record<string, any>, Builder extends (fn: any) => any, Visibility extends FunctionVisibility = 'public', ExtraArgs extends Record<string, any> = Record<string, any>>(action: Builder, customization: Customization<any, CustomArgsValidator, CustomCtx, CustomMadeArgs, ExtraArgs>): CustomBuilder<'action', CustomArgsValidator, CustomCtx, CustomMadeArgs, ExtractCtx<Builder>, Visibility, ExtraArgs>;
159
-
160
- /**
161
- * Creates a reusable query builder from a Convex query builder.
162
- * Returns a builder function that accepts Convex-style config objects with args, handler, and returns.
163
- *
164
- * @example
165
- * ```ts
166
- * import { query } from './_generated/server'
167
- * import { zQueryBuilder } from 'zodvex'
168
- *
169
- * // Create a reusable builder
170
- * export const zq = zQueryBuilder(query)
171
- *
172
- * // Use it with Convex-style object syntax
173
- * export const getUser = zq({
174
- * args: { id: z.string() },
175
- * handler: async (ctx, { id }) => {
176
- * return ctx.db.get(id)
177
- * }
178
- * })
179
- * ```
180
- */
181
- declare function zQueryBuilder<Builder extends (fn: any) => any>(builder: Builder): <A extends z.ZodTypeAny | Record<string, z.ZodTypeAny>, R extends z.ZodTypeAny | undefined = undefined, Visibility extends FunctionVisibility = ExtractVisibility<Builder>>(config: {
182
- args?: A;
183
- handler: (ctx: ExtractCtx<Builder>, args: ZodToConvexArgs<A extends undefined ? Record<string, never> : A>) => InferHandlerReturns<R> | Promise<InferHandlerReturns<R>>;
184
- returns?: R;
185
- }) => RegisteredQuery<Visibility, ZodToConvexArgs<A extends undefined ? Record<string, never> : A>, Promise<InferReturns<R>>>;
186
- /**
187
- * Creates a reusable mutation builder from a Convex mutation builder.
188
- * Returns a builder function that accepts Convex-style config objects with args, handler, and returns.
189
- *
190
- * @example
191
- * ```ts
192
- * import { mutation } from './_generated/server'
193
- * import { zMutationBuilder } from 'zodvex'
194
- *
195
- * // Create a reusable builder
196
- * export const zm = zMutationBuilder(mutation)
197
- *
198
- * // Use it with Convex-style object syntax
199
- * export const updateUser = zm({
200
- * args: { id: z.string(), name: z.string() },
201
- * handler: async (ctx, { id, name }) => {
202
- * return ctx.db.patch(id, { name })
203
- * }
204
- * })
205
- * ```
206
- */
207
- declare function zMutationBuilder<Builder extends (fn: any) => any>(builder: Builder): <A extends z.ZodTypeAny | Record<string, z.ZodTypeAny>, R extends z.ZodTypeAny | undefined = undefined, Visibility extends FunctionVisibility = ExtractVisibility<Builder>>(config: {
208
- args?: A;
209
- handler: (ctx: ExtractCtx<Builder>, args: ZodToConvexArgs<A extends undefined ? Record<string, never> : A>) => InferHandlerReturns<R> | Promise<InferHandlerReturns<R>>;
210
- returns?: R;
211
- }) => RegisteredMutation<Visibility, ZodToConvexArgs<A extends undefined ? Record<string, never> : A>, Promise<InferReturns<R>>>;
212
- /**
213
- * Creates a reusable action builder from a Convex action builder.
214
- * Returns a builder function that accepts Convex-style config objects with args, handler, and returns.
215
- *
216
- * @example
217
- * ```ts
218
- * import { action } from './_generated/server'
219
- * import { zActionBuilder } from 'zodvex'
220
- *
221
- * // Create a reusable builder
222
- * export const za = zActionBuilder(action)
223
- *
224
- * // Use it with Convex-style object syntax
225
- * export const sendEmail = za({
226
- * args: { to: z.string().email(), subject: z.string() },
227
- * handler: async (ctx, { to, subject }) => {
228
- * // Send email
229
- * }
230
- * })
231
- * ```
232
- */
233
- declare function zActionBuilder<Builder extends (fn: any) => any>(builder: Builder): <A extends z.ZodTypeAny | Record<string, z.ZodTypeAny>, R extends z.ZodTypeAny | undefined = undefined, Visibility extends FunctionVisibility = ExtractVisibility<Builder>>(config: {
234
- args?: A;
235
- handler: (ctx: ExtractCtx<Builder>, args: ZodToConvexArgs<A extends undefined ? Record<string, never> : A>) => InferHandlerReturns<R> | Promise<InferHandlerReturns<R>>;
236
- returns?: R;
237
- }) => RegisteredAction<Visibility, ZodToConvexArgs<A extends undefined ? Record<string, never> : A>, Promise<InferReturns<R>>>;
238
- /**
239
- * Creates a custom query builder with context injection from a Convex query builder.
240
- * Allows you to add custom context (like auth, permissions, etc.) to your queries.
241
- *
242
- * @example
243
- * ```ts
244
- * import { type QueryCtx, query } from './_generated/server'
245
- * import { zCustomQueryBuilder, customCtx } from 'zodvex'
246
- *
247
- * // Create a builder with auth context
248
- * export const authQuery = zCustomQueryBuilder(
249
- * query,
250
- * customCtx(async (ctx: QueryCtx) => {
251
- * const user = await getUserOrThrow(ctx)
252
- * return { user }
253
- * })
254
- * )
255
- *
256
- * // Use it with automatic user injection
257
- * export const getMyProfile = authQuery({
258
- * args: {},
259
- * handler: async (ctx) => {
260
- * // ctx.user is automatically available
261
- * return ctx.db.get(ctx.user._id)
262
- * }
263
- * })
264
- * ```
265
- */
266
- declare function zCustomQueryBuilder<Builder extends (fn: any) => any, CustomArgsValidator extends PropertyValidators, CustomCtx extends Record<string, any>, CustomMadeArgs extends Record<string, any>, Visibility extends FunctionVisibility = ExtractVisibility<Builder>, ExtraArgs extends Record<string, any> = Record<string, any>>(query: Builder, customization: Customization<any, CustomArgsValidator, CustomCtx, CustomMadeArgs, ExtraArgs>): CustomBuilder<'query', CustomArgsValidator, CustomCtx, CustomMadeArgs, ExtractCtx<Builder>, Visibility, ExtraArgs>;
267
- /**
268
- * Creates a custom mutation builder with context injection from a Convex mutation builder.
269
- * Allows you to add custom context (like auth, permissions, etc.) to your mutations.
270
- *
271
- * @example
272
- * ```ts
273
- * import { type MutationCtx, mutation } from './_generated/server'
274
- * import { zCustomMutationBuilder, customCtx } from 'zodvex'
275
- *
276
- * // Create a builder with auth context
277
- * export const authMutation = zCustomMutationBuilder(
278
- * mutation,
279
- * customCtx(async (ctx: MutationCtx) => {
280
- * const user = await getUserOrThrow(ctx)
281
- * return { user }
282
- * })
283
- * )
284
- *
285
- * // Use it with automatic user injection
286
- * export const updateProfile = authMutation({
287
- * args: { name: z.string() },
288
- * handler: async (ctx, { name }) => {
289
- * // ctx.user is automatically available
290
- * await ctx.db.patch(ctx.user._id, { name })
291
- * }
292
- * })
293
- * ```
294
- */
295
- declare function zCustomMutationBuilder<Builder extends (fn: any) => any, CustomArgsValidator extends PropertyValidators, CustomCtx extends Record<string, any>, CustomMadeArgs extends Record<string, any>, Visibility extends FunctionVisibility = ExtractVisibility<Builder>, ExtraArgs extends Record<string, any> = Record<string, any>>(mutation: Builder, customization: Customization<any, CustomArgsValidator, CustomCtx, CustomMadeArgs, ExtraArgs>): CustomBuilder<'mutation', CustomArgsValidator, CustomCtx, CustomMadeArgs, ExtractCtx<Builder>, Visibility, ExtraArgs>;
296
- /**
297
- * Creates a custom action builder with context injection from a Convex action builder.
298
- * Allows you to add custom context (like auth, permissions, etc.) to your actions.
299
- *
300
- * @example
301
- * ```ts
302
- * import { type ActionCtx, action } from './_generated/server'
303
- * import { zCustomActionBuilder, customCtx } from 'zodvex'
304
- *
305
- * // Create a builder with auth context
306
- * export const authAction = zCustomActionBuilder(
307
- * action,
308
- * customCtx(async (ctx: ActionCtx) => {
309
- * const identity = await ctx.auth.getUserIdentity()
310
- * if (!identity) throw new Error('Unauthorized')
311
- * return { userId: identity.subject }
312
- * })
313
- * )
314
- *
315
- * // Use it with automatic auth injection
316
- * export const sendEmail = authAction({
317
- * args: { to: z.string().email() },
318
- * handler: async (ctx, { to }) => {
319
- * // ctx.userId is automatically available
320
- * await sendEmailService(to, ctx.userId)
321
- * }
322
- * })
323
- * ```
324
- */
325
- declare function zCustomActionBuilder<Builder extends (fn: any) => any, CustomArgsValidator extends PropertyValidators, CustomCtx extends Record<string, any>, CustomMadeArgs extends Record<string, any>, Visibility extends FunctionVisibility = ExtractVisibility<Builder>, ExtraArgs extends Record<string, any> = Record<string, any>>(action: Builder, customization: Customization<any, CustomArgsValidator, CustomCtx, CustomMadeArgs, ExtraArgs>): CustomBuilder<'action', CustomArgsValidator, CustomCtx, CustomMadeArgs, ExtractCtx<Builder>, Visibility, ExtraArgs>;
326
-
327
- type ConvexCodec<T> = {
328
- validator: any;
329
- encode: (value: T) => any;
330
- decode: (value: any) => T;
331
- pick: <K extends keyof T>(keys: K[]) => ConvexCodec<Pick<T, K>>;
332
- };
333
- declare function convexCodec<T>(schema: z.ZodType<T>): ConvexCodec<T>;
334
- declare function toConvexJS(schema?: any, value?: any): any;
335
- declare function fromConvexJS(value: any, schema: any): any;
336
-
337
- type BaseCodec = {
338
- check: (schema: any) => boolean;
339
- toValidator: (schema: any) => any;
340
- fromConvex: (value: any, schema: any) => any;
341
- toConvex: (value: any, schema: any) => any;
342
- };
343
- declare function registerBaseCodec(codec: BaseCodec): void;
344
- declare function findBaseCodec(schema: any): BaseCodec | undefined;
345
- declare function isDateSchema(schema: any): boolean;
346
-
347
- declare function zodDoc<TableName extends string, Shape extends z.ZodRawShape, Schema extends z.ZodObject<Shape>>(tableName: TableName, schema: Schema): z.ZodObject<Shape & {
348
- _id: ReturnType<typeof zid<TableName>>;
349
- _creationTime: z.ZodNumber;
350
- }>;
351
- declare function zodDocOrNull<TableName extends string, Shape extends z.ZodRawShape, Schema extends z.ZodObject<Shape>>(tableName: TableName, schema: Schema): z.ZodUnion<readonly [z.ZodObject<Readonly<{
352
- [k: string]: z.core.$ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>;
353
- }> & {
354
- _id: z.ZodType<GenericId<TableName>, unknown, z.core.$ZodTypeInternals<GenericId<TableName>, unknown>> & {
355
- _tableName: TableName;
356
- };
357
- _creationTime: z.ZodNumber;
358
- }, z.core.$strip>, z.ZodNull]>;
359
- /**
360
- * Defines a Convex table using a raw Zod shape (an object mapping field names to Zod types).
361
- *
362
- * This function intentionally accepts a raw shape instead of a ZodObject instance.
363
- * Accepting raw shapes allows TypeScript to infer field types more accurately and efficiently,
364
- * leading to better type inference and performance throughout the codebase.
365
- * This architectural decision is important for projects that rely heavily on type safety and
366
- * developer experience, as it avoids the type erasure that can occur when using ZodObject directly.
367
- *
368
- * Returns the Table definition along with Zod schemas for documents and arrays.
369
- *
370
- * @param name - The table name
371
- * @param shape - A raw object mapping field names to Zod validators
372
- * @returns A Table with attached shape, zDoc schema, and docArray helper
373
- *
374
- * @example
375
- * ```ts
376
- * const Users = zodTable('users', {
377
- * name: z.string(),
378
- * email: z.string().email(),
379
- * age: z.number().optional()
380
- * })
381
- *
382
- * // Use in schema
383
- * export default defineSchema({ users: Users.table })
384
- *
385
- * // Use for return types
386
- * export const getUsers = zQuery(query, {},
387
- * async (ctx) => ctx.db.query('users').collect(),
388
- * { returns: Users.docArray }
389
- * )
390
- * ```
391
- */
392
- declare function zodTable<TableName extends string, Shape extends Record<string, z.ZodTypeAny>>(name: TableName, shape: Shape): {
393
- name: TableName;
394
- table: convex_server.TableDefinition<convex_values.VObject<convex_server.Expand<{ [Property_1 in { [Property in keyof ConvexValidatorFromZodFieldsAuto<Shape>]: ConvexValidatorFromZodFieldsAuto<Shape>[Property]["isOptional"] extends "optional" ? Property : never; }[keyof Shape]]?: Exclude<convex_values.Infer<ConvexValidatorFromZodFieldsAuto<Shape>[Property_1]>, undefined> | undefined; } & { [Property_2_1 in Exclude<keyof Shape, { [Property_2 in keyof ConvexValidatorFromZodFieldsAuto<Shape>]: ConvexValidatorFromZodFieldsAuto<Shape>[Property_2]["isOptional"] extends "optional" ? Property_2 : never; }[keyof Shape]>]: convex_values.Infer<ConvexValidatorFromZodFieldsAuto<Shape>[Property_2_1]>; }>, ConvexValidatorFromZodFieldsAuto<Shape>, "required", { [Property_3 in keyof ConvexValidatorFromZodFieldsAuto<Shape>]: Property_3 | `${Property_3 & string}.${ConvexValidatorFromZodFieldsAuto<Shape>[Property_3]["fieldPaths"]}`; }[keyof Shape] & string>, {}, {}, {}>;
395
- doc: convex_values.VObject<convex_server.Expand<{ [Property_5 in (convex_helpers.Expand<ConvexValidatorFromZodFieldsAuto<Shape> & {
396
- _id: convex_values.VId<GenericId<TableName>, "required">;
397
- _creationTime: convex_values.VFloat64<number, "required">;
398
- }> extends infer T_1 extends Record<string, convex_values.GenericValidator> ? { [Property_4 in keyof T_1]: T_1[Property_4]["isOptional"] extends "optional" ? Property_4 : never; } : never)[keyof convex_helpers.Expand<ConvexValidatorFromZodFieldsAuto<Shape> & {
399
- _id: convex_values.VId<GenericId<TableName>, "required">;
400
- _creationTime: convex_values.VFloat64<number, "required">;
401
- }>]]?: Exclude<convex_values.Infer<convex_helpers.Expand<ConvexValidatorFromZodFieldsAuto<Shape> & {
402
- _id: convex_values.VId<GenericId<TableName>, "required">;
403
- _creationTime: convex_values.VFloat64<number, "required">;
404
- }>[Property_5]>, undefined> | undefined; } & { [Property_1_1 in Exclude<keyof convex_helpers.Expand<ConvexValidatorFromZodFieldsAuto<Shape> & {
405
- _id: convex_values.VId<GenericId<TableName>, "required">;
406
- _creationTime: convex_values.VFloat64<number, "required">;
407
- }>, (convex_helpers.Expand<ConvexValidatorFromZodFieldsAuto<Shape> & {
408
- _id: convex_values.VId<GenericId<TableName>, "required">;
409
- _creationTime: convex_values.VFloat64<number, "required">;
410
- }> extends infer T_2 extends Record<string, convex_values.GenericValidator> ? { [Property_4_1 in keyof T_2]: T_2[Property_4_1]["isOptional"] extends "optional" ? Property_4_1 : never; } : never)[keyof convex_helpers.Expand<ConvexValidatorFromZodFieldsAuto<Shape> & {
411
- _id: convex_values.VId<GenericId<TableName>, "required">;
412
- _creationTime: convex_values.VFloat64<number, "required">;
413
- }>]>]: convex_values.Infer<convex_helpers.Expand<ConvexValidatorFromZodFieldsAuto<Shape> & {
414
- _id: convex_values.VId<GenericId<TableName>, "required">;
415
- _creationTime: convex_values.VFloat64<number, "required">;
416
- }>[Property_1_1]>; }>, convex_helpers.Expand<ConvexValidatorFromZodFieldsAuto<Shape> & {
417
- _id: convex_values.VId<GenericId<TableName>, "required">;
418
- _creationTime: convex_values.VFloat64<number, "required">;
419
- }>, "required", (convex_helpers.Expand<ConvexValidatorFromZodFieldsAuto<Shape> & {
420
- _id: convex_values.VId<GenericId<TableName>, "required">;
421
- _creationTime: convex_values.VFloat64<number, "required">;
422
- }> extends infer T_3 extends convex_values.PropertyValidators ? { [Property_2_1_1 in keyof T_3]: Property_2_1_1 | `${Property_2_1_1 & string}.${T_3[Property_2_1_1]["fieldPaths"]}`; } : never)[keyof convex_helpers.Expand<ConvexValidatorFromZodFieldsAuto<Shape> & {
423
- _id: convex_values.VId<GenericId<TableName>, "required">;
424
- _creationTime: convex_values.VFloat64<number, "required">;
425
- }>]>;
426
- withoutSystemFields: ConvexValidatorFromZodFieldsAuto<Shape>;
427
- withSystemFields: convex_helpers.Expand<ConvexValidatorFromZodFieldsAuto<Shape> & {
428
- _id: convex_values.VId<GenericId<TableName>, "required">;
429
- _creationTime: convex_values.VFloat64<number, "required">;
430
- }>;
431
- systemFields: {
432
- _id: convex_values.VId<GenericId<TableName>, "required">;
433
- _creationTime: convex_values.VFloat64<number, "required">;
434
- };
435
- _id: convex_values.VId<GenericId<TableName>, "required">;
436
- } & {
437
- shape: Shape;
438
- zDoc: z.ZodObject<Shape & {
439
- _id: ReturnType<typeof zid<TableName>>;
440
- _creationTime: z.ZodNumber;
441
- }>;
442
- docArray: z.ZodArray<z.ZodObject<Readonly<{
443
- [k: string]: z.core.$ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>;
444
- }> & {
445
- _id: z.ZodType<GenericId<TableName>, unknown, z.core.$ZodTypeInternals<GenericId<TableName>, unknown>> & {
446
- _tableName: TableName;
447
- };
448
- _creationTime: z.ZodNumber;
449
- }, z.core.$strip>>;
450
- };
451
-
452
- declare function pick<T extends Record<string, any>, K extends keyof T>(obj: T, keys: K[]): Pick<T, K>;
453
- declare function returnsAs<R extends z.ZodTypeAny>(): <T extends z.input<R>>(v: T) => T;
454
- declare function formatZodIssues(error: z.ZodError, context?: 'args' | 'returns' | 'input' | 'output' | 'codec'): {
455
- error: string;
456
- context: "output" | "input" | "args" | "returns" | "codec" | undefined;
457
- issues: {
458
- path: string;
459
- code: "custom" | "invalid_type" | "unrecognized_keys" | "invalid_union" | "invalid_value" | "invalid_key" | "too_big" | "too_small" | "invalid_format" | "not_multiple_of" | "invalid_element";
460
- message: string;
461
- }[];
462
- flatten: any;
463
- };
464
- declare function handleZodValidationError(e: unknown, context: 'args' | 'returns' | 'input' | 'output' | 'codec'): never;
465
- declare function zPaginated<T extends z.ZodTypeAny>(item: T): z.ZodObject<{
466
- page: z.ZodArray<T>;
467
- isDone: z.ZodBoolean;
468
- continueCursor: z.ZodOptional<z.ZodNullable<z.ZodString>>;
469
- }, z.core.$strip>;
470
- /**
471
- * Maps Date fields to number fields for docSchema generation.
472
- * Handles Date, Date.optional(), Date.nullable(), and Date.default() cases.
473
- * Returns the original field for non-Date types.
474
- */
475
- declare function mapDateFieldToNumber(field: z.ZodTypeAny): z.ZodTypeAny;
476
- type Mask = readonly string[] | Record<string, boolean | 1 | true>;
477
- /**
478
- * Returns a plain shape object containing only the selected fields.
479
- * Accepts either a ZodObject or a raw shape object.
480
- */
481
- declare function pickShape(schemaOrShape: z.ZodObject<any> | Record<string, any>, mask: Mask): Record<string, any>;
482
- declare function safePick(schema: z.ZodObject<any>, mask: Mask): z.ZodObject<any>;
483
- /**
484
- * Convenience: omit a set of keys by building the complement.
485
- * Avoids using Zod's .omit() which can cause type depth issues.
486
- */
487
- declare function safeOmit(schema: z.ZodObject<any>, mask: Mask): z.ZodObject<any>;
488
-
489
- export { type ConvexCodec, type ConvexValidatorFromZod, type ConvexValidatorFromZodFieldsAuto, type CustomBuilder, type ExtractCtx, type ExtractVisibility, type InferArgs, type InferHandlerReturns, type InferReturns, type PreserveReturnType, type Zid, type ZodToConvexArgs, type ZodValidator, convexCodec, customFnBuilder, findBaseCodec, formatZodIssues, fromConvexJS, getObjectShape, handleZodValidationError, isDateSchema, makeUnion, mapDateFieldToNumber, pick, pickShape, registerBaseCodec, registryHelpers, returnsAs, safeOmit, safePick, toConvexJS, zActionBuilder, zCustomAction, zCustomActionBuilder, zCustomMutation, zCustomMutationBuilder, zCustomQuery, zCustomQueryBuilder, zMutationBuilder, zPaginated, zQueryBuilder, zid, zodDoc, zodDocOrNull, zodTable, zodToConvex, zodToConvexFields };