Zod Unions appear to break `Table` helper (maybe zodToConvex related?)
https://stack.convex.dev/convex-the-database-that-made-me-switch-careers
I'm following the pattern described in this article and getting an type error here. Looks to be since the
Table expects a Record while the union produces an array of members.
This still works below so i'll do this, but wanted to flag this in case it wasn't known
export const TestTable = Table('test', convexTestSchema.members) will same type error
The problem Im trying to solve is dynamic data for a data table where each field/column type can only support specific value types (like a short text field can only support z.string()`. I'm writing all of these combinations as unions and merging the rest of the base table fields in each union member.
Seems like teh best way of going about this so its impossibel to accidentally pass in a string value for a boolean fieldConvex: The Database that Made Me Switch Careers
Today is my first day working for Convex 🎉, so I thought I’d take the opportunity to share my thoughts on why I decided to join and what excites me a...

5 Replies
This issue seem to still exist in soem way. Both using the convex-helper package as well as the new Zodvex package from @panzacoder .
Using
defineTable(zodToConvex(unionZodSchema)) works but disallows us the usefull helper functiions
Using convexHelpers Table('unionTable', zodToConvexFields(unionZodSchema)) does not work because the fields are not a Record<string, Validator<any, any, any>>
Neither does Zodvex' zodTable('unionTable', unionZodSchema) because the ZodSchema shape is not a Record<string, z.ZodTypeAny>.
In the vanila convex the argument to defineTable is of type Validator<Record<string, any>, "required", any>, so wrapping the argument for Table of zodTable in a Valdiator, would be enough?@Bazza I think the answer here might actually be to use a Discriminated Union: https://zod.dev/api?id=discriminated-unions
I do have a fix for zodvex I need to release around Enums, but I don’t think that would affect this.
Zod
Defining schemas | Zod
Complete API reference for all Zod schema types, methods, and validation features
i actually use discriminated unions in my code, issue is that the convex-helpers Table function (and by extension the zodTable) only accepts the extends
Record<string, GenericValidator> as DocumentSchema, while vanilla convex has overloads for DocumentSchema extends Validator<Record<string, any>, "required", any> when creating a TableDefinition.
I tried recreating the convex-helper function; but it doesn't match well with the 'without and withSystem fields' for convex-helpers or the shape field for zodvex ...I believe discriminated unions work when nested in another field, both for schema and function args validation.
That does work, but it has the limitation that your can't use indexing for mutual fields for nested entities. (which IS possible when having a top level union type using the the default defineTable).