zod return validator in convex-helpers?
Are there any plans to add return validator in convex-helpers/server/zod?
8 Replies
I added that when I originally introduced the zod helpers - it's called "output" since we hadn't landed on the name "returns" yet. I'm hesitant to change the name as it'd be a breaking change, but it would be more symmetric...
what would have made this more discoverable? Where could I drop a comment pointing people to use "output"?
https://stack.convex.dev/typescript-zod-function-validation#output-validation
Stack
Zod with TypeScript for Server-side Validation and End-to-End Types
Use Zod with TypeScript for argument validation on your server functions allows you to both protect against invalid data, and define TypeScript types ...
Thanks for pointing out. Rather than removing "output", can we consider keeping it and adding "returns" to prevent any confusion in the future. Also they follow different syntax for defining schema.
Interesting - I assumed people would want to use Zod for both input & output, but would you prefer to use Convex validators for the return validators? The types will likely get pretty gnarly, but maybe I could get it to work specifying either if there's a lot of interest.
Out of curiosity, what is your usecase for the return validators? Just an extra sanity check?
Also feel free to fork & submit a PR if you get it working - I won't have bandwidth to dig in here for a while
Edited my comment (changed v to z). It would be nice to use similar syntax for both the input and output validators but shouldn’t be a major concern as we have atleast output validator to work with.
The
returns
type actually isn't always an object. you can pass v.null()
(z.null()
) or any valid Convex value. So you could do returns: z.string()
or z.object({ id: z.string() })
but passing an object directly would require the library assuming you want to turn that into a z.object
right?It might work with convex values but I think zod validators (args and output) are not flexible IMO. Please correct me if my understanding is incorrect.
z.object doesn't work on args, it only takes {}
{} doesn't work on output, it only takes zod type
That is because args has to be an object and the output doesn’t, as that’s how convex functions are defined (named arguments). So anything you can do with Convex you can do with the zod validators too. I’m not sure I’m understanding. To return {} you use z.object. To have z.object as args you just don’t include the z.object part.
Would it be better if args accepted z.object explicitly?
I understand now. Thanks for the clarification.