Skip to content

Commit af896cb

Browse files
committed
get it working for zod too
1 parent d1462ba commit af896cb

File tree

1 file changed

+12
-9
lines changed
  • packages/convex-helpers/server

1 file changed

+12
-9
lines changed

packages/convex-helpers/server/zod.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -309,17 +309,20 @@ function customFnBuilder(
309309
const inputMod = mod.input ?? NoOp.input;
310310
const inputArgs = mod.args ?? NoOp.args;
311311
return function customBuilder(fn: any): any {
312-
let returns = fn.returns ?? fn.output;
313-
if (returns && !(returns instanceof z.ZodType)) {
314-
returns = z.object(returns);
315-
}
312+
const { args, handler = fn, returns: maybeObject, ...extra } = fn;
313+
314+
const returns =
315+
maybeObject && !(maybeObject instanceof z.ZodType)
316+
? z.object(maybeObject)
317+
: maybeObject;
316318

317319
const returnValidator =
318-
fn.returns && !fn.skipConvexValidation
320+
returns && !fn.skipConvexValidation
319321
? { returns: zodOutputToConvex(returns) }
320322
: null;
321-
if ("args" in fn && !fn.skipConvexValidation) {
322-
let argsValidator = fn.args;
323+
324+
if (args && !fn.skipConvexValidation) {
325+
let argsValidator = args;
323326
if (argsValidator instanceof z.ZodType) {
324327
if (argsValidator instanceof z.ZodObject) {
325328
argsValidator = argsValidator._def.shape();
@@ -341,6 +344,7 @@ function customFnBuilder(
341344
const added = await inputMod(
342345
ctx,
343346
pick(allArgs, Object.keys(inputArgs)) as any,
347+
extra,
344348
);
345349
const rawArgs = pick(allArgs, Object.keys(argsValidator));
346350
const parsed = z.object(argsValidator).safeParse(rawArgs);
@@ -370,11 +374,10 @@ function customFnBuilder(
370374
"modifier, you must declare the arguments for the function too.",
371375
);
372376
}
373-
const handler = fn.handler ?? fn;
374377
return builder({
375378
...returnValidator,
376379
handler: async (ctx: any, args: any) => {
377-
const added = await inputMod(ctx, args);
380+
const added = await inputMod(ctx, args, extra);
378381
if (returns) {
379382
// We don't catch the error here. It's a developer error and we
380383
// don't want to risk exposing the unexpected value to the client.

0 commit comments

Comments
 (0)